Skip to content

Commit 1d70ab2

Browse files
committed
update
1 parent 1aaf440 commit 1d70ab2

3 files changed

Lines changed: 27 additions & 47 deletions

File tree

README.md

Lines changed: 24 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,25 @@ $ php think jwt:make
1919
2. 配置
2020
`config/jwt.php`
2121

22-
* `sso` 是否单点登录
23-
* `ssoCacheKey` 缓存前缀
24-
* `ssoKey` 用户唯一标识(多点登录 设置失效)
22+
* `uniqidKey` 用户唯一标识
2523
* `signerKey` 密钥
2624
* `notBefore` 时间前不能使用 默认生成后直接使用
2725
* `expiresAt` Token有效期(秒)
2826
* `signer` 加密算法
2927
* `type` 获取 Token 途径
30-
* `injectUser` 是否注入用户模型
31-
* `userModel` 用户模型
32-
* `hasLogged` 开启单点登录时,多点登录抛异常code = 50401
33-
* `tokenAlready` Token过期抛异常code = 50402
28+
* `inject` 是否注入用户模型
29+
* `model` 用户模型
30+
* `refresh` Token过期抛异常code = 50001
31+
* `relogin` Token失效异常code = 50002
32+
33+
* `xiaodi\Exception\HasLoggedException`
34+
* `xiaodi\Exception\TokenAlreadyEexpired`
3435

35-
`xiaodi\Exception\HasLoggedException`, `xiaodi\Exception\TokenAlreadyEexpired`
3636
以上两个异常都会抛一个HTTP异常 StatusCode = 401
3737

3838
3. Token 生成
3939
```php
40-
use xiaodi\Facade\Jwt;
40+
use xiaodi\JWTAuth\Facade\Jwt;
4141

4242
public function login()
4343
{
@@ -46,16 +46,17 @@ public function login()
4646
return json([
4747
'token' => Jwt::token(['uid' => 1]),
4848
'token_type' => Jwt::type(),
49-
'expires_in' => Jwt::ttl()
49+
'expires_in' => Jwt::ttl(),
50+
'refresh_in' => Jwt::refreshTTL()
5051
]);
5152
}
5253
```
5354

5455
4. Token 验证(手动)
5556
```php
56-
use xiaodi\Facade\Jwt;
57-
use xiaodi\Exception\HasLoggedException;
58-
use xiaodi\Exception\TokenAlreadyEexpired;
57+
use xiaodi\JWTAuth\Facade\Jwt;
58+
use xiaodi\JWTAuth\Exception\HasLoggedException;
59+
use xiaodi\JWTAuth\Exception\TokenAlreadyEexpired;
5960

6061
class User {
6162

@@ -79,7 +80,7 @@ class User {
7980

8081
5. Token 验证(中间件)
8182
```php
82-
use xiaodi\Jwt;
83+
use xiaodi\JWTAuth\Jwt;
8384

8485
use app\model\User;
8586

@@ -94,7 +95,7 @@ class UserController {
9495
// 开启用户模型注入
9596
public function user(User $user)
9697
{
97-
var_dump($user->name);
98+
var_dump($user->nickname);
9899
}
99100
}
100101

@@ -112,7 +113,7 @@ class UserController {
112113

113114
类型 | 途径 | 标识 |
114115
:-: | :-: | :-: |
115-
Bearer | Authorization | Bearer |
116+
Header | Authorization | Bearer Token |
116117
Cookie | Cookie| token |
117118
Url | Request | token |
118119

@@ -123,34 +124,13 @@ Url | Request | token |
123124

124125
return [
125126

126-
// ...其它配置
127-
'type' => 'Bearer',
127+
'default' => [
128+
// ...其它配置
129+
'type' => 'Bearer',
130+
131+
// 'type' => 'Cookie',
132+
// 'type' => 'Url',
133+
]
128134

129-
// 'type' => 'Cookie',
130-
// 'type' => 'Url',
131135
];
132136
```
133-
134-
```php
135-
# UserController.php
136-
137-
use xiaodi\Facade\Jwt;
138-
139-
class User
140-
{
141-
public function index()
142-
{
143-
// 自动获取并验证
144-
try {
145-
Jwt::verify();
146-
} catch (HasLoggedException $e) {
147-
// 已在其它终端登录
148-
} catch (TokenAlreadyEexpired $e) {
149-
// Token已过期
150-
}
151-
152-
$uid = Jwt::userId();
153-
}
154-
155-
}
156-
```

src/Handle/RequestToken.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ public function __construct(App $app)
2727
*
2828
* @return string
2929
*/
30-
public function getToken(string $handle): string
30+
public function get(string $handle): string
3131
{
3232
if (!in_array($handle, $this->handles)) {
33-
throw new JwtException('不支持只方式获取.', 500);
33+
throw new JwtException('不支持此方式获取.', 500);
3434
}
3535

3636
$this->token = (new Header($this->app))->handle();

src/Jwt.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ protected function getRequestToken(): Token
152152
{
153153
$requestToken = new RequestToken($this->app);
154154

155-
$token = $requestToken->getToken($this->type());
155+
$token = $requestToken->get($this->type());
156156

157157
try {
158158
$token = (new Parser())->parse($token);

0 commit comments

Comments
 (0)