Reputation: 41
I m following this https://www.codemag.com/Article/2001081/Nest.js-Step-by-Step-Part-3-Users-and-Authentication for implementing jwt using passport in nestJS, everything is working as expected, but I have one question in this approach, if we use UseGuards(@AuthGurads()), the app knows to use passportstrategy to verify the token and call the validate method to proceed further, but how come passport strategy knows the right jwt strategy class to pick for calling the validate method, it is not explicitly mentioned that we are asking passport strategy to use jwtStrategy class and it is not a default export, but still how passportstrategy after validating the token calls the right JwtStrategy class for validate method ?
Upvotes: 1
Views: 711
Reputation: 70510
The important thing for the answer is in this code block: (pulled from the linked article)
@Module({
imports: [ ...,
PassportModule.register({
defaultStrategy: 'jwt',
property: 'user',
session: false,
}), ...
],
controllers: [AuthController],
providers: [AuthService, JwtStrategy],
exports: [PassportModule],
})
export class AuthModule {}
The defaultStrategy
tells Nest "when I use AuthGuard()
, take this strategy as the one to use". In this case, 'jwt'
. Each passport strategy has a default name for passport to know what strategy is being used. In the case of passport-jwt
it's (surprise, surprise) 'jwt'
Upvotes: 1