我使用Sanctum创建了一个vue和Laravel Spa,使用axios发送了请求,但是我的状态为204,当我尝试获取用户信息时,我得到了未经身份验证,我不知道这是怎么回事!
这是我的api.php
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
home.vue
<script>
import axios from "axios";
axios.defaults.withCredentials = true;
axios.defaults.baseURL = "http://localhost:8000/";
export default {
name: "Home",
methods: {
login() {
axios.get("/sanctum/csrf-cookie").then((response) => {
axios.post('/login',{
email:'user@user.com',
password:'123456789'
})
.then(response=>{
console.log(response)
})
});
},
},
};
</script>
dashboard.vue
<script>
import axios from "axios";
axios.defaults.withCredentials = true;
axios.defaults.baseURL = "http://localhost:8000/";
export default {
mounted(){
axios.get("/api/user").then((response)=>{
console.log(response);
})
}
}
</script>
答案 0 :(得分:0)
已解决!!! 通过编辑Providers / RouteServiceProvider.php,将 mapApiRoutes 函数从中间件(['api'])更改为中间件(['web'])
protected function mapApiRoutes()
{
Route::prefix('api')
->middleware('web')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}