GetUserFromToken   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 60%

Importance

Changes 0
Metric Value
wmc 4
eloc 11
dl 0
loc 24
ccs 6
cts 10
cp 0.6
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 19 4
1
<?php
2
3
namespace App\Http\Middleware;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
use Tymon\JWTAuth\Exceptions\TokenInvalidException;
8
9
class GetUserFromToken extends BaseMiddleware
10
{
11
    /**
12
     * @return mixed
13
     */
14 33
    public function handle(Request $request, Closure $next)
15
    {
16 33
        if (!$token = $this->auth->setRequest($request)->getToken()) {
17
            return $this->respond('tymon.jwt.absent', 'token_not_provided', 401);
18
        }
19
20
        try {
21 33
            $user = $this->auth->authenticate($token);
22
        } catch (TokenInvalidException $exception) {
23
            abort(401, 'Invalid or expired token');
24
        }
25
26 33
        if (!$user) {
27
            return $this->respond('tymon.jwt.user_not_found', 'user_not_found', 401);
28
        }
29
30 33
        $this->events->dispatch('tymon.jwt.valid', $user);
31
32 33
        return $next($request);
33
    }
34
}
35