Jwt::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace xiaodi\JWTAuth\Middleware;
6
7
use think\App;
8
use xiaodi\JWTAuth\Exception\JWTException;
9
use xiaodi\JWTAuth\User;
10
11
/**
12
 * 中间件.
13
 */
14
class Jwt
15
{
16
    private $app;
17
    private $user;
18
19
    public function __construct(App $app, User $user)
20
    {
21
        $this->app = $app;
22
        $this->user = $user;
23
    }
24
25
    public function handle($request, \Closure $next)
26
    {
27
        if (true === $this->app->jwt->verify()) {
28
            // 自动注入用户模型
29
            if ($this->user->hasInject()) {
30
                $user = $this->user->get();
31
                // 路由注入
32
                $request->user = $user;
33
34
                // 绑定当前用户模型
35
                $model = $this->user->getModel();
36
                $this->app->bind($model, $user);
37
            }
38
39
            return $next($request);
40
        }
41
42
        throw new JWTException('Token 验证不通过', 401);
43
    }
44
}
45