Issue with NextAuth destroying the session? #2370
Replies: 1 comment 1 reply
-
The session validity is independent of your provider's token expiry. You could say The session is considered expired when the session cookie is removed. By default, this is 30 days of inactivity. In case the You can override the default expiry with the By default a session is never considered stale from the client's perspective, meaning it won't get a new session in the lifetime of your SPA (eg. a reload would re-fetch the session). You can change that behaviour here: You can also poll the session regularly in the background, to keep it active, see the above link for that too. |
Beta Was this translation helpful? Give feedback.
-
In my app I have a wrapper layout component over the whole application in the _app.js file which evaluates the useSession()'s loading value to determine if a should show or it can render the props.children. Which in theory works perfectly because it will just show a loader one time and once the session is ready (e.g. loading === false) it will display the page accordingly to the session state. And because of SPA, we never should see the loader again.
The problem is, for some reason NextAuth likes to destroy the session at random times. I use Okta provider, and the timeout is set to 8 hours for the OpenID tokens. When the session destroy happens, the loading value becomes true again so my wrapper layout component shows the Loader component again for a few seconds. What I'm trying to understand is why NextAuth is destroying the session, even though the OpenID tokens (access tokens) still have hours before becoming invalid.
I also noticed in the NextJS console output this line whenever this happens:
{ loading: true, session: undefined }
event - build page: /api/auth/[...nextauth]
Which, could be the culprit. However, this also happened a few times in my deployed app. So not sure if it's related to fast refresh. Anyway what could be triggering a rebuild of nextauth backend component?
Hopefully someone has had a similar issue or can provide some suggestions.
Beta Was this translation helpful? Give feedback.
All reactions