【问题标题】:nuxt axios onError unathenticated logout then redirectnuxt axios onError 未经身份验证的注销然后重定向
【发布时间】:2020-05-16 07:21:45
【问题描述】:

我在 laravel 中的后端,我设置如果用户登录到另一台设备,则当前设备身份验证令牌无效

如果他在 axios 中收到未经验证的错误 401,我也想立即注销并重定向到登录页面

我就是这样的

export default function({ $axios, redirect, $auth }) {
  $axios.onError(error => {
    if (error.response.status === 401) {
      if ($auth.loggedIn) {
        $auth.logout();
      }
      redirect("/login");
    }
  });
}

但是什么也没发生,当我检查console.log($auth) 时,它得到了undefine

帮帮我谢谢

【问题讨论】:

    标签: axios nuxt.js


    【解决方案1】:

    如果你需要从 $axios 插件访问 $auth,你可以使用 auth.plugins 选项。

    https://auth.nuxtjs.org/recipes/extend.html

    {
      modules: [
        '@nuxtjs/auth'
      ],
      auth: {
         plugins: [ { src: '~/plugins/axios', ssr: true }, '~/plugins/auth.js' ]
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-06-30
      • 1970-01-01
      • 2020-02-22
      • 1970-01-01
      • 1970-01-01
      • 2021-04-01
      • 2016-08-08
      • 1970-01-01
      • 2015-05-25
      相关资源
      最近更新 更多