Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
checkJWTTokenMiddelware | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
30 | |
0.00% |
0 / 1 |
handle | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
30 |
1 | <?php |
2 | |
3 | namespace App\Http\Middleware; |
4 | |
5 | use App\Traits\response; |
6 | use Closure; |
7 | use Illuminate\Http\Request; |
8 | use Tymon\JWTAuth\Exceptions\TokenExpiredException; |
9 | use Tymon\JWTAuth\Exceptions\TokenInvalidException; |
10 | use Tymon\JWTAuth\Facades\JWTAuth; |
11 | use Tymon\JWTAuth\Exceptions\JWTException; |
12 | use Exception; |
13 | |
14 | |
15 | class checkJWTTokenMiddelware |
16 | { |
17 | use response; |
18 | /** |
19 | * Handle an incoming request. |
20 | * |
21 | * @param \Illuminate\Http\Request $request |
22 | * @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next |
23 | * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse |
24 | */ |
25 | public function handle(Request $request, Closure $next, $guard) |
26 | { |
27 | try { |
28 | config(['auth.defaults.guard' => 'user_api']); |
29 | $user = JWTAuth::parseToken()->authenticate(); |
30 | $token = JWTAuth::getToken(); |
31 | $payload = JWTAuth::getPayload($token)->toArray(); |
32 | |
33 | if ($payload['type'] != $guard) |
34 | return $this->failed(trans('auth.Not authorized'), 401, 'E01'); |
35 | |
36 | } catch (Exception $e) { |
37 | if ($e instanceof TokenInvalidException) { |
38 | return $this->failed(trans('auth.Token is Invalid'), 401, 'E01'); |
39 | } else if ($e instanceof TokenExpiredException) { |
40 | return $this->failed(trans('auth.Token is Expired'), 401, 'E01'); |
41 | } else { |
42 | return $this->failed(trans('auth.Authorization Token not found'), 404, 'E04'); |
43 | } |
44 | } |
45 | |
46 | return $next($request); |
47 | } |
48 | } |