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

ulfie22's avatar

Auth::user() returns null after Passport API authentication

I have Laravel instance that I am trying to upgrade to 5.7 (and Passport 7.0, from 5.6 and 5.0 respectively). The app exists solely to serve data through an API. I use Passport for user authentication - everything worked in 5.6/5.0. The error I am now getting on some API calls is that Auth::user(); return null - so any method on what should be the authenticated user crashes. This happens AFTER the route is authenticated. I do not see how this can happen.

In my code, I try to get the authenticated user in the many ways Laravel allow. Only the ones that refer to the auth.api guard work:

$user = Auth::user(); // FAILS
$user = Auth::guard('api')->user(); // WORKS
$user = auth()->user(); // FAILS
$user = $request->user(); // FAILS
$user = auth()->guard('api')->user(); // WORKS

This creates a problem for testing, where I generally use WithoutMiddleware and actingAs - now all these tests fail because they do not go through the api guard.

This all worked with the prior versions.

Any thoughts on what is happening and how to fix it? Thank you!!

0 likes
4 replies
ulfie22's avatar

Thank you for your help. I've looked through there and I don't see anything that I can relate to the issue I am seeing. Is there something specific you see? Thanks!

fsmonter's avatar

Could be related to Cookie Serialization. Try adding protected static $serialize = true; on App\Http\Middleware\EncryptCookiess middleware.

ulfie22's avatar

Again, thank you. I do not think that is the issue as I am authenticating using Passport via an API which does not use cookies.

Please or to participate in this conversation.