Next.js Discord

Discord Forum

Next auth is not logging out the user after the session has expired.

Unanswered
Cape horse mackerel posted this in #help-forum
Open in Discord
Cape horse mackerelOP
Hello guys, I can't log out the user after session has expired.
I have this setup
// auth.ts
session: {
    // Seconds - How long until an idle session expires and is no longer valid.
    maxAge: secondsFromNowToMidnight,
    strategy: "jwt",
  },

// auth-provider.tsx
 <SessionProvider
      {...props}
      // If the session state has expired when it is triggered, the user will be automatically logout.
      // https://next-auth.js.org/getting-started/client#refetching-the-session
      refetchInterval={10 * 60} // 10 minutes
      refetchOnWindowFocus={false}
      refetchWhenOffline={false}
    >
      {children}
    </SessionProvider>

It happens sometimes that some users can trigger API call and get 401 as their session has expired.
Any idea?

4 Replies

Cape horse mackerelOP
Also, I have a global layout file where I'm calling session guard
// session-guard.tsx
... the rest is omitted
const { status } = useSession({
    onUnauthenticated() {
      redirect(`/api/auth/signin?callbackUrl=${uri}`);
    },
    required: true,
  });

// layout.tsx
<ReactQueryProvider>
<SessionGuard>
{children}
</SessionGuard>
</ReactQueryProvider>
Cape horse mackerelOP
Just got the 401 error
So my token is expired but the user is not logged out from the app
it could be due to cache, are you caching any routes?
Cape horse mackerelOP
nope, I've just found out that the next auth session and token coming from the server are not synced up