에러 문구
[Nest] 13029 - 05/27/2024, 1:49:37 AM ERROR [ExceptionsHandler] secretOrPrivateKey must have a value Error: secretOrPrivateKey must have a value
- .env 파일에서 값을 불러 들이지 못하는 상황
에러상황 시 코드
auth.module.ts
import * as dotenv from 'dotenv';
dotenv.config();
import { forwardRef, Module } from '@nestjs/common';
import { JwtModule } from '@nestjs/jwt';
import { AuthService } from './auth.service';
import { AuthController } from './auth.controller';
import { UserModule } from '../user/user.module';
import { PassportModule } from '@nestjs/passport';
import { LocalStrategy } from './local.strategy';
import * as process from 'node:process';
import { JwtStrategy } from './jwt.strategy';
@Module({
imports: [
JwtModule.register({
secret: process.env.JWT_SECRET,
signOptions: { expiresIn: '3m' },
}),
forwardRef(() => UserModule),
PassportModule,
],
providers: [AuthService, LocalStrategy, JwtStrategy],
controllers: [AuthController],
exports: [AuthService],
})
export class AuthModule {}
구글링을 통해서 동기화의 문제로 .env 이 호출이 되지 않은 것을 파악,
https://stackoverflow.com/questions/58673430/error-secretorprivatekey-must-have-a-value
Error: secretOrPrivateKey must have a value
I am using jwt to create token, but when i login via postman I get the error "Error: secretOrPrivateKey must have a value" from my console. I have attached my login code. Please anyone who can help...
stackoverflow.com
에 있는 답변을 따라 JwtModule.register -> JwtModule.registerAsync 로 수정
import * as dotenv from 'dotenv';
dotenv.config();
import { forwardRef, Module } from '@nestjs/common';
import { JwtModule } from '@nestjs/jwt';
import { AuthService } from './auth.service';
import { AuthController } from './auth.controller';
import { UserModule } from '../user/user.module';
import { PassportModule } from '@nestjs/passport';
import { LocalStrategy } from './local.strategy';
import { JwtStrategy } from './jwt.strategy';
import { ConfigService } from '@nestjs/config';
@Module({
imports: [
JwtModule.registerAsync({
inject: [ConfigService],
useFactory: (configService: ConfigService) => ({
secret: configService.get<string>('JWT_SECRET'),
signOptions: { expiresIn: '3m' },
}),
}),
forwardRef(() => UserModule),
PassportModule,
],
providers: [AuthService, LocalStrategy, JwtStrategy],
controllers: [AuthController],
exports: [AuthService],
})
export class AuthModule {}
추가적으로
app.module.ts 에서 ConfigModule 을 global 하게 사용할 수 있도록 설정하고
TypeOrmModule 설정도 동기화 문제가 생기지 않도로 아래와 같이 설정
import * as dotenv from 'dotenv';
dotenv.config();
.....
@Module({
imports: [
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (configService: ConfigService) => ({
type: 'mysql',
host: configService.get<string>('DB_HOST'),
port: configService.get<number>('DB_PORT'),
username: configService.get<string>('DB_USERNAME'),
password: configService.get<string>('DB_PASSWORD'),
database: configService.get<string>('DB_NAME'),
autoLoadEntities: true,
synchronize: true,
logging: true,
namingStrategy: new SnakeNamingStrategy(),
}),
}),
ConfigModule.forRoot({
isGlobal: true,
envFilePath: `.env.${process.env.NODE_ENV}`,
}),
...
],
controllers: [AppController],
providers: [AppService, ConfigService],
})
export class AppModule {}
envFilePath: `.env.${process.env.NODE_ENV} 이 부분은
package.json 에 개발 모드에 따라 다른 .env 를 읽어 올수 있도록 설정한 것을 반영