useSession({ required: true }), which always returns a non-null Session object #1370

Closed
opened 2026-03-13 08:35:23 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @widavies on GitHub (Jun 17, 2025).

Inspired from https://github.com/nextauthjs/next-auth/issues/1210

Summary of proposed feature

A living session could be a requirement for specific pages. If it doesn’t exist, then the user should be redirected to the sign in page with an error like "Session expired, please try signing in again".

Purpose of proposed feature

Sometimes, a user might log out by accident, or by deleting cookies on purpose. If that happens (e.g. on a separate tab), then useSession({ required: true }) should detect the absence of a session cookie and always return a non-nullable Session object type.

Detail about proposed feature

If the required option is specified, then an effect should be registered to redirect the user to the sign in page as soon as no session is available.

Potential problems

The session object could not only become nonexistent, but might even change over time. That edge case should be handled separately.

Describe any alternatives you've considered

Creating a hook in userland, e.g.:

function useSessionRequired() {
  const [session, loading] = useSession();
  const router = useRouter();

  React.useEffect(() => {
    if (!session && !loading) {
      router.push(`${process.env.NEXTAUTH_URL}/auth/sign-in?error=SessionExpired`);
    }
  }, [loading, router, session]);

  return [session, loading];
}
Originally created by @widavies on GitHub (Jun 17, 2025). Inspired from https://github.com/nextauthjs/next-auth/issues/1210 **Summary of proposed feature** A living session could be a requirement for specific pages. If it doesn’t exist, then the user should be redirected to the sign in page with an error like "Session expired, please try signing in again". **Purpose of proposed feature** Sometimes, a user might log out by accident, or by deleting cookies on purpose. If that happens (e.g. on a separate tab), then `useSession({ required: true })` should detect the absence of a session cookie and always return a non-nullable `Session` object type. **Detail about proposed feature** If the `required` option is specified, then an effect should be registered to redirect the user to the sign in page as soon as no session is available. **Potential problems** The session object could not only become nonexistent, but might even change over time. That edge case should be handled separately. **Describe any alternatives you've considered** Creating a hook in userland, e.g.: ``` function useSessionRequired() { const [session, loading] = useSession(); const router = useRouter(); React.useEffect(() => { if (!session && !loading) { router.push(`${process.env.NEXTAUTH_URL}/auth/sign-in?error=SessionExpired`); } }, [loading, router, session]); return [session, loading]; } ```
GiteaMirror added the nextjs label 2026-03-13 08:35:23 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Sep 16, 2025):

Hi, @widavies. I'm Dosu, and I'm helping the better-auth team manage their backlog and am marking this issue as stale.

Issue Summary:

  • You proposed adding a { required: true } option to the useSession hook to handle automatic redirection to sign-in when the session is missing.
  • This feature aims to address session loss scenarios like logging out elsewhere or cookie deletion.
  • You noted challenges with detecting session state changes and currently use a userland hook as a workaround.
  • No further comments or updates have been made on this issue.

Next Steps:

  • Please let me know if this issue is still relevant with the latest version of better-auth by commenting here.
  • If I don’t hear back within 7 days, this issue will be automatically closed.

Thanks for your understanding and contribution!

@dosubot[bot] commented on GitHub (Sep 16, 2025): Hi, @widavies. I'm [Dosu](https://dosu.dev), and I'm helping the better-auth team manage their backlog and am marking this issue as stale. **Issue Summary:** - You proposed adding a `{ required: true }` option to the `useSession` hook to handle automatic redirection to sign-in when the session is missing. - This feature aims to address session loss scenarios like logging out elsewhere or cookie deletion. - You noted challenges with detecting session state changes and currently use a userland hook as a workaround. - No further comments or updates have been made on this issue. **Next Steps:** - Please let me know if this issue is still relevant with the latest version of better-auth by commenting here. - If I don’t hear back within 7 days, this issue will be automatically closed. Thanks for your understanding and contribution!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1370