Skip to content

Instantly share code, notes, and snippets.

@full-stack-king
Last active February 28, 2020 04:59
Show Gist options
  • Save full-stack-king/b53a702abc836559e1222cafe9735c09 to your computer and use it in GitHub Desktop.
Save full-stack-king/b53a702abc836559e1222cafe9735c09 to your computer and use it in GitHub Desktop.
Return authentication exception for Laravel API 5.6
<?php
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $exception)
{
// custom exception handler for unauthenticated requests
if ($exception instanceof AuthenticationException) {
return response()->json([
'errors' => [
'status' => 401,
'message' => 'Unauthenticated',
]
], 401);
}
// default handler
return parent::render($request, $exception);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment