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

emfpc's avatar
Level 1

Auth::user() in api route

When using controllers i can run if statement in the function without using a middle ware to determine if a user has a role or is log in; example: if(Auth::user()->role == 'admin'){ return 'I'm Admin'; }else{ return 'i'm not admin; } something like that. But i wan't to use the same when using api routes; i understand there's a few stuff to be considered; is there away to use this simple logic for it?

0 likes
4 replies
emfpc's avatar
Level 1

@RICARDO - tried to use auth('api')->user();. It won't show the user information

Snapey's avatar

Well here's the thing...

API routes are stateless. How do you expect it to remember who you are?

Are you using passport, or did you think it was the thing to send your ajax requests from a browser to the API routes? No its not. Put those requests in web.php so that it can benefit from cookie authentication.

Please or to participate in this conversation.