Auth::user() is null in a Laravel route even though the user logged in
Auth::user() returns nullShort answer
Auth::user() returns null when the request is not authenticated by the guard you read from. Common reasons: the route has no auth middleware, an API route is called without a Bearer token, or a separate SPA is not set up for Sanctum's stateful cookies.
Fix it fast
1Find out which cause you have
Check which middleware and guard the failing route uses.
php artisan route:list2Match what you see, then jump to its fix
- 1
Route list shows no auth middleware and the page works only when you visit it after logging inThe route is not protected by the auth middlewareFix - 2
The route is in routes/api.php and the client sends no Authorization headerAn API route is called without a tokenFix - 3
Login succeeds but the next request from the frontend has no userA separate SPA is not treated as statefulFix
- 1
What does the error look like?
Auth::user() returns nullAuth::check() returns false right after login
Cause 1 of 3
The route is not protected by the auth middleware
1Check
Laravel gives you the user through a guard, and the auth middleware is what turns an unauthenticated request into a redirect. On a route without it the code runs with a null user.
php artisan route:list2Fix
Add the auth middleware to the route or group.
Route::get('/profile', [ProfileController::class, 'show'])->middleware('auth');3Verify
Visit the route logged out (you are redirected to login) and logged in (the user is set).
curl -I https://example.com/profileConfirmed by laravel.com
Cause 2 of 3
An API route is called without a token
1Check
Token requests must send the token in the Authorization header as a Bearer token. Sanctum looks for a cookie first and then for this header.
curl -i -H "Accept: application/json" https://api.example.com/api/user2Fix
Protect the route with the sanctum guard and send the token.
Route::get('/user', fn (Request $request) => $request->user())
->middleware('auth:sanctum');
# client
curl -H "Authorization: Bearer YOUR_TOKEN" -H "Accept: application/json" https://api.example.com/api/user3Verify
The response is the user JSON instead of null or a 401.
curl -H "Authorization: Bearer YOUR_TOKEN" -H "Accept: application/json" https://api.example.com/api/userConfirmed by laravel.com
Cause 3 of 3
A separate SPA is not treated as stateful
1Check
Sanctum uses cookies only for domains in the stateful setting. The SPA and API must share the top-level domain, send an Accept: application/json header and either Referer or Origin, and call /sanctum/csrf-cookie first.
grep -n -E "stateful|SANCTUM" config/sanctum.php .env2Fix
Add the SPA domain (with port if any) to the stateful list, call statefulApi() in bootstrap/app.php, and send credentials from the client.
// bootstrap/app.php
->withMiddleware(function (Middleware $middleware): void {
$middleware->statefulApi();
})
// client (axios)
axios.defaults.withCredentials = true;
axios.defaults.withXSRFToken = true;3Verify
After the csrf-cookie call and login, request /api/user from the SPA and get the user.
grep -n -E "supports_credentials|domain" config/cors.php config/session.phpConfirmed by laravel.com
How do I stop it coming back?
- Put every private route inside an auth or auth:sanctum group.
- Use $request->user() in controllers so the guard in use is explicit.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Composer says the gd extension is missing on a Laravel project
composer install or composer update stops on a package that requires ext-gd
Composer refuses to install a package that requires ext-gd when the PHP binary running Composer has no GD extension.
4 causes, each with a fixBack end
composer require maatwebsite/excel fails to install
Composer says the package needs an older PHP than the one installed
Composer refuses maatwebsite/excel when the PHP version or a PHP extension does not match what the package and PhpSpreadsheet need.
3 causes, each with a fixBack end
composer install says the lock file is not compatible and asks for update
Your lock file does not contain a compatible set of packages. Please run composer update.
composer install checked the locked packages against this machine's PHP and extensions and found a conflict.
3 causes, each with a fixBack end
composer require laravel/horizon fails: ext-pcntl is missing
laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system
Horizon needs the pcntl extension, and Composer refuses to install it where PHP lacks that extension.
3 causes, each with a fixBack end
composer require laravel/passport stops with 'requirements could not be resolved'
... but these were not loaded, likely because it conflicts with another require.
Composer found no Passport release that fits your project at once.
4 causes, each with a fixBack end
Composer says the root composer.json requires a PHP version your PHP does not satisfy
Your requirements could not be resolved to an installable set of packages.
Composer compares the php constraint in your composer.json, or in a package, with the PHP that runs Composer.
3 causes, each with a fix