Skip to content

Commit 9768166

Browse files
committed
更新身份认证文档,调整上下文中身份信息的存储位置,确保一致性并反映最新的API变更
1 parent 16c63cd commit 9768166

3 files changed

Lines changed: 7 additions & 12 deletions

File tree

docs/plugins/auth-bearer-strategy.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export const router = new Router();
4141

4242
router.get('/api', {
4343
action: (ctx) => {
44-
console.log(ctx.bearer.data); // { id: 1, name: 'abc' }
44+
console.log(ctx.auth.bearer.data); // { id: 1, name: 'abc' }
4545
},
4646
});
4747
```

docs/plugins/auth-jwt-strategy.md

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { JwtStrategy } from '@aomex/auth-jwt-strategy';
1616

1717
export const auth = new Auth({
1818
strategies: {
19-
jwt: new JwtStrategy<{ userId: number }>({
19+
jwt: new JwtStrategy({
2020
secret: 'YOUR_SECRET',
2121
}),
2222
},
@@ -64,19 +64,14 @@ header的key建议使用`authorization`,其它源的key建议使用`access_tok
6464

6565
### onVerified
6666

67-
**签名:**`(data: { payload: Payload; ctx: WebContext; token: string }): Promise<VerifiedPayload | false>`
67+
**签名:**`(payload: Payload; ctx: WebContext; token: string): Promise<VerifiedPayload | false>`
6868

6969
验证成功后的回调,对payload做额外处理。如果token或者payload无效,则返回`false`
7070

71-
如果最终的数据与令牌中存储的数据不一致,则需要传入泛型第二个参数:
72-
7371
```typescript
74-
const jwt = new JwtStrategy<
75-
{ userId: number },
76-
{ id: number; name: string; age: number } // [!code ++]
77-
>({
72+
const jwt = new JwtStrategy({
7873
secret: 'YOUR_SECRET',
79-
async onVerified({ payload }) {
74+
async onVerified(payload: { userId: number }) {
8075
return { id: payload.userId, name: '树先生', age: 30 };
8176
},
8277
});
@@ -85,7 +80,7 @@ const jwt = new JwtStrategy<
8580
const router = new Router();
8681
router.get('/api', {
8782
action: async (ctx) => {
88-
ctx.jwt; // {id: 1, name: '树先生', age: 30}
83+
ctx.auth.jwt.data; // {id: 1, name: '树先生', age: 30}
8984
},
9085
});
9186
```

docs/plugins/auth.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ auth.strategy('aaa').someMethod();
5353

5454
## 上下文key
5555

56-
默认地,被认证后的身份信息会存储在`ctx`上,而key就是策略对应的key。如果想修改,则需要在生成中间件时指定
56+
默认地,被认证后的身份信息会存储在`ctx.auth`上,而key就是策略对应的key。如果想修改,则需要在生成中间件时指定
5757

5858
```typescript
5959
auth.authenticate('aaa'); // ctx.aaa

0 commit comments

Comments
 (0)