nest.js
nest.js "secretOrPrivateKey must have a value" error
FaustK
2022. 8. 20. 12:59
process.env.JWT_SECRET 값을 제대로 읽지 못하는 문제가 있었다.
dotenv 모듈도 설치도 하고 config 설정도 해주었지만 문제가 해결되지는 않았다.
해결 방법은 굉장히 단순했다.
백틱으로 감싸주니 문제가 해결되었다.
- jwt.strategy.ts
import { Injectable } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { ExtractJwt, Strategy } from 'passport-jwt';
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor() {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
ignoreExpiration: false,
secretOrKey: `${process.env.JWT_SECRET_KEY}`,
});
}
아래와 같이 변경하자
process.env.JWT_SECRET_KEY ====> `${process.env.JWT_SECRET_KEY}`
라고 생각했는데, 실제로 해결된 것은 아니었다.
진짜 해결 방법은 간단했다.
app.module.ts 뿐만 아니라 auth.module.ts 에도 config 설정을 해주어야 했다
imports: [
ConfigModule.forRoot({
envFilePath:
process.env.NODE_ENV === 'production' ? '.env' : '.env.development',
}),
]
...
참고)
https://dev.to/emmanuelthecoder/how-to-solve-secretorprivatekey-must-have-a-value-in-nodejs-4mpg
How to solve "secretOrPrivateKey must have a value" in Node.js
I was writing an awesome stuff with Express today and I ran into an error, after solving it (thanks...
dev.to