Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mohamadAbdelhady's avatar

I am getting unauthenticated error on my user request in laravel sanctum

I’m building a React app with a Laravel backend (Sanctum).

In my React app, I have a Context for user authentication that makes a request to /api/user to fetch the authenticated user.

Before calling /api/user, I make sure to request /sanctum/csrf-cookie, and I see both the XSRF-TOKEN and laravel_session cookies in the browser.

Despite that, the /api/user request returns Unauthenticated.

Why would this happen even though both the CSRF token and session cookie are present? authe context

axios file

import axios from 'axios'

const api = axios.create({
  baseURL: 'http://localhost:8000',
  withCredentials: true,
})

export default api

api route file

<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

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

Route::post('/register', [App\Http\Controllers\AuthenticationController::class, 'register']);
Route::post('/login', [App\Http\Controllers\AuthenticationController::class, 'login']);
Route::post('/logout', [App\Http\Controllers\AuthenticationController::class, 'logout'])->middleware('auth:sanctum');

env file

SANCTUM_STATEFUL_DOMAINS=localhost:5173
SESSION_DOMAIN=localhost

APP_FRONTEND_URL=http://localhost:5173
1 like
1 reply

Please or to participate in this conversation.