File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -41,7 +41,7 @@ export const router = new Router();
4141
4242router .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```
Original file line number Diff line number Diff line change @@ -16,7 +16,7 @@ import { JwtStrategy } from '@aomex/auth-jwt-strategy';
1616
1717export 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<
8580const router = new Router ();
8681router .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```
Original file line number Diff line number Diff 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
5959auth .authenticate (' aaa' ); // ctx.aaa
You can’t perform that action at this time.
0 commit comments