Passed
Push — master ( 3e77af...7c612c )
by Brian
02:49
created

RedirectIfNotAdmin::handle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 12
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 3
1
<?php
2
3
namespace App\Modules\Admins\Http\Middleware;
4
5
use Closure;
6
use Illuminate\Auth\AuthenticationException;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Facades\Auth;
9
use Symfony\Component\HttpFoundation\Response;
10
11
/**
12
 * @see \Http\Middleware\Authenticate
13
 */
14
class RedirectIfNotAdmin
15
{
16
    /**
17
     * Handle an incoming request.
18
     *
19
     * @throws \Illuminate\Auth\AuthenticationException
20
     */
21
    public function handle(Request $request, Closure $next, string $guard = 'admin'): Response
22
    {
23
        if (Auth::guard($guard)->check()) {
24
            return $next($request);
25
        }
26
27
        $redirectToRoute = $request->expectsJson() ? '' : route('admin.login');
28
29
        throw new AuthenticationException(
30
            'Unauthenticated.',
31
            [$guard],
32
            $redirectToRoute
33
        );
34
    }
35
}
36