Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Auth::user() is null in a Laravel route even though the user logged in

ErrorAuth::user() returns null

Short 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.

  • Applies to Laravel 12, Sanctum 4, PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check which middleware and guard the failing route uses.

    php artisan route:list
  2. 2Match what you see, then jump to its fix

    1. 1Route 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. 2The route is in routes/api.php and the client sends no Authorization headerAn API route is called without a tokenFix
    3. 3Login succeeds but the next request from the frontend has no userA separate SPA is not treated as statefulFix

What does the error look like?

  • Auth::user() returns null
  • Auth::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:list

2Fix

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/profile

Confirmed by laravel.com

Did this fix it?

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/user

2Fix

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/user

3Verify

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/user

Confirmed by laravel.com

Did this fix it?

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 .env

2Fix

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.php

Confirmed by laravel.com

Did this fix it?

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 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 fix

Back 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.