【发布时间】:2021-08-31 10:58:07
【问题描述】:
您好,我在 react 和 laravel sanctum 中遇到问题,在 postman 上测试时运行良好,但在 react 中实现时显示错误 401 Unauthorized
我把它放在我的 .env laravel 文件中
SANCTUM_STATEFUL_DOMAINS=localhost:3000 SESSION_DRIVER=cookie SESSION_DOMAIN=localhost
这是我的有状态域
'stateful' => explode(',', env(
'SANCTUM_STATEFUL_DOMAINS',
'localhost,localhost:3000,127.0.0.1,127.0.0.1:8000,::1,' . parse_url(env('APP_URL'), PHP_URL_HOST),
)),
这是我的反应前端
export const postLogout = (token) => (dispatch) => {
const config = {
headers: { Authorization: "Bearer " + token },
};
console.log(config);
axios.defaults.withCredentials = true;
axios.get("http://localhost:8000/sanctum/csrf-cookie").then((response) => {
axios
.post(`${Api}/logout`, config)
.then((res) => {
console.log(res);
dispatch({
type: POST_LOGOUT,
payload: res,
});
})
.catch((err) => {
console.log(err);
});
});
};
【问题讨论】:
标签: reactjs laravel http-status-code-401 unauthorized laravel-sanctum