From a1fd30d36902e06ffba06759193b037918c97a26 Mon Sep 17 00:00:00 2001 From: Database Server Date: Mon, 7 Nov 2022 09:13:40 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E8=AE=BF=E9=97=AE=20API=20?= =?UTF-8?q?=E7=9A=84=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Exceptions/Handler.php | 19 ++++++++++++++++++- .../Middleware/RedirectIfAuthenticated.php | 5 +++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 82a37e4..791a8f4 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -2,8 +2,9 @@ namespace App\Exceptions; -use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; use Throwable; +use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; +use Illuminate\Auth\AuthenticationException; class Handler extends ExceptionHandler { @@ -47,4 +48,20 @@ public function register() // }); } + + /** + * Convert an authentication exception into an unauthenticated response. + * + * @param \Illuminate\Http\Request $request + * @param \Illuminate\Auth\AuthenticationException $exception + * @return \Illuminate\Http\Response + */ + protected function unauthenticated($request, AuthenticationException $exception) + { + if ($request->expectsJson()) { + return response()->json(['error' => 'Unauthenticated.'], 401); + } + + return redirect()->guest(route('index')); + } } diff --git a/app/Http/Middleware/RedirectIfAuthenticated.php b/app/Http/Middleware/RedirectIfAuthenticated.php index a2813a0..6dafc19 100644 --- a/app/Http/Middleware/RedirectIfAuthenticated.php +++ b/app/Http/Middleware/RedirectIfAuthenticated.php @@ -23,6 +23,11 @@ public function handle(Request $request, Closure $next, ...$guards) foreach ($guards as $guard) { if (Auth::guard($guard)->check()) { + // $user = $this->auth->guard($guard)->user(); + // if ($user->banned_at) { + // return $this->forbidden('您已被封禁,原因是: ' . $user->banned_reason ?? '一次或多次触犯了我们的规则。'); + // } + return redirect(RouteServiceProvider::HOME); } }