From 776b1cb68dade983c2b1d5c8f47a8626553ae935 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Mon, 13 Nov 2023 21:16:48 +0100 Subject: [PATCH] Add unauthenticated method to handle authentication exceptions --- app/Exceptions/Handler.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 24f4eff9e..1ac36da85 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -4,6 +4,7 @@ namespace App\Exceptions; use App\Models\InstanceSettings; use App\Models\User; +use Illuminate\Auth\AuthenticationException; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; use Sentry\Laravel\Integration; use Sentry\State\Scope; @@ -40,6 +41,13 @@ class Handler extends ExceptionHandler ]; private InstanceSettings $settings; + protected function unauthenticated($request, AuthenticationException $exception) + { + if ($request->is('api/*') || $request->expectsJson() || $this->shouldReturnJson($request, $exception)) { + return response()->json(['message' => $exception->getMessage()], 401); + } + return redirect()->guest($exception->redirectTo() ?? route('login')); + } /** * Register the exception handling callbacks for the application. */