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

stesvis's avatar

Api returns "200 OK" instead of "401 Unauthorized"?

Hello,

I am using Passport to protect my APIs, and when I am logged in it's all good, but when I am logged out my API still returns a 200 code instead of 401. The response is the HTML content of my web login page.

How come??

0 likes
4 replies
fylzero's avatar

@stesvis What do you mean "logged out"? your API should be accepting a Bearer token... it doesn't rely on you being logged in to the site.

If this is for SPA, please specify.

1 like
mariohbrino's avatar

Are you using routes/web.php or routes/api.php? Can you please show your route?

stesvis's avatar
stesvis
OP
Best Answer
Level 1

This is the route @mariohbrino :

Route::resource('vehicles', 'Api\VehiclesApiController')->middleware('auth:api');

And I use it from a mobile app.

I think I managed to resolve it tho, by passing this header: Accept: application/json

With it, if i try to consume the endpoint without a token it returns 401 correctly. Without it, it would return the html of my login page (where the Authenticate middleware redirects me).

2 likes

Please or to participate in this conversation.