On Laravel 5.6, when I call a route protected by the auth:api
middleware I get a 400 response error instead of the 401 I expect.
api.php:
Route::middleware('auth:api')->group(function() {
//...
}
And the Exception handler:
public function render($request, Exception $e)
{
// If the request wants JSON (AJAX doesn't always want JSON)
if ($request->wantsJson()) {
// Define the response
$response = [
'errors' => 'Sorry, something went wrong.'
];
// If the app is in debug mode
if (config('app.debug')) {
// Add the exception class name, message and stack trace to response
$response['exception'] = get_class($e); // Reflection might be better here
$response['message'] = $e->getMessage();
$response['trace'] = $e->getTrace();
}
// Default response of 400
$status = 400;
// If this exception is an instance of HttpException
if ($this->isHttpException($e)) {
// Grab the HTTP status code from the Exception
$status = $e->getStatusCode();
}
// Return a JSON response with the response array and status code
return response()->json($response, $status);
}
// Default to the parent class' implementation of handler
return parent::render($request, $e);
}
The error:
{
"errors": "Sorry, something went wrong.",
"exception": "Illuminate\\Auth\\AuthenticationException",
"message": "Unauthenticated.",
"trace": [Here's the trace]
}
Expected behaviour is visible in this answer, though the problem is different.
Why am I getting a 400 with the above instead of a 401 with just the message
?