如何从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;
}
}

这是我的控制器:

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
}
}

那么我如何在jwt策略中返回的控制器中获得有效载荷?

JwtStrategy#validate返回/解析的值将是req.user的值

import { Req } from '@nestjs/common'
// ...
@UseGuards(AuthGuard("jwt"))
@Get()
getPayload(@Req() req: any) {
console.log(req.user)
}

https://docs.nestjs.com/security/authentication login-route

最新更新