Commit dcc6328d authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

Modify handler to return json for some exceptions all the time also remove auth redirect

parent a8b75d2d
......@@ -6,7 +6,9 @@ use Exception;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Source\Exceptions\EntityExistsException;
use Illuminate\Auth\AuthenticationException;
use Source\Exceptions\EntityNotFoundException;
use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
class Handler extends ExceptionHandler {
......@@ -54,4 +56,30 @@ class Handler extends ExceptionHandler {
return parent::render($request, $exception);
}
/**
* Convert an authentication exception into a response.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Auth\AuthenticationException $exception
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function unauthenticated($request, AuthenticationException $exception) {
return response()->json(['message' => $exception->getMessage()], 401);
}
/**
* Create a response object from the given validation exception.
*
* @param \Illuminate\Validation\ValidationException $e
* @param \Illuminate\Http\Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function convertValidationExceptionToResponse(ValidationException $e, $request) {
if ($e->response) {
return $e->response;
}
return $this->invalidJson($request, $e);
}
}
......@@ -4,18 +4,15 @@ namespace App\Http\Middleware;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
class Authenticate extends Middleware
{
class Authenticate extends Middleware {
/**
* Get the path the user should be redirected to when they are not authenticated.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Http\Request $request
* @return string|null
*/
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
return route('login');
}
protected function redirectTo($request) {
// Don't return anything, as this application is basically a pure api, so, we don't have a login view route...
return null;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment