If you used laravel 5 passport then i am sure you require to use "auth:api" middleware, But you can see when you pass wrong api token then you just get "unauthenticated" json response with error key. If you want to change message or json formate then you can do it.
You have to on on default Exceptions handler file and you can change there. So just open your Handler.php file and change json response.
app/Exceptios/Handler.php
<?php
namespace App\Exceptions;
use Exception;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
class Handler extends ExceptionHandler
{
......
......
/**
* Convert an authentication exception into an unauthenticated response.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Auth\AuthenticationException $exception
* @return \Illuminate\Http\Response
*/
protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->expectsJson()) {
/** return response()->json(['error' => 'Unauthenticated.'], 401); */
$response = ['status' => 'error','message' => 'You pass invalid token'];
return response()->json($response);
}
return redirect()->guest('login');
}
}
You can do it Just.....
Do you like below Tutorials ?
- PHP Laravel 5.5 - import export data into excel and csv using maatwebsite
- Laravel 7.x and 6.x create custom facade example from scratch
- Node JS - Get Real time online users counter using socket io example
- PHP Laravel 7.x and 6.x - Bootstrap Admin Theme Integration example from scratch
- MySQL Query - How to select records of today to next Sunday ?
- Laravel 7.x and 6.x - username custom validation example
- Laravel 5.5 - Import Extract zip file using chumper/zipper package
- PHP Laravel Set Custom Validation Error Messages Example