DEV Community

Discussion on: Authenticating a Vue SPA is easy with Laravel Sanctum

Collapse
 
samora92 profile image
samora92

I did all these steps, but when i am trying to get user data i get Unauthenticated error
routes/api.php

Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
    return $request->user();
});
Enter fullscreen mode Exit fullscreen mode

home.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>
Enter fullscreen mode Exit fullscreen mode

I do not know if i did any thing wrong ?

Collapse
 
maxeze profile image
Maximiliano Basoalto

Happening the same to me :(

Collapse
 
samora92 profile image
samora92 • Edited

I solved it , in your Providers/RouteServiceProvider.php, change the 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'));
    }
Enter fullscreen mode Exit fullscreen mode

Hope this helping you.

Thread Thread
 
harmlezprinze profile image
Olami Lekan

Great work, please i will like to have a little chat with u man

Thread Thread
 
aymenalhattami profile image
Ayman Alhattami

This is not a good solution,

Thread Thread
 
aymenalhattami profile image
Ayman Alhattami

The solution is to set value for

SANCTUM_STATEFUL_DOMAINS='127.0.0.1:3000'
Enter fullscreen mode Exit fullscreen mode

in the .env file