MIF_E31222691/app/Http/Middleware/Authenticate.php

53 lines
1.2 KiB
PHP

<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
use Illuminate\Auth\AuthenticationException;
class Authenticate
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string ...$guards
* @return mixed
*
* @throws \Illuminate\Auth\AuthenticationException
*/
public function handle($request, Closure $next, ...$guards)
{
$this->authenticate($guards);
return $next($request);
}
/**
* Determine if the user is logged in to any of the given guards.
*
* @param array $guards
* @return void
*
* @throws \Illuminate\Auth\AuthenticationException
*/
protected function authenticate(array $guards)
{
if (count($guards) <= 1) {
Auth::guard(array_first($guards))->authenticate();
return Auth::shouldUse($guard);
}
foreach ($guards as $guard) {
if (Auth::guard($guard)->check()) {
return Auth::shouldUse($guard);
}
}
throw new AuthenticationException;
}
}