简体   繁体   English

nuxt auth 在登录前调用用户并给出 401

[英]nuxt auth calls user before login and gives 401

I created a NUXT site with the NUXT AUTH and it works fine, except when I enter the first page on the web app.我使用 NUXT AUTH 创建了一个 NUXT 站点,它工作正常,除了当我进入 web 应用程序的第一页时。 The web app tries to call the user endpoint but since I haven't logged in yet this endpoint naturally returns web 应用程序尝试调用用户端点,但由于我尚未登录,因此该端点自然会返回

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

My nuxt auth configuration in nuxt.config.js looks like this我在nuxt.config.js中的 nuxt auth 配置看起来像这样

    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?谁能帮我摆脱401错误? I just want the user endpoint to be called after login我只想在登录后调用用户端点

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.您忘记在不需要登录的主页上设置auth: false并且它试图让用户使用过期的令牌。

  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.如果主页应该在登录后,那么令牌可能再次被使用,在这种情况下,我建议通过注销全局处理所有 401。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM