'laravel sanctum - vuejs, 401 (Unauthorized) in live server while trying to get user info

I have developed this in laravel vuejs, used laravel sanctum for authentication. The app works fine in localhost, But getting this error while I did this in the live domain. I am sharing the error where I was trying to get user info :

enter image description here

The screenshot of header :

enter image description here

I am sharing the .env:

SESSION_DRIVER=cookie
SESSION_LIFETIME=120
SESSION_DOMAIN=localhost
SESSION_SECURE_COOKIE=false
SANCTUM_STATEFUL_DOMAIN=localhost,localhost:8080

From sanctum.php

'stateful' => explode(',', env('SANCTUM_STATEFUL_DOMAINS', 
    'localhost,localhost:8080')),

From kernel :

'api' => [
        \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
        'throttle:api',
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],

from bootstrap.js

axios.defaults.withCredentials = true;

And from component :

created() {
    axios.defaults.headers.common['Authorization'] = `Bearer ${this.token}`
    axios.get('/api/user').then(response => {
       localStorage.setItem('userInfo', JSON.stringify(response.data))
       let getUser = localStorage.getItem('userInfo')
       this.currentUser = JSON.parse(getUser)
    }).catch(errors => {
        console.log(errors)
    })
}


Solution 1:[1]

Your POST request to /api/user-login is returning a server error(500). Maybe that's why its not returning the auth token or cookie

You need to fix the server error first

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ronodip Basak