Skip to content

Instantly share code, notes, and snippets.

@xiaoysh8
Created June 3, 2018 11:46
Show Gist options
  • Save xiaoysh8/3a989ef8871af3699ea88924cf8de680 to your computer and use it in GitHub Desktop.
Save xiaoysh8/3a989ef8871af3699ea88924cf8de680 to your computer and use it in GitHub Desktop.
laravel redirecting user
Redirecting Unauthenticated Users
When the auth middleware detects an unauthorized user, it will either return a JSON 401 response, or, if the request was not an AJAX request, redirect the user to the login named route.
You may modify this behavior by defining an unauthenticated function in your app/Exceptions/Handler.php file:
use Illuminate\Auth\AuthenticationException;
protected function unauthenticated($request, AuthenticationException $exception)
{
return $request->expectsJson()
? response()->json(['message' => $exception->getMessage()], 401)
: redirect()->guest(route('login'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment