简体   繁体   English

当其中一个 API 响应 Unauthorized 401 (Nuxt-Auth) 时如何注销用户

[英]How logout user when one of APIs responds Unauthorized 401 (Nuxt-Auth)

How logout user when one of APIs responds Unauthorized 401 with Nuxt-Auth?当其中一个 API 使用 Nuxt-Auth 响应未经授权的 401 时,如何注销用户? I use AXIOS & built-in functions of Nuxt-Auth for requests我使用 AXIOS 和 Nuxt-Auth 的内置函数来处理请求

My nuxt-config settings for nuxt-auth:我的 nuxt-auth 的 nuxt-config 设置:

 auth: { redirect: { login: '/login', logout: '/', callback: '/login', home: '/panel/dashboard' }, strategies: { local: { token: { property: 'token', global: true, type: 'Bearer', required: true, }, user: { property: 'user', autoFetch: true }, endpoints: { login: {url: '/api/auth/login_verify', method: 'post'}, logout: {url: '/api/auth/logout', method: 'get'}, user: {url: '/api/auth/validate', method: 'get'}, }, }, } },

Since you're using Axios, it'll be easy to use Interceptors and catch the error according to your requirement and then call the logout action由于您使用的是 Axios,因此很容易使用拦截器并根据您的要求捕获错误,然后调用注销操作

Something like below with a plugin created in src/plugins/axios.js will work像下面这样在src/plugins/axios.js中创建的插件将起作用

export default function ({ $axios }) {
  $axios.onError((error) => {
    if (error.response.status === 401) {
      CALL_LOGOUT_ACTION_HERE
    }
  })
}

pass context instead of $axios,...传递上下文而不是 $axios,...

export default function ( context) {
context.$axios.onError((error) => {
    if (error.response.status === 401) {
    context.$auth.logout()
    }
})

} }

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

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