mch*_*mvu 22 mysql node.js express
我正在使用 jwt 创建令牌,但是当我通过邮递员登录时,我从控制台收到错误“错误:secretOrPrivateKey 必须有一个值”。我附上了我的登录代码。请任何可以帮助我的人
exports.login = (req, res, next) => {
User.findOne({
where: {
email: req.body.email
}
})
.then(user => {
if (!user) {
return res.status(401).json({
message:
"Auth failed!! either the account does't exist or you entered a wrong account"
});
}
bcrypt.compare(req.body.password, user.password, (err, result) => {
if (err) {
return res.status(401).json({
message: "Auth failed",
token: token
});
}
if (result) {
const token = jwt.sign(
{
email: user.email,
password: user.id
},
process.env.JWT_KEY,
{
expiresIn: "1h"
}
);
res.status(200).json({
message: "Auth granted, welcome!",
token: token
});
}
});
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
});
};
Run Code Online (Sandbox Code Playgroud)
这是我的 env.json 文件
{
"env":{
"MYSQL":"jllgshllWEUJHGHYJkjsfjds90",
"JWT_KEY": "secret"
}
}
Run Code Online (Sandbox Code Playgroud)
Sul*_*Sah 30
您的应用程序似乎无法正确读取环境变量。
我不知道您使用哪个包来加载环境变量,但最简单的方法是使用dotenv包。
使用 npm i dotenv 安装后,尽早在应用程序主文件中导入它,如下所示:
require("dotenv").config();
Run Code Online (Sandbox Code Playgroud)
使用此内容在您的应用程序根文件夹中创建 .env 文件(如您所见,格式为 key=value)
MYSQL=jllgshllWEUJHGHYJkjsfjds90
JWT_KEY=secret
Run Code Online (Sandbox Code Playgroud)
然后,您可以像之前一样访问它们的值:
process.env.JWT_KEY
Run Code Online (Sandbox Code Playgroud)
.env 文件:
thi*_*ign 20
在尝试依赖 NestJS 时遇到了这个问题process.env.X
。据说在后台@nestjs/config
使用dotenv
,但它没有按预期工作。我必须在给定文件中使用ConfigService
或显式配置:dotenv
jwt.strategy.ts
import * as dotenv from 'dotenv';
dotenv.config();
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor() {
super({
secretOrKey: process.env.JWT_SECRET,
});
}
}
Run Code Online (Sandbox Code Playgroud)
或者
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor(configService: ConfigService) {
super({
secretOrKey: configService.get<string>('JWT_SECRET'),
});
}
}
Run Code Online (Sandbox Code Playgroud)
auth.module.ts
import * as dotenv from 'dotenv';
dotenv.config();
@Module({
imports: [
JwtModule.register({
secret: process.env.JWT_SECRET,
}),
],
})
Run Code Online (Sandbox Code Playgroud)
或者
@Module({
imports: [
JwtModule.registerAsync({
imports: [ConfigModule]
useFactory: async (configService: ConfigService) => {
return {
secret: configService.get<string>('JWT_SECRET'),
};
},
inject: [ConfigService],
}),
],
})
Run Code Online (Sandbox Code Playgroud)
Lei*_*ish 16
它意外地JWTService
在我的AuthModule
. 这似乎覆盖了我在注册JWTModule
.
我之前有过什么:
@Module({
imports: [
PassportModule,
JwtModule.registerAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (configService: ConfigService) => {
const authConfig = configService.get<AuthenticationConfig>(
PathNames.AUTH,
);
return {
secret: authConfig.accessToken.secret,
signOptions: {
expiresIn: authConfig.accessToken.expiryTime,
},
};
},
}),
],
providers: [
AuthService,
UsersService,
PrismaService,
AuthResolver,
// this was the problem
JwtService,
JWTStrategy,
],
exports: [AuthService],
})
export class AuthModule {}
Run Code Online (Sandbox Code Playgroud)
Rab*_*suf 13
删除 process.env.JWT_SECRET_KEY 并这样做:${process.env.JWT_SECRET_KEY}
用反引号包裹它。它为我解决了这个问题。
小智 10
只有当我将它与这样的空字符串连接时,它才对我有用:
"" + process.env.JWT_KEY
Run Code Online (Sandbox Code Playgroud)
小智 6
只需删除 process.env.JWT_KEY 并替换为“secret key using” ES6 String Literals
${process.env.JWT_SECRET_KEY}
它为我解决了这个问题