From f35a0e0e6d2ff3e24b2cefb80be8f06849801a0b Mon Sep 17 00:00:00 2001 From: panliang <1163816051@qq.com> Date: Sat, 20 Apr 2024 10:45:25 +0800 Subject: [PATCH] AuthenticationException --- app/Exceptions/Handler.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 51c87f4..b90bf34 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -7,6 +7,7 @@ use Throwable; use Illuminate\Http\{Request, Response}; use Illuminate\Validation\ValidationException; use Slowlyo\OwlAdmin\Exceptions\AdminException; +use Illuminate\Auth\AuthenticationException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; class Handler extends ExceptionHandler @@ -49,4 +50,11 @@ class Handler extends ExceptionHandler 'errors' => $exception->errors(), ], $exception->status); } + + protected function unauthenticated($request, AuthenticationException $exception) + { + return $this->shouldReturnJson($request, $exception) + ? response()->json(['code' => Response::HTTP_UNAUTHORIZED, 'message' => $exception->getMessage()], 401) + : redirect()->guest($exception->redirectTo() ?? route('login')); + } }