fix(auth): JWT_SECRET is required

This commit is contained in:
tigattack
2025-10-01 23:48:13 +01:00
parent 9ddc27e50c
commit 9c39d83fe5
3 changed files with 16 additions and 10 deletions

View File

@@ -18,10 +18,10 @@ const authenticateToken = async (req, res, next) => {
} }
// Verify token // Verify token
const decoded = jwt.verify( if (!process.env.JWT_SECRET) {
token, throw new Error("JWT_SECRET environment variable is required");
process.env.JWT_SECRET || "your-secret-key", }
); const decoded = jwt.verify(token, process.env.JWT_SECRET);
// Validate session and check inactivity timeout // Validate session and check inactivity timeout
const validation = await validate_session(decoded.sessionId, token); const validation = await validate_session(decoded.sessionId, token);
@@ -85,10 +85,10 @@ const optionalAuth = async (req, _res, next) => {
const token = authHeader?.split(" ")[1]; const token = authHeader?.split(" ")[1];
if (token) { if (token) {
const decoded = jwt.verify( if (!process.env.JWT_SECRET) {
token, throw new Error("JWT_SECRET environment variable is required");
process.env.JWT_SECRET || "your-secret-key", }
); const decoded = jwt.verify(token, process.env.JWT_SECRET);
const user = await prisma.users.findUnique({ const user = await prisma.users.findUnique({
where: { id: decoded.userId }, where: { id: decoded.userId },
select: { select: {

View File

@@ -156,7 +156,10 @@ router.post(
// Generate JWT token // Generate JWT token
const generateToken = (userId) => { const generateToken = (userId) => {
return jwt.sign({ userId }, process.env.JWT_SECRET || "your-secret-key", { if (!process.env.JWT_SECRET) {
throw new Error("JWT_SECRET environment variable is required");
}
return jwt.sign({ userId }, process.env.JWT_SECRET, {
expiresIn: process.env.JWT_EXPIRES_IN || "24h", expiresIn: process.env.JWT_EXPIRES_IN || "24h",
}); });
}; };

View File

@@ -9,7 +9,10 @@ const prisma = new PrismaClient();
*/ */
// Configuration // Configuration
const JWT_SECRET = process.env.JWT_SECRET || "your-secret-key"; if (!process.env.JWT_SECRET) {
throw new Error("JWT_SECRET environment variable is required");
}
const JWT_SECRET = process.env.JWT_SECRET;
const JWT_EXPIRES_IN = process.env.JWT_EXPIRES_IN || "1h"; const JWT_EXPIRES_IN = process.env.JWT_EXPIRES_IN || "1h";
const JWT_REFRESH_EXPIRES_IN = process.env.JWT_REFRESH_EXPIRES_IN || "7d"; const JWT_REFRESH_EXPIRES_IN = process.env.JWT_REFRESH_EXPIRES_IN || "7d";
const INACTIVITY_TIMEOUT_MINUTES = parseInt( const INACTIVITY_TIMEOUT_MINUTES = parseInt(