Search code examples
laravellaravel-passportlaravel-api

Laravel Passport How to show custom error message when invalid token is supplied


I am trying to create an API with Laravel passport, and I have to protect some routes so that only authenticated users can access these routes.

Route::post('register', [ApiAuthController::class, 'register']);
Route::post('login', [ApiAuthController::class, 'login']);
Route::middleware('auth:api')->group(function () {
    Route::post('/task', [ApiControllerTaskController::class,'store']);
});

Now hereafter login, I got an access token, and I used this access token like this. enter image description here

Everything is working fine here, but I want to show an invalid token error message when I removed the bearer token. Instead, I got this. enter image description here


Solution

  • Make sure you added the Content-type : application/json in your headers in Postman before sending your request