首页 > 解决方案 > Set-Cookie 未在我的浏览器中设置 cookie (React + Sanctum)

问题描述

我正在使用 Laravel sanctum 在 cors.php 中使用 ReactJS 进行身份验证。我已经设置了这些值

'paths' => ['api/*', 'api/csrf-cookie','/login'],
'supports_credentials' => true,

圣所.php

'prefix'=>'api'

在 React 中,我将 axios 与凭证一起使用

axios.get('http://www.react.test/api/csrf-cookie').then(response => {
        console.log(response);
 }, { withCredentials: true });

使用 Postman 时,我得到了 Set-Cookie Value ,但是当我尝试使用浏览器时,它没有在我的浏览器中的 Application->cookies 选项卡下设置

http://www.react.test/
http://localhost:3000/

标签: reactjslaravelcsrflaravel-sanctum

解决方案


您使用的axios包错误,withCredentials应该作为 a config optionstoaxios而不是 a then

将您的代码更改为此

axios.get('http://www.react.test/api/csrf-cookie',{ withCredentials: true }) // FIX
.then(response => {
    console.log(response);
}).catch((err)=>{
   console.log(err);
});

这现在可以正常工作

更多请参考axios


推荐阅读