From 703197e27acc99d8771f0baf4440916470fa8c81 Mon Sep 17 00:00:00 2001 From: Joseph Silber Date: Mon, 14 Mar 2016 22:47:17 -0400 Subject: [PATCH] Allow passing multiple gaurds to the auth middleware --- app/Http/Middleware/Authenticate.php | 37 ++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/app/Http/Middleware/Authenticate.php b/app/Http/Middleware/Authenticate.php index 67abcaea..e277e646 100644 --- a/app/Http/Middleware/Authenticate.php +++ b/app/Http/Middleware/Authenticate.php @@ -12,19 +12,40 @@ class Authenticate * * @param \Illuminate\Http\Request $request * @param \Closure $next - * @param string|null $guard + * @param string ...$guards * @return mixed */ - public function handle($request, Closure $next, $guard = null) + public function handle($request, Closure $next, ...$guards) { - if (Auth::guard($guard)->guest()) { - if ($request->ajax() || $request->wantsJson()) { - return response('Unauthorized.', 401); - } else { - return redirect()->guest('login'); + if ($this->check($guards)) { + return $next($request); + } + + if ($request->ajax() || $request->wantsJson()) { + return response('Unauthorized.', 401); + } else { + return redirect()->guest('login'); + } + } + + /** + * Determine if the user is logged in to any of the given guards. + * + * @param array $guards + * @return bool + */ + protected function check(array $guards) + { + if (empty($guards)) { + return Auth::check(); + } + + foreach ($guards as $guard) { + if (Auth::guard($guard)->check()) { + return true; } } - return $next($request); + return false; } }