Search code examples
laravellaravel-passport

Change Json response in passport


Can you please tell me how to change JSON response to request with wrong api token.

I mean instead

{
    "message": "Unauthenticated."
}

I want to do this way

{
    "error": true,
    "message": "Unauthenticated.",

}

Where should i change this ? Thank you


Solution

  • In your project's app/Exceptions/Handler.php file there is a function called render(). Inside that function add the following code block:

    if($exception instanceof \Illuminate\Auth\AuthenticationException){
      return response()->json(['error' => true, 'message' => 'Unauthenticated.'], 401);
    }