NuxtN
Nuxt13mo ago
marvin

Nuxt Auth

Hi,

I am currently struggling with implementing OAuth2 for my application. I have added @sidebase/nuxt-auth with a NuxtAuthHandler with Authentik as following:
export default NuxtAuthHandler({
  secret: useRuntimeConfig().auth.secret,
  providers: [
    AuthentikProvider.default({
      clientId: useRuntimeConfig().auth.clientId,
      clientSecret: useRuntimeConfig().auth.clientSecret,
      issuer: useRuntimeConfig().auth.issuer
    })
  ],
  callbacks: {
    jwt (data) {
      const { token, account } = data
      if (account) {
        token.accessToken = account.access_token
      }
      return token
    },
    session ({ session, token }) {
      session.accessToken = token.accessToken
      return session
    }
  }
})

The defined callbacks functions allow me to use the accessToken from Authentik in the frontend and therefore as a header in useFetch to my external (Spring Boot) API.

The problem I have is the accessToken being valid for only 5 minutes by default. However @sidebase/nuxt-auth doesn't recognise this. The state keeps staying on "authenticated", and the accessToken doesn't get refreshed in any way. The user has to sign out completely and then sign in again to get a new accessToken for the next 5 minutes. Of course I could increase the accessToken lifetime, but that wouldn't fix the bad user experience.

How can I fix this problem, or what am I doing wrong here?
Was this page helpful?