Skip to content

Commit 7dffc61

Browse files
author
qbhy
committed
feat: jwtguard添加自定义payload功能
1 parent 6dbd8b4 commit 7dffc61

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

src/Guard/JwtGuard.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ public function parseToken()
6464
return null;
6565
}
6666

67-
public function login(Authenticatable $user)
67+
public function login(Authenticatable $user, array $payload = [])
6868
{
69-
$token = $this->getJwtManager()->make([
69+
$token = $this->getJwtManager()->make(array_merge($payload, [
7070
'uid' => $user->getId(),
7171
's' => str_random(),
72-
])->token();
72+
]))->token();
7373

7474
Context::set($this->resultKey($token), $user);
7575

@@ -184,9 +184,9 @@ public function getJwtManager(): JWTManager
184184
* 获取 token 标识.
185185
* 为了性能,直接 md5.
186186
*
187-
* @throws \Qbhy\SimpleJwt\Exceptions\InvalidTokenException
188187
* @throws \Qbhy\SimpleJwt\Exceptions\SignatureException
189188
* @throws \Qbhy\SimpleJwt\Exceptions\TokenExpiredException
189+
* @throws \Qbhy\SimpleJwt\Exceptions\InvalidTokenException
190190
* @return mixed|string
191191
*/
192192
protected function getJti(string $token): string

src/Guard/SsoGuard.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ public function getClients(): array
4545
return $this->config['clients'] ?? ['unknown'];
4646
}
4747

48-
public function login(Authenticatable $user, string $client = null)
48+
public function login(Authenticatable $user, array $payload = [], string $client = null)
4949
{
5050
$client = $client ?: $this->getClients()[0]; // 需要至少配置一个客户端
51-
$token = parent::login($user);
51+
$token = parent::login($user, $payload);
5252
$redisKey = str_replace('{uid}', (string) $user->getId(), $this->config['redis_key'] ?? 'u:token:{uid}');
5353

5454
if (! empty($previousToken = $this->redis->hGet($redisKey, $client)) && $previousToken != $token) {

tests/Cases/ExampleTest.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ public function testJwtGuard()
7272
return $guard->guest();
7373
}));
7474

75+
$this->assertTrue($guard->getJwtManager()->justParse($guard->login($user, [
76+
'sub' => 'qbhy0715',
77+
'iss' => 'hyperf-auth',
78+
]))->getPayload()['sub'] == 'qbhy0715');
79+
7580
$token = dev_clock('jwt login 方法', function () use ($auth, $user) {
7681
return $auth->login($user);
7782
});
@@ -133,20 +138,20 @@ public function testSsoGuard()
133138
$this->assertTrue($guard->getProvider() instanceof EloquentProvider);
134139

135140
$user = $this->user(10);
136-
$token = $guard->login($user, 'pc');
141+
$token = $guard->login($user, [], 'pc');
137142
$this->assertTrue(is_string($token));
138143
$this->assertTrue($guard->check($token));
139144

140145
// 抢线登录
141-
$newToken = $guard->login($user, 'pc');
146+
$newToken = $guard->login($user, [], 'pc');
142147
$this->assertTrue($newToken != $token);
143148
$this->assertTrue($guard->check($newToken));
144149

145150
// 测试掉线的 token 还能不能用
146151
$this->assertTrue($guard->guest($token));
147152

148153
// 第二个设备登录
149-
$weappToken = $guard->login($user, 'weapp');
154+
$weappToken = $guard->login($user, [], 'weapp');
150155
$this->assertTrue($guard->check($weappToken));
151156
}
152157

0 commit comments

Comments
 (0)