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); } }