1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- <?php
- namespace App\Http\Middleware;
- use Closure;
- use Illuminate\Support\Facades\Auth;
- class Authenticate
- {
-
- public function handle($request, Closure $next, ...$guards)
- {
- if ($this->check($guards)) {
- return $next($request);
- }
- if ($request->ajax() || $request->wantsJson()) {
- return response('Unauthorized.', 401);
- } else {
- return redirect()->guest('login');
- }
- }
-
- protected function check(array $guards)
- {
- if (empty($guards)) {
- return Auth::check();
- }
- foreach ($guards as $guard) {
- if (Auth::guard($guard)->check()) {
- Auth::shouldUse($guard);
- return true;
- }
- }
- return false;
- }
- }
|