Ово*_*чоы 1 node.js jwt nestjs nestjs-jwt
我有 jwt 策略:
export class JwtStrategy extends PassportStrategy(Strategy, "jwt") {
constructor() {
super({
ignoreExpiration: false,
secretOrKey: "secret",
jwtFromRequest: ExtractJwt.fromExtractors([
(request: Request) => {
let data = request.cookies['access'];
return data;
}
]),
});
}
async validate(payload: any){
return payload;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的控制器:
export class AuthController {
constructor(private authService: AuthService) {}
@UseGuards(AuthGuard("jwt"))
@Get()
getPayload() {
//here I need to get the payload that was returned in jwt strategy
}
}
Run Code Online (Sandbox Code Playgroud)
那么如何获取 jwt 策略中返回的控制器中的有效负载呢?
返回/解析的值JwtStrategy#validate将是req.user
import { Req } from '@nestjs/common'
// ...
@UseGuards(AuthGuard("jwt"))
@Get()
getPayload(@Req() req: any) {
console.log(req.user)
}
Run Code Online (Sandbox Code Playgroud)
https://docs.nestjs.com/security/authentication#login-route
| 归档时间: |
|
| 查看次数: |
1587 次 |
| 最近记录: |