Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vue.js Axios在Nuxt应用程序的Cookie中未看到令牌_Vue.js_Authentication_Jwt_Nuxt.js - Fatal编程技术网

Vue.js Axios在Nuxt应用程序的Cookie中未看到令牌

Vue.js Axios在Nuxt应用程序的Cookie中未看到令牌,vue.js,authentication,jwt,nuxt.js,Vue.js,Authentication,Jwt,Nuxt.js,我正在尝试为我的应用程序实现nuxt身份验证;我正在使用Nuxt universal storage跨应用程序处理我的存储。我的问题是,一切正常,但当我第一次登录时,我将令牌保存到cookies中,但我随后进行的任何API调用都返回401错误,但我立即刷新浏览器,它就开始正常工作 我的登录组件 this.loading = true this.$axios .post(LOGIN, this.user) .then(({ data }) =

我正在尝试为我的应用程序实现nuxt身份验证;我正在使用Nuxt universal storage跨应用程序处理我的存储。我的问题是,一切正常,但当我第一次登录时,我将令牌保存到cookies中,但我随后进行的任何API调用都返回401错误,但我立即刷新浏览器,它就开始正常工作

我的登录组件

      this.loading = true
      this.$axios
        .post(LOGIN, this.user)
        .then(({ data }) => {
          this.loading = false
          console.log(data)
          if (data.data.user.two_factor_auth.status) {
            this.$router.push('/otp')
          } else {
            this.$storage.setUniversal('user', data.data.user)
            this.$storage.setUniversal('token', data.data.access_token)
            this.showToast({
              display: true,
              description: `Welcome ${data.data.user.firstname}!`,
            })
            this.$router.replace('/')
          }
        })
        .catch((err) => {
          this.loading = false
          this.showModal({
            icon: 'error',
            title: 'Error',
            description: err.response.data.message,
          })
          this.displayModal = true
          console.log(err)
        })
}
在axios插件中

export default function ({ $axios, redirect, $storage, store }) {
  console.log(store)
  const token = $storage.getCookie('token')
  // $axios.setToken(token, 'Bearer')
  console.log(token)
  if (token) {
    $axios.onRequest((config) => {
      config.headers.common['Authorization'] = `Bearer ${token}`
    })
  }

  // set the token for all requests
  // for every 401 error, route the user to the login page
  $axios.onError((error) => {
    if (error.response.status === 401) {
      //   $storage.removeUniversal('user')
      //   $storage.removeCookie('token')
      store.dispatch('toast/showToast', {
        icon: 'error',
        description: "You're not authorized",
      })
      redirect('/login')
    }
  })
}
我的中间件

import {
  routeOption,
  getMatchedComponents,
  normalizePath,
} from '~/utils/auth.js'

export default function ({ $storage, route, redirect }) {
  //   console.log($storage)
  // Disable middleware if options: { auth: false } is set on the route
  if (routeOption(route, 'auth', false)) {
    return
  }

  // Disable middleware if no route was matched to allow 404/error page
  const matches = []
  const Components = getMatchedComponents(route, matches)
  if (!Components.length) {
    return
  }

  // const { login, callback } = $auth.options.redirect

  const token = $storage.getCookie('token')

  if (token) {
    console.log('token dey')
    // -- Authorized --
    // // Redirect to home page if inside login page (or login page disabled)
    if (!'/login' || normalizePath(route.path) === normalizePath('/login')) {
      redirect('/')
    }

    // redirect('/')
  } else {
    // -- Guest --
    // Redirect to login page if not authorized and not inside callback page
    // (Those passing `callback` at runtime need to mark their callback component
    // with `auth: false` to avoid an unnecessary redirect from callback to login)
    if (!'/login' || normalizePath(route.path) !== normalizePath('/login')) {
      redirect('/login')
    }

    // redirect('/login')
  }
}

我现在真的很想得到任何帮助