I am facing issues with user authentication using Laravel Sanctum. I have set up everything properly, with Vite and Vue 3 as the frontend. The problem arises when I attempt to login with Laravel's default auth - it works fine. However, when I make a request from the Vue frontend like this:
const AxiosInstance = axios.create({
headers: {
Accept: "application/json",
"Access-Control-Allow-Origin": "*",
"Content-Type": "application/json",
"X-Requested-With": "XMLHttpRequest",
},
baseURL: "http://127.0.0.1:8000/",
withCredentials: true,
});
The actual issue arises when I try to fetch the user data, it always returns Unauthenticated:
const user = async() => {
await AxiosInstance.get('api/user')
}
Even though the user is created directly from axios, it remains unauthenticated. I have checked if the user is logged in, and Laravel recognizes it with something like:
@guest
<h1>Welcome guest</h1>
@else
<h1>Welcome user</h1>
@endauth
But why is 'api/user' route not authenticated?