如何从 jwt 策略中获取 payload?
How to get payload from jwt strategy?
我有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策略返回的controller中的payload呢?
JwtStrategy#validate
的值 returned/resolved 将是 req.user
的值
import { Req } from '@nestjs/common'
// ...
@UseGuards(AuthGuard("jwt"))
@Get()
getPayload(@Req() req: any) {
console.log(req.user)
}
我有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策略返回的controller中的payload呢?
JwtStrategy#validate
的值 returned/resolved 将是 req.user
import { Req } from '@nestjs/common'
// ...
@UseGuards(AuthGuard("jwt"))
@Get()
getPayload(@Req() req: any) {
console.log(req.user)
}