Auth-module: auth-next not redirecting after login and can't access protect page even if token is stored

Created on 18 Dec 2020  路  7Comments  路  Source: nuxt-community/auth-module

Version

"@nuxtjs/auth-next": "5.0.0-1608219886.ec0152d",
"nuxt": "^2.14.7",

Nuxt configuration

  • [x] universal

Nuxt configuration

  auth: {
    localStorage: false,
    strategies: {
      local: {
        endpoints: {
          refresh: {
            url: '/refresh-token',
            method: 'get',
            propertyName: 'jwt',
          },
          login: { url: 'login', method: 'post', propertyName: 'token' },
          user: { url: 'users/profile', method: 'get', propertyName: false },
          logout: false,
        },
        user: {
          property: 'user',
          autoFetch: true,
        },
        token: {
          property: 'token',
          type: 'Bearer',
          required: true,
        },
      },
    },
    redirect: {
      login: '/entrar',
      logout: '/entrar',
      home: '/templates',
    },
  },

What is expected?

To redirect to /templates page after login

What is actually happening?

It stores all cookies auth._token.local, auth._token_expiration.local and auth.strategy, but it's not redirecting to /template (home)

And even more, if i try to enter a page that needs auth, it redirects me to login page

I also tried to store in localstorage, but still doesn't work

Also after login request, it calls users request just after success

iat is 1 hour

bug

Most helpful comment

@CavalcanteLeo Thank you! :)

I'll add "improve error handling" to to-do list

All 7 comments

Hi @CavalcanteLeo , I tried to reproduce your issue using auth demo, but for me it's working properly. Maybe you can try to make a repro in codesandbox?

But I see that in your config you're using some deprecated options, like propertyName. Also, if you want to use refresh token, you should change to refresh scheme.

  auth: {
    localStorage: false,
    strategies: {
      local: {
        scheme: 'refresh', // <- set refresh scheme
        endpoints: {
          refresh: {
            url: '/refresh-token',
            method: 'post' // <- probably should be post, like login
          },
          login: { url: 'login', method: 'post' },
          user: { url: 'users/profile', method: 'get' },
          logout: false,
        },
        user: {
          property: 'user', // <- should be 'user' or false?
          autoFetch: true,
        },
        token: {
          property: 'token',
          type: 'Bearer',
          required: true,
        },
        refreshToken: {
          property: 'jwt' // <- refresh token property
        }
      },
    },
    redirect: {
      login: '/entrar',
      logout: '/entrar',
      home: '/templates',
    },
  },

You can see more about refresh scheme in docs

Also after login request, it calls users request just after success

Yes, this is expected. If you don't want to call user endpoint after login, then disable user.autoFetch and manually set the user using this.$auth.setUser. See docs of autoFetch and setUser

@CavalcanteLeo Maybe it's a typo issue? Just checking your config again and the endpoints.user.propertyName is false while user.property is user.

User data not set properly might cause the loggedIn state to be false, so no redirect.

I tried the the code above, but still not working,

And my refresh token is GET, not a POST, i just have to call the API sending my token that is about to expire in the header, nothing more

@JoaoPedroAS51

You can try debug the check method of the scheme you're using. If check return valid: false, then loggedIn state will be false

local scheme check and refresh scheme check

After a video call with @JoaoPedroAS51 we figured out the problem was the way I set the user config in nuxt.config

But if the nuxt-auth have thrown me an error, would be much easier to figure out.
My suggestion is to throw erros back to all requests, then we can figure out much faster.

My error was because the property had to be set as false, and when nuxt-auth made the request, it cound't parse and the state loggedIn was always false.

also nuxt-auth should handle when it occurs, i couldn't parse profile API, my states was loggedIn false but all cookies were set

I really appreciate your time and help @JoaoPedroAS51

@CavalcanteLeo Thank you! :)

I'll add "improve error handling" to to-do list

@JoaoPedroAS51 Right now I am having the same problem but if it establishes the session correctly but does not perform the redirection after a successful login or even when I am inside the session and I press the button that makes logout it does not redirect,

In the console I get the following error when I give F5 to redirect through the auth middleware

Could you give me a hint of what is happening? It seems that I will also have some problem with the configuration

Thanks for everything!
imagen

nuxt.config.js

  auth: {
    redirect: {
      login: '/login',
      logout: '/login',
      home: '/',
    },
    strategies: {
      local: {
        scheme: 'refresh',
        token: {
          property: 'tokens.accessToken',
          type: 'Bearer'
        },
        refreshToken: {
          property: 'tokens.refreshToken',
          data: 'refresh_token'
        },
        user:{
          property: 'user',
          autoFetch: true
        },
        endpoints: {
          login: {
            url: 'auth/login',
            method: 'POST'
          },
          user: {
            url: 'auth/me',
            method: 'GET',
          },
          logout: {
            url: 'auth/logout',
            method: 'POST'
          },
          refresh: {
            url: 'auth/refresh',
            method: 'POST'
          }
        },
      }
    }
  }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

javialon26 picture javialon26  路  3Comments

sebmor picture sebmor  路  3Comments

pi0 picture pi0  路  3Comments

manniL picture manniL  路  4Comments

essamamdani picture essamamdani  路  3Comments