1 year ago
#234023
avinash gurugubelli
Nest JS: TypeError: JwtStrategy requires a secret or key? when using with injection token
I want to create an auth module as a separate node package so that we can reuse it for multiple projects.
AS part of it we want to supply the necessary config from the main app via AuthLibModule. register() which supplies this param as config.
and the passed config will be held using an injection token to use it via dependency injection.
HOW DO WE ACHIEVE IT?
AuthLib.module.ts
@Module({})
export class AuthLibModule {
static register(config: AuthLibConfig): DynamicModule {
const authConfigToken = {
provide: AuthLibConstants.JWT_CONFIG_TOKEN,
useValue: config.jwtConfig
};
return {
module: AuthLibModule,
imports: [
ConfigModule.forRoot(
{
isGlobal: true
}
),
MongooseModule.forRoot(config.mongoDbUrl ? config.mongoDbUrl : process.env.MONGO_URI),
AuthModule
],
controllers: [],
exports: [authConfigToken],
providers: [authConfigToken]
}
}
}
APP.MODULE.TS
@Module({
imports: [
AuthLib.register({
authTokenExpiration: 60S,
authTokenSecret: XYZZZZZ,
refreshTokenSecret: YYYYYYYYYYYYYYYYY,
refreshTokenExpiration: 90S
})
],
controllers: [],
providers: [],
})
export class AppModule { }
jwt.strategy.ts
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy, GuardNames.jwtAuth) {
constructor(
@Inject(AuthLibConstants.JWT_CONFIG) jwtConfig: JWTConfig
) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
ignoreExpiration: false,
// jwtConfig.authTokenSecret -> getting as undefined.
secretOrKey: jwtConfig.authTokenSecret
});
}
async validate(payload: TokenParsePayload): Promise<TokenParsePayload> {
return payload;
}
}
Error:
[Nest] 21492 - 28/02/2022, 5:30:03 pm ERROR [ExceptionHandler] JwtStrategy requires a secret or key
TypeError: JwtStrategy requires a secret or key
at new JwtStrategy (D:\Freelancing\code-mentor\infra-services\auth-lib\node_modules\passport-jwt\lib\strategy.js:45:15)
at new MixinStrategy (D:\Freelancing\code-mentor\infra-services\auth-lib\node_modules\@nestjs\passport\dist\passport\passport.strategy.js:32:13)
at new JwtStrategy (D:\Freelancing\code-mentor\infra-services\auth-lib\src\auth\strategies\Jwt-auth.strategy.ts:17:9)
at Injector.instantiateClass (D:\Freelancing\code-mentor\infra-services\auth-lib\node_modules\@nestjs\core\injector\injector.js:301:19)
at callback (D:\Freelancing\code-mentor\infra-services\auth-lib\node_modules\@nestjs\core\injector\injector.js:48:41)
at Injector.resolveConstructorParams (D:\Freelancing\code-mentor\infra-services\auth-lib\node_modules\@nestjs\core\injector\injector.js:124:24)
at Injector.loadInstance (D:\Freelancing\code-mentor\infra-services\auth-lib\node_modules\@nestjs\core\injector\injector.js:52:9)
at Injector.loadProvider (D:\Freelancing\code-mentor\infra-services\auth-lib\node_modules\@nestjs\core\injector\injector.js:74:9)
at async Promise.all (index 6)
at InstanceLoader.createInstancesOfProviders (D:\Freelancing\code-mentor\infra-services\auth-lib\node_modules\@nestjs\core\injector\instance-loader.js:44:9)
node.js
typescript
nestjs
passport.js
nestjs-jwt
0 Answers
Your Answer