Skip to content

Commit

Permalink
Merge pull request #4522 from gauravmak/patch-1
Browse files Browse the repository at this point in the history
[5.7] Specify redirects for unauthorized users in middleware
  • Loading branch information
taylorotwell committed Sep 15, 2018
2 parents 91d5717 + a3d4a2e commit 424769f
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,17 +182,13 @@ Of course, if you are using [controllers](/docs/{{version}}/controllers), you ma

#### 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](/docs/{{version}}/routing#named-routes).
When the `auth` middleware detects an unauthorized user, it will redirect the user to the `login` [named route](/docs/{{version}}/routing#named-routes).

You may modify this behavior by defining an `unauthenticated` function in your `app/Exceptions/Handler.php` file:
You may modify this behavior by updating the `redirectTo` function in your `app/Http/Middleware/Authenticate.php` file:

use Illuminate\Auth\AuthenticationException;

protected function unauthenticated($request, AuthenticationException $exception)
protected function redirectTo($request)
{
return $request->expectsJson()
? response()->json(['message' => $exception->getMessage()], 401)
: redirect()->guest(route('login'));
return route('login');
}

#### Specifying A Guard
Expand Down

0 comments on commit 424769f

Please sign in to comment.