Skip to content

Commit

Permalink
Convert an AuthenticationException into an unauthenticated response
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephSilber committed May 22, 2016
1 parent 1fd2064 commit 8d9f92a
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Illuminate/Foundation/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Exception;
use Psr\Log\LoggerInterface;
use Illuminate\Http\Response;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Validation\ValidationException;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Http\Exception\HttpResponseException;
Expand Down Expand Up @@ -100,6 +101,8 @@ public function render($request, Exception $e)
return $e->getResponse();
} elseif ($e instanceof ModelNotFoundException) {
$e = new NotFoundHttpException($e->getMessage(), $e);
} elseif ($e instanceof AuthenticationException) {
return $this->unauthenticated($request, $e);
} elseif ($e instanceof AuthorizationException) {
$e = new HttpException(403, $e->getMessage());
} elseif ($e instanceof ValidationException && $e->getResponse()) {
Expand Down Expand Up @@ -175,6 +178,22 @@ protected function convertExceptionToResponse(Exception $e)
return SymfonyResponse::create($decorated, $e->getStatusCode(), $e->getHeaders());
}

/**
* Convert an authentication exception into an unauthenticated response.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Auth\AuthenticationException $e
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function unauthenticated($request, AuthenticationException $e)
{
if ($request->ajax() || $request->wantsJson()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest('login');
}
}

/**
* Get the html response content.
*
Expand Down

0 comments on commit 8d9f92a

Please sign in to comment.