Don't redirect for api calls

When calling api routes the Authenticate middleware attempts to redirect you to the login page. If you expect JSON back or don't have auth routes then you don't want this to happen. By re-using the logic from Laravel's exception handler on which format to output we can also determine wether to redirect the user to the login page or give them a JSON error response.
This commit is contained in:
Dries Vints 2018-10-12 15:40:15 +02:00
parent c9e612f510
commit 6f3aa7a4c5
No known key found for this signature in database
GPG Key ID: BDD2ED2E8C8025E7
1 changed files with 3 additions and 1 deletions

View File

@ -14,6 +14,8 @@ class Authenticate extends Middleware
*/
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
return route('login');
}
}
}