How to properly return data with laravel sanctum?

Viewed 63

I'm trying to add authentication to my api using laravel sanctum. My test route is:

Route::middleware('auth:sanctum')->get('/test', function () { return 'API OK'; });

This return "API OK" when authenticated. That's ok, but also adds additional information in html format. I only need the response. What am I missing here? This is a succesful case: enter image description here

When token is invalid I'm rediredted to the login page. I just need the 401 error: enter image description here enter image description here

1 Answers

For the second part of your problem with an invalid token this answer helped me to override the sanctum response Customize laravel sanctum unauthorize response

The first part of your question when testing

Route::middleware('auth:sanctum')->get('/test', function () { return 'API OK'; });

I get API OK as the response, perhaps your config/sanctum.php or config/auth.php are misconfigured

Related