How to return unauthorized using Laravel API Authentication
Please add the method in the class Handler in the file location app/Exceptions/Handler.php
/**
* 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);
}
return redirect()->guest(route('login'));
}
And also add the following line above the class in the same file as mentioned above: use Illuminate\Auth\AuthenticationException;
In the postman within the headers section please add the following header : X-Requested-With:XMLHttpRequest
Hope this helps and resolves the issue. Thanks.
Be sure to be sending the right headers in your request
Content-Type: application/json