nuxt-auth icon indicating copy to clipboard operation
nuxt-auth copied to clipboard

The accessToken isn't refreshed if it expires before the refreshToken

Open clcoco opened this issue 10 months ago • 2 comments

Environment

Reproduction

  • use the local provider
  • have your accessToken last less than your refreshToken

Describe the bug

The accessToken should be refreshed too once it expires instead of relying on the refresh token's expiration to be refreshed

Additional context

No response

Logs


clcoco avatar Mar 10 '25 12:03 clcoco

I am facing the same problem :( Checking the source here, I think the reason is that it is watching the useState value instead of watching the cookie value.

noriyuki-shimizu avatar Mar 13 '25 14:03 noriyuki-shimizu

FWIIW, I was using middleware getSession only with refresh Nuxt config enablePeriodically enabled. Everything works fine in that regard, except it's a bit obsessive for my portal needs. After disabling enablePeriodically, I run into this.

Changing app/middleware/get-session.ts to call refresh -- because it in turn calls session -- instead of directly calling getSession works for me:

export default defineNuxtRouteMiddleware((to, from) => {
  const { getSession, refresh, signOut, status } = useAuth()

  if (status.value !== 'unauthenticated') {
    refresh().then((result) => {
      console.log('get-session result:', result)
      //getSession().then((result) => {
      //  console.log('get-session result:', result)
      //})
    }).catch((err) => {
      console.error('get-session error:', err)
      signOut({callbackUrl: 'logout', external: false})
    })
  }

  //console.log(from, '->', to)
})

theflyingape avatar Mar 20 '25 17:03 theflyingape