Search code examples
sessionnext.jsnext-auth

Next-auth how to prevent session new expiration date?


I am using next-auth in my next app for user authentification.

Each time the user switch to another window or tab in the browser, and go back to the application, the session takes a new expiration date like shown below :

{
  expires: '2023-01-05T00:53:50.523Z',
  accessToken: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjYzOTZiMTlhYTczMmUzMzYwMjU2ZjBlMiIsImlhdCI6MTY3Mjg3OTk3MCwiZXhwIjoxNjcyOTY2MzcwfQ.NSFAUb9kHgKkCoBtw60tG9vgOXMTwX8lD7Kgb8TxsFI'
}

swith tab and go back

{
  expires: '2023-01-05T00:54:05.724Z',
  accessToken: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjYzOTZiMTlhYTczMmUzMzYwMjU2ZjBlMiIsImlhdCI6MTY3Mjg3OTk3MCwiZXhwIjoxNjcyOTY2MzcwfQ.NSFAUb9kHgKkCoBtw60tG9vgOXMTwX8lD7Kgb8TxsFI'
}

Is it possible to prevent that ? I don't want to have a new expiration date. I read the official documentation but I found nothing about this feature.

Thank you for your attention!


Solution

  • you can set a refetchOnWindowFocus option to false on SessionProvider to avoid it auto refresh on every focus tab.

    Documentation example

    <SessionProvider
      session={session}
      // In case you use a custom path and your app lives at "/cool-app" rather than at the root "/"
      basePath="cool-app"
      // Re-fetch session every 5 minutes
      refetchInterval={5 * 60}
      // Disable Re-fetches session when window is focused
      refetchOnWindowFocus={false}
    >
      <Component {...pageProps} />
    </SessionProvider>;