56c4d72708
- We throw the exception to let the Handler.php handling the error
46 lines
1013 B
PHP
46 lines
1013 B
PHP
<?php
|
|
|
|
namespace App\Web\Middlewares;
|
|
|
|
use Closure;
|
|
use Illuminate\Contracts\Auth\Factory as Auth;
|
|
use \Illuminate\Http\Request;
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
|
|
|
class Authenticate
|
|
{
|
|
/**
|
|
* The authentication guard factory instance.
|
|
*
|
|
* @var \Illuminate\Contracts\Auth\Factory
|
|
*/
|
|
protected $auth;
|
|
|
|
/**
|
|
* Create a new middleware instance.
|
|
*
|
|
* @param \Illuminate\Contracts\Auth\Factory $auth
|
|
* @return void
|
|
*/
|
|
public function __construct(Auth $auth)
|
|
{
|
|
$this->auth = $auth;
|
|
}
|
|
|
|
/**
|
|
* Handle an incoming request.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param \Closure $next
|
|
* @param string|null $guard
|
|
* @return mixed
|
|
*/
|
|
public function handle(Request $request, Closure $next, $guard = null)
|
|
{
|
|
if ($this->auth->guard($guard)->guest()) {
|
|
throw new HttpException(401, 'Unhautorized');
|
|
}
|
|
return $next($request);
|
|
}
|
|
}
|