0

currently I'm facing a weird problem with nuxt auth. My config looks like this:

auth: {
    strategies: {
      google: {
        clientId: '<clientId>',
        codeChallengeMethod: '',
        responseType: 'code',
        token: {
          property: 'token',
        },
        endpoints: {
          token: 'http://localhost:4000/api/auth/google/token',
          userInfo: 'http://localhost:4000/api/auth/google/user',
        },
        localStorage: false,
        cookie: true,
      },
    },
    redirect: {
      home: '/',
      logout: '/login',
      login: '/login',
    },
    tokenRequired: true,
    plugins: [ { src: '~/plugins/nuxt-auth.js', ssr: true }],
  },

I start the login flow with:

await this.$auth.loginWith('google')

In the backend I decode the JWT, verify it and return the user. So far so good.

I encountered the problem, that even though all the requests are correct, and $auth.loggedIn is true, Nuxt does not redirect me to the main page.

After a litte debugging I found out, that Nuxt sends two requests to the user endpoint:

The first request is totally empty and doesn't contain a cookie. That's why my API returns 401 and nuxt runs into an error --> $auth.loggedIn=false

Within milliseconds there is another request to the user endpoint with the cookie, the user data is returned und $auth.loggedin is true. Nuxt doesn't redirect, because an error occured in the first request.

This behavior is visible on every page refresh and every login. I tried a lot, but I really can't find my mistake here.

Maybe one of you guys can help out here

pipo92
  • 119
  • 10

0 Answers0