• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

miaoxing / plugin / 7048390524

30 Nov 2023 03:02PM UTC coverage: 39.661% (+0.5%) from 39.147%
7048390524

push

github

twinh
ci: update package version

936 of 2360 relevant lines covered (39.66%)

18.18 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

56.0
/src/Auth/JwtAuth.php
1
<?php
2

3
namespace Miaoxing\Plugin\Auth;
4

5
use Miaoxing\Plugin\Service\Jwt;
6
use Miaoxing\Plugin\Service\UserModel;
7

8
/**
9
 * @mixin \JwtMixin
10
 * @mixin \ReqMixin
11
 */
12
class JwtAuth extends BaseAuth
13
{
14
    protected $loginRet;
15

16
    public function login(UserModel $user)
17
    {
18
        $data = ['id' => $user->id];
6✔
19
        $token = $this->jwt->generate($data);
6✔
20
        $this->loginRet = suc(['token' => $token, 'data' => $data]);
6✔
21
        return suc([
6✔
22
            'message' => '登录成功',
6✔
23
            'token' => $token,
6✔
24
        ]);
4✔
25
    }
26

27
    public function logout()
28
    {
29
        $this->loginRet = err('已退出登录');
×
30
    }
31

32
    public function checkLogin()
33
    {
34
        if (null === $this->loginRet) {
6✔
35
            $auth = $this->req->getServer('HTTP_AUTHORIZATION', '');
×
36
            // Remove "Bearer " prefix
37
            $auth = explode(' ', $auth)[1] ?? $auth;
×
38
            if (!$auth) {
×
39
                $ret = $this->loginRet();
×
40
            } else {
41
                $ret = $this->jwt->verify($auth);
×
42
                if ($ret->isErr()) {
×
43
                    Jwt::CODE_EXPIRED === $ret->getCode() && $ret->setMessage('您的登录已过期,请重新登录');
×
44
                    $ret->setCode(static::CODE_UNAUTHORIZED);
×
45
                }
46
            }
47

48
            $this->loginRet = $ret;
×
49
        }
50
        return $this->loginRet;
6✔
51
    }
52

53
    public function getData()
54
    {
55
        $ret = $this->checkLogin();
3✔
56
        if ($ret->isErr()) {
3✔
57
            return [];
×
58
        }
59

60
        return [
2✔
61
            'id' => $ret['data']['id'],
3✔
62
        ];
2✔
63
    }
64
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc