AuthCheckMiddleware.php
1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
namespace App\Http\Middleware;
use App\Enums\UserStatusEnum;
use Closure;
use Hikoon\LaravelJwt\JwtGuard;
use Illuminate\Contracts\Auth\Factory as Auth;
use Illuminate\Http\Request;
class AuthCheckMiddleware
{
/**
* The authentication factory instance.
*
* @var \Illuminate\Contracts\Auth\Factory
*/
protected Auth $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 \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @param mixed ...$guards
* @throws \Throwable
*/
public function handle(Request $request, Closure $next, ...$guards)
{
if (empty($guards)) {
$guards = [NULL];
}
foreach ($guards as $guard) {
$authGuard = $this->auth->guard($guard);
$check = $authGuard instanceof JwtGuard ? $authGuard->checkOrFail() : $authGuard->check();
if ($check && $authGuard->user()?->getAttribute('status') === UserStatusEnum::ACTIVATE) {
$this->auth->shouldUse($guard);
}
}
return $next($request);
}
}