Currently I have a login, register, update and delete functionality using my api made in Laravel using passport feature. Everything works fine the insertion of data and fetc
Here's how I solved it. If you are using Laravel 5.5 or above you can override the default exception handler by editing app/Exceptions/Handler.php to add the following:
use Illuminate\Auth\AuthenticationException;
protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->expectsJson()) {
$json = [
'isAuth'=>false,
'message' => $exception->getMessage()
];
return response()
->json($json, 401);
}
$guard = array_get($exception->guards(),0);
switch ($guard) {
default:
$login = 'login';
break;
}
return redirect()->guest(route($login));
}
In the JSON return, you can add any parameters per your requirement.