nuxt auth calls user before login and gives 401

Viewed 777

I created a NUXT site with the NUXT AUTH and it works fine, except when I enter the first page on the web app. The web app tries to call the user endpoint but since I haven't logged in yet this endpoint naturally returns

Failed to load resource: the server responded with a status of 401 (Unauthorized)

My nuxt auth configuration in nuxt.config.js looks like this

    auth: {
      strategies: {
        local: {
          endpoints: {
            login: {
              url: '/token',
              method: 'post',
              propertyName: 'access_token'
            },
            logout: { url: '/api/account/logout', method: 'post' },
            user: {
              url: '/api/account/getCurrentUser',
              method: 'get',
              propertyName: false
            }
          },
          tokenType: 'bearer',
          autoFetchUser: true
        }
      },
      redirect: {
        home: '/',
        callback: false,
        logout: '/',
        login: '/login'
      }
    }

Can anybody help me get rid of the 401 error? I just want the user endpoint to be called after login

1 Answers

I think there is one reason and two scenarios.

  1. You forgot to set auth: false on the homepage which does not require login and it is trying to get the user with expired token.

  2. If the homepage should be behind login, then again the token is probably experied and in this case I suggest to handle all 401 globally with logout.

Related