Files
spa-api/app/Http/Middleware/Authenticate.php

52 lines
1.1 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Middleware;
2014-08-11 10:13:20 -05:00
2014-10-06 15:25:53 -05:00
use Closure;
2015-12-03 12:25:38 -06:00
use Illuminate\Support\Facades\Auth;
2014-08-11 10:13:20 -05:00
2015-02-22 20:47:03 -06:00
class Authenticate
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string ...$guards
2015-02-22 20:47:03 -06:00
* @return mixed
*/
public function handle($request, Closure $next, ...$guards)
2015-02-22 20:47:03 -06:00
{
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;
2015-02-22 20:47:03 -06:00
}
}
2014-08-11 10:13:20 -05:00
return false;
2015-02-22 20:47:03 -06:00
}
}