Merge pull request #3685 from JosephSilber/multi-auth
Allow passing multiple gaurds to the auth middleware
This commit is contained in:
commit
4f4d378d6a
|
@ -12,19 +12,40 @@ class Authenticate
|
||||||
*
|
*
|
||||||
* @param \Illuminate\Http\Request $request
|
* @param \Illuminate\Http\Request $request
|
||||||
* @param \Closure $next
|
* @param \Closure $next
|
||||||
* @param string|null $guard
|
* @param string ...$guards
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function handle($request, Closure $next, $guard = null)
|
public function handle($request, Closure $next, ...$guards)
|
||||||
{
|
{
|
||||||
if (Auth::guard($guard)->guest()) {
|
if ($this->check($guards)) {
|
||||||
if ($request->ajax() || $request->wantsJson()) {
|
return $next($request);
|
||||||
return response('Unauthorized.', 401);
|
}
|
||||||
} else {
|
|
||||||
return redirect()->guest('login');
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue