Reputation: 3223
I'm implementing authentication for my NestJS project. I strictly followed the NestJS documentation and this but I still get an error when I try to start the API. I can't seem to figure out what is causing this. I tried setting the secretOrKey
to hardcoded string but no to effect then I moved it to a .env
still not working.
Below are my code for the module and the JwtStrategy file.
// AuthModule
@Module({
imports: [
ConfigModule.forRoot(),
UserModule,
PassportModule.register({ defaultStrategy: 'jwt' }),
TypeOrmModule.forFeature([Registration]),
JwtModule.register({
secretOrPrivateKey: process.env.JWT_SECRET_KEY,
signOptions: { expiresIn: process.env.JWT_EXPIRES }
}),
],
controllers: [AuthController],
providers: [AuthService, LocalStrategy, JwtStrategy],
exports: [AuthService, LocalStrategy, JwtStrategy]
})
export class AuthModule {}
// JWTStrategy
import { Injectable, UnauthorizedException } from "@nestjs/common";
import { PassportStrategy } from "@nestjs/passport";
import { Strategy, ExtractJwt } from "passport-jwt";
import { AuthService } from "./auth.service";
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor(private readonly authService: AuthService) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
passReqToCallback: true,
secretOrKey: 'test'
})
}
async validate(payload: any, done: Function) {
const user = await this.authService.validateUserToken(payload)
if (!user) {
return done(new UnauthorizedException(), false)
}
done(null, user)
}
}
This is the error I get from the terminal
[Nest] 6876 - 05/11/2020, 10:54:31 PM [NestFactory] Starting Nest application...
[Nest] 6876 - 05/11/2020, 10:54:31 PM [InstanceLoader] AppModule dependencies initialized +76ms
[Nest] 6876 - 05/11/2020, 10:54:31 PM [InstanceLoader] TypeOrmModule dependencies initialized +0ms
[Nest] 6876 - 05/11/2020, 10:54:31 PM [InstanceLoader] PassportModule dependencies initialized +1ms
[Nest] 6876 - 05/11/2020, 10:54:31 PM [InstanceLoader] PassportModule dependencies initialized +0ms
[Nest] 6876 - 05/11/2020, 10:54:31 PM [InstanceLoader] JwtModule dependencies initialized +0ms
[Nest] 6876 - 05/11/2020, 10:54:31 PM [InstanceLoader] ConfigHostModule dependencies initialized +0ms
[Nest] 6876 - 05/11/2020, 10:54:31 PM [InstanceLoader] ConfigModule dependencies initialized +0ms
[Nest] 6876 - 05/11/2020, 10:54:31 PM [InstanceLoader] TypeOrmCoreModule dependencies initialized +121ms
[Nest] 6876 - 05/11/2020, 10:54:31 PM [InstanceLoader] TypeOrmModule dependencies initialized +1ms
[Nest] 6876 - 05/11/2020, 10:54:31 PM [InstanceLoader] TypeOrmModule dependencies initialized +0ms
[Nest] 6876 - 05/11/2020, 10:54:31 PM [ExceptionHandler] JwtStrategy requires a secret or key +20ms
TypeError: JwtStrategy requires a secret or key
at new JwtStrategy (/Users/user/Documents/code/sideline/hyperspotters-api/node_modules/passport-jwt/lib/strategy.js:45:15)
at new MixinStrategy (/Users/user/Documents/code/sideline/hyperspotters-api/node_modules/@nestjs/passport/dist/passport/passport.strategy.js:31:13)
at new LocalStrategy (/Users/user/Documents/code/sideline/hyperspotters-api/dist/src/auth/local.strategy.js:18:9)
at Injector.instantiateClass (/Users/user/Documents/code/sideline/hyperspotters-api/node_modules/@nestjs/core/injector/injector.js:289:19)
at callback (/Users/user/Documents/code/sideline/hyperspotters-api/node_modules/@nestjs/core/injector/injector.js:76:41)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async Injector.resolveConstructorParams (/Users/user/Documents/code/sideline/hyperspotters-api/node_modules/@nestjs/core/injector/injector.js:117:24)
at async Injector.loadInstance (/Users/user/Documents/code/sideline/hyperspotters-api/node_modules/@nestjs/core/injector/injector.js:80:9)
at async Injector.loadProvider (/Users/user/Documents/code/sideline/hyperspotters-api/node_modules/@nestjs/core/injector/injector.js:37:9)
at async Promise.all (index 4)
✨ Done in 11.99s.
Upvotes: 1
Views: 2778
Reputation: 3223
I figured out the problem, on my local.strategy.ts file instead of importing Strategy class from passport-local it imported from passport-jwt.
Upvotes: 3