From 5b3c5f3f4ecaa3e429bf7af25210c1d03b36494e Mon Sep 17 00:00:00 2001 From: Jacob Bennett Date: Fri, 22 Jan 2016 10:01:39 -0600 Subject: [PATCH] Don't return a login page to a JSON request Currently, any unauthorized API requests that pass through the `auth` middleware get a redirect to the login page. Adding the `wantsJson` flag to the conditional corrects this behavior. --- app/Http/Middleware/Authenticate.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Middleware/Authenticate.php b/app/Http/Middleware/Authenticate.php index d670fbfe..67abcaea 100644 --- a/app/Http/Middleware/Authenticate.php +++ b/app/Http/Middleware/Authenticate.php @@ -18,7 +18,7 @@ class Authenticate public function handle($request, Closure $next, $guard = null) { if (Auth::guard($guard)->guest()) { - if ($request->ajax()) { + if ($request->ajax() || $request->wantsJson()) { return response('Unauthorized.', 401); } else { return redirect()->guest('login');