Reputation: 113
I created a vue and Laravel Spa using Sanctum , sent requests using axios but i get status 204 and when i tried to get user info i got Unauthenticated , i dont not know what is wrong !!
This is my 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:'[email protected]',
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>
Upvotes: 0
Views: 666
Reputation: 113
SOLVED!!! By editing Providers/RouteServiceProvider.php, change mapApiRoutes function from middleware(['api']) to middleware(['web'])
protected function mapApiRoutes()
{
Route::prefix('api')
->middleware('web')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
Upvotes: 1