如何在 NestJS 中像这样将 req
传递到 cookieExtractor
中:
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor() {
super({
jwtFromRequest: cookieExtractor(req), // I want to pass req here
secretOrKey: config.get('jwt.secret'),
});
}
}
export const cookieExtractor: JwtFromRequestFunction = (req: Request) => {
if (req && req.cookies) {
return req.cookies['access-token'];
}
return null;
};
答案 0 :(得分:1)
你不应该需要。您在此处所需要做的就是将属性设置为 jwtFromRequest: cookieExtractor
。由于 cookieExtractor
方法实现了 JwtFromRequestFunction
接口,passport 应该接受它,并且可以稍后调用它,完全没有问题。