【发布时间】:2020-10-05 07:24:01
【问题描述】:
所以,我很困惑。我慢慢掌握了 NestJS,但 passport 的工作方式让我感到困惑。
我关注了tutorial,一切正常。
我创建了一个 JWT 策略:
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor(private prisma: PrismaService) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: 'topSecret51',
});
}
async validate(payload: JwtPayload): Promise<User | null> {
const { email } = payload;
const user = await this.prisma.user.findOne({
where: { email }
});
if (!user) {
throw new UnauthorizedException('Athorisation not provided')
}
return user;
}
}
并定义了模块:
@Module({
imports: [
PassportModule.register({
defaultStrategy: 'jwt',
}),
JwtModule.register({
secret: 'topSecret51',
signOptions: {
expiresIn: 3600,
},
})
],
providers: [UserService, PrismaService, JwtStrategy],
controllers: [UserController],
exports: [JwtStrategy, PassportModule],
})
export class UserModule {}
并且瞧一个有效的令牌被发行。我不明白的是passport 是如何访问JwtStrategy 的。护照如何知道我的文件夹结构中有一个文件包含JwtStrategy?
1.) 这不是PassportModule 或JWTModule 注入的依赖项
2.) 它不作为参数传递给任何方法
是否有一些幕后魔术可以查看所有提供者并确定它们中的任何一个是否是提供给PassportStrategy 的参数的子类?
【问题讨论】: