我创建了一个vue和Laravel使用Sanctum,发送请求使用axios,但我得到状态204,当我试图获取用户信息,我得到了Unauthenticated,我不知道什么是错的!!
这是我的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>发布于 2020-07-07 21:17:32
解决了!通过编辑提供者/RouteServiceProvider.php,将mapApiRoutes函数从中间件(‘api’)改为中间件(‘web’)
protected function mapApiRoutes()
{
Route::prefix('api')
->middleware('web')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}https://stackoverflow.com/questions/62733796
复制相似问题