"TypeError: JwtStrategy requires a secret or key" with NestJS
huss-a opened this issue · comments
I'm getting a TypeError: JwtStrategy requires a secret or key
error in a NestJS application, here's the code
// auth.module.ts (AuthModule)
import { Module } from '@nestjs/common';
import { AuthService } from './auth.service';
import { AuthController } from './auth.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UsersRepo } from './users.repository';
import { PassportModule } from '@nestjs/passport';
import { JwtModule } from '@nestjs/jwt';
import { JwtStrategy } from './jwt.strategy';
@Module({
imports: [
PassportModule.register({ defaultStrategy: 'jwt' }),
JwtModule.register({
secret: 'keyboard-cat',
signOptions: {
expiresIn: 3600,
},
}),
TypeOrmModule.forFeature([UsersRepo]),
],
providers: [AuthService, JwtStrategy],
controllers: [AuthController],
exports: [JwtStrategy, PassportModule],
})
export class AuthModule {}
// jwt.strategy.ts
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { InjectRepository } from '@nestjs/typeorm';
import { validate } from 'class-validator';
import { ExtractJwt, Strategy } from 'passport-jwt';
import { jwtPayload } from './jwt-payload.interface';
import { UsersRepo } from './users.repository';
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor(@InjectRepository(UsersRepo) private usersRepo: UsersRepo) {
super({
secret: 'keyboard-cat',
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
});
}
}
Pretty sure the "secret" in your constructor and jwtmodule.register need to be "secretOrKey"
@VividLemon this is not correct @nestjs/jwt
does accept the secret
param, the passport-jwt
library on the other side does not and this should be replaced with secretOrKey
.
Im having the same issue. I even try to hardcode the secret string, for discard environment issues.
And the result is :
[Nest] 46027 - 05/20/2023, 10:30:27 PM ERROR [ExceptionsHandler] secretOrPrivateKey must have a value
Error: secretOrPrivateKey must have a value
I dont know what else do
I believe your .env has to have a SECRET_KEY=""
@neoligero same thing here… I am really struggling and don’t understand why not even hardcoding it I get rid of the ffff error, I m desperate. Did you solve it?