So, I am confused. I'm slowly getting a grip around NestJS but the way passport works has me baffled.
I followed the tutorial and everything works.
I created a JWT Strategy:
@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;
}
}
And defined the module:
@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 {}
And voila a valid token is issued. What I don't understand is how passport accesses the JwtStrategy. How is passport aware that there is a file in my folder structure that contains a JwtStrategy?
1.) It is not dependency injected by the PassportModule or JWTModule
2.) It is not passed as an argument to any method
Is there some behind-the-scenes magic that looks through all providers and determines if any of them is a sub-class of the argument provided to PassportStrategy?