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

pickab00's avatar

Laravel Sanctum API Login

So I have finally decided to use Laravel 8 with fortify/sanctum. But I am a bit confused as to why the default API route is redirecting me to the login page instead of showing me a message like "unauthenticated"

I have a custom login/register controller which simply registers or logs in the user and creates a new token using the $user->createToken('user-token'). But now when I try to go to this route:

Route::middleware('auth:sanctum')->get('index', [\App\Http\Controllers\TestController::class, 'index']);

in postman, it shows me the login page. How do I show the unauthenticated message? What am I doing wrong? By default, arent api routes supposed to authenticate with tokens and show unauthenticated if failed?

0 likes
2 replies
Sergiu17's avatar

I'm no sure, but, try to add a Header to your request: Accept: application/json

pickab00's avatar
pickab00
OP
Best Answer
Level 4

I actually figured out what the issue was. In the Authenticate middleware, we have a method which redirects the user to login if attempt fails. So I just changed the redirect method or override it somewhere else

2 likes

Please or to participate in this conversation.