api-server/src/config/jwt.config.ts

22 lines
736 B
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { registerAs } from '@nestjs/config';
export default registerAs('jwt', () => {
const secret = process.env.JWT_SECRET;
if (!secret || secret === 'change_me_in_production') {
if (process.env.NODE_ENV === 'production') {
throw new Error(
'生产环境必须设置环境变量 JWT_SECRET不能使用默认值',
);
}
console.warn(
'\n⚠ 警告: JWT_SECRET 使用的是默认值 "change_me_in_production"\n' +
' 部署到生产环境前请务必设置环境变量 JWT_SECRET\n',
);
}
return {
secret: secret || 'change_me_in_production',
expiresIn: process.env.JWT_EXPIRES_IN || '1h',
refreshExpiresIn: process.env.JWT_REFRESH_EXPIRES_IN || '7d',
};
});