如何从jwt策略中获取有效负载?

Ово*_*чоы 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 策略中返回的控制器中的有效负载呢?

Mic*_*evi 5

返回/解析的值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