【问题标题】:How/Where does nuxtjs set the authorization headernuxtjs 如何/在哪里设置授权标头
【发布时间】:2021-10-10 23:24:50
【问题描述】:

我从另一位离开公司的开发人员那里继承了一个 Web 项目。它是用 nuxtjs 和 laravel 构建的,我都不擅长。

在 web 项目中,在用户进入登录屏幕,输入电子邮件和密码,然后按保存后,我可以在我的开发人员工具中看到 nuxtjs 框架在 @987654324 向 laravel api 发送了一个 {email: "user@email.com", password: "password"} @。然后,laravel api 会返回一个 200 响应,并带有此有效负载 { data: { message: "Login successful.", token: "50|Fs88RPU7HON1LnBskm35O9txG0OnXDiG3x4XWILJ" }}。到目前为止一切都很好!

在上面的 200 响应之后,NuxtJS 立即向https://example.project.com/api/auth/user 发送另一个请求,以尝试获取有关此用户的信息。 Nginx 给出 401 响应。我首先怀疑为什么会发生这种情况是因为 NuxtJS 发送了一个Authorization: Bearer undefined,如我的浏览器开发工具的截图所示

我见过Authorization: Bearer [object object] 的其他情况。

我想知道 NuxtJS 如何决定为Authorization http 标头提供什么值?这是我当前的nuxt.config.js

export default {
  // Global page headers: https://go.nuxtjs.dev/config-head
  head: {
    title: 'Blah',
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1' },
      { hid: 'description', name: 'description', content: '' },
    ],
    link: [{ rel: 'icon', type: 'image/x-icon', href: '/favicon.png' }],
  },
  // Auto import components: https://go.nuxtjs.dev/config-components
  components: true,

  // Modules: https://go.nuxtjs.dev/config-modules
  modules: [
    // https://go.nuxtjs.dev/axios
    '@nuxtjs/axios',
    '@nuxtjs/auth'
  ],

  auth: {
    redirect: {
      login: '/login',
      logout: '/',
      callback: '/api/login',
      home: '/'
    },
    strategies: {
      local: {
        endpoints: {
          login: { url: '/api/login'},
          user: { url: '/api/auth/user'}
        },
      }
    },
    localStorage: true
  },
  proxy: {
    '/api': {
      target: 'https://example.project.com/',
      pathRewrite: { '^/api': '' },
    },
  },


  // Axios module configuration: https://go.nuxtjs.dev/config-axios
  axios: {
    baseURL: 'https://example.project.com/',
    credentials: true,
    headers : {
      common: {
        'Accept' : 'application/json'
      }
    }
  },

  // Build Configuration: https://go.nuxtjs.dev/config-build
  build: {
  }
}

另外,这是pages/login.vue 中启动登录过程的代码行:

await this.$auth.loginWith('local', { data: {email:"user@email.com",password:"password"} });

【问题讨论】:

    标签: laravel nuxt.js laravel-sanctum nuxt-auth


    【解决方案1】:

    如下更改您的策略以设置作为响应返回的令牌的属性名称。

    strategies: {
      local: {
        token: {
          property: 'token'
        },
        endpoints: {
          login: { url: '/api/login'},
          user: { url: '/api/auth/user'}
        },
      }
    },
    

    它将在您使用$axios 的所有请求中包含授权标头。

    您可能还需要设置从后端返回的 用户 的属性。

    【讨论】:

    • 我注意到我的问题中有一个错误,并更新为 /api/login 返回 {data: {message: "...etc...", token: "50|Fs88RPU7HON1LnBskm35O9txG0OnXDiG3x4XWILJ"}} 。所以我改变了我的策略来拥有 strategies.local.token.property == "data.token"data.token 是指定嵌套属性名称的正确方法吗?因为现在这会导致 nuxt/axios 发送Authorization: Bearer [object Object]
    • 实际上,解决方案是使用您发布的strategies,但只需将login: 改为login: { url: '/api/login', propertyName: 'data.token'}
    【解决方案2】:

    只需在授权请求后添加Authorization 标头作为默认标头即可。假设服务器发送访问令牌作为响应,代码可能如下所示:

    const response = await this.$auth.loginWith('local', { 
      data: {
        email: "user@email.com",
        password:"password"
      } 
    })
    const { token } = response;
    axios.defaults.headers.common = { Authorization: `Bearer ${token}` };
    

    【讨论】:

    • 我更新了我的问题以显示代码使用 await this.$auth.loginWith('local', { data: {email:"user@email.com",password:"password"} }); 。因此,nuxtjs 在幕后捕获了令牌。我不知道如何处理 nuxtjs 背后的魔力
    • 我检查了文档,loginWith 返回服务器的响应。我更新了答案。
    猜你喜欢
    • 2019-09-23
    • 1970-01-01
    • 1970-01-01
    • 2019-09-28
    • 1970-01-01
    • 2021-11-21
    • 2015-12-02
    • 2017-10-31
    • 2017-04-10
    相关资源
    最近更新 更多