RefreshToken   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 34
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 23 2
1
<?php
2
3
namespace Modules\Core\Http\Middleware;
4
5
use Closure;
6
use Illuminate\Contracts\Auth\Guard as GuardContract;
7
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
8
use Tymon\JWTAuth\Exceptions\JWTException;
9
use Tymon\JWTAuth\JWTAuth;
10
use Tymon\JWTAuth\Providers\Auth\Illuminate;
11
12
class RefreshToken extends BaseAuthenticate
13
{
14
    /**
15
     * Handle an incoming request.
16
     *
17
     * @param \Illuminate\Http\Request $request
18
     * @param \Closure                 $next
19
     * @param $guard
20
     *
21
     * @return mixed
22
     */
23
    public function handle($request, Closure $next, $guard)
24
    {
25
        app()->singleton('tymon.jwt.auth', function () use ($guard) {
26
            /** @var GuardContract $auth */
27
            $auth = auth($guard);
28
29
            return new JWTAuth(app('tymon.jwt.manager'), new Illuminate($auth), app('tymon.jwt.parser'));
30
        });
31
32
        $this->auth = app('tymon.jwt.auth');
33
34
        $this->checkForToken($request);
35
36
        try {
37
            $token = $this->auth->parseToken()->refresh();
38
        } catch (JWTException $e) {
39
            throw new UnauthorizedHttpException('jwt-auth', $e->getMessage(), $e, $e->getCode());
40
        }
41
42
        $response = $next($request);
43
44
        // Send the refreshed token back to the client.
45
        return $this->setAuthenticationHeader($response, $token);
46
    }
47
}
48