Laravel Fortify SPA POST /注销 419 未知状态

问题描述 投票:0回答:1

我很难处理

sanctum
fortify
,我使用
sanctum
fortify
进行
Vue SPA
身份验证,当我登录时它工作正常,当做
POST /sanctum/csrf-cookie
时,cookie 是在客户端设置,然后我在执行
POST /login
验证登录时抓取此 cookie 并与请求标头一起发送,但这是迄今为止唯一有效的方法,当我向
POST /logout
发送请求时,它显示
419 unknown status csrf-token mismatch 
,即使抓取 cookie 并再次使用请求标头发送它,如果我将请求发送到
GET api/users/auth
,它会显示
401 unauthorized
和响应
unauthenticated
,我已经完成了所有配置以使其正常工作,例如配置
cors.php
并设置
paths
,设置
.env
变量并为
fortify.php
配置
fortifyServiceProvider.php
SPA authentication

Login

function getCookie(name) {
  const value = document.cookie
  const parts = value.split(name)
  if (parts.length === 2) {
    return parts.pop().split(';').shift()
  }
}

const login = async () => {
  error.value = null
  const payload = {
    email: email.value,
    password: password.value
  }

  axios.get('http://localhost:80/sanctum/csrf-cookie').then(() => {
    const csrfToken = getCookie('XSRF-TOKEN=')

    axios
      .post('http://localhost:80/login', payload, {
        headers: {
          'X-XSRF-TOKEN': decodeURIComponent(csrfToken)
        },
        withCredentials: true
      })
      .then((response) => {
        if (response.data.error) {
          error.value = response.data.error
        } else {
          router.push('/')
        }
      })
  })
}

Logout

const logout = async () => {
  const csrfToken = getCookie('XSRF-TOKEN')
  axios
    .post('http://localhost:80/logout', {
      headers: {
        'X-XSRF-TOKEN': decodeURIComponent(csrfToken)
      },
      withCredentials: true
    })
    .then(router.push('/login'))
}
laravel vue.js single-page-application fortify laravel-sanctum
1个回答
0
投票

这可能是您获取 XSRF-TOKEN cookie 的方式导致的。 在您的登录获取请求中,您会得到这样的 cookie:

const csrfToken = getCookie('XSRF-TOKEN=')

但是在注销函数中,您可以通过这行代码获取 cookie:

const csrfToken = getCookie('XSRF-TOKEN')

我想您可能首先需要检查您的身份验证 API,然后回顾您的登录和注销方法,以检查身份验证过程中是否存在不一致之处。 祝你好运。

© www.soinside.com 2019 - 2024. All rights reserved.