Skip to content

Commit bbdf16b

Browse files
Allow weak JWT_SECRET in development, require strong secret in production
1 parent 313c009 commit bbdf16b

4 files changed

Lines changed: 20 additions & 16 deletions

File tree

backend/dist/middleware/auth.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
66
exports.authMiddleware = void 0;
77
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
88
const logger_1 = __importDefault(require("../utils/logger"));
9-
const JWT_SECRET = process.env.JWT_SECRET;
10-
if (!JWT_SECRET) {
11-
logger_1.default.error('JWT_SECRET is not set in environment variables! Authentication will not work.');
12-
throw new Error('JWT_SECRET must be set in environment variables');
9+
const JWT_SECRET = process.env.JWT_SECRET || 'dev_secret_key_for_local_development';
10+
// In production, JWT_SECRET must be properly set
11+
if (process.env.NODE_ENV === 'production' && (!JWT_SECRET || JWT_SECRET === 'dev_secret_key_for_local_development')) {
12+
logger_1.default.error('JWT_SECRET is not set in production environment! Authentication will not work.');
13+
throw new Error('JWT_SECRET must be set in production environment variables');
1314
}
1415
const authMiddleware = (req, res, next) => {
1516
const authHeader = req.headers.authorization;

backend/dist/resolvers/userResolver.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ const bcrypt_1 = __importDefault(require("bcrypt"));
77
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
88
const User_1 = __importDefault(require("../models/User"));
99
const logger_1 = __importDefault(require("../utils/logger"));
10-
const JWT_SECRET = process.env.JWT_SECRET;
11-
if (!JWT_SECRET) {
12-
logger_1.default.error('JWT_SECRET is not set in environment variables!');
13-
throw new Error('JWT_SECRET must be set in environment variables');
10+
const JWT_SECRET = process.env.JWT_SECRET || 'dev_secret_key_for_local_development';
11+
// In production, JWT_SECRET must be properly set
12+
if (process.env.NODE_ENV === 'production' && (!JWT_SECRET || JWT_SECRET === 'dev_secret_key_for_local_development')) {
13+
logger_1.default.error('JWT_SECRET is not set in production environment!');
14+
throw new Error('JWT_SECRET must be set in production environment variables');
1415
}
1516
const userResolver = {
1617
Query: {

backend/src/middleware/auth.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import { Request, Response, NextFunction } from 'express';
22
import jwt from 'jsonwebtoken';
33
import logger from '../utils/logger';
44

5-
const JWT_SECRET = process.env.JWT_SECRET;
5+
const JWT_SECRET = process.env.JWT_SECRET || 'dev_secret_key_for_local_development';
66

7-
if (!JWT_SECRET) {
8-
logger.error('JWT_SECRET is not set in environment variables! Authentication will not work.');
9-
throw new Error('JWT_SECRET must be set in environment variables');
7+
// In production, JWT_SECRET must be properly set
8+
if (process.env.NODE_ENV === 'production' && (!JWT_SECRET || JWT_SECRET === 'dev_secret_key_for_local_development')) {
9+
logger.error('JWT_SECRET is not set in production environment! Authentication will not work.');
10+
throw new Error('JWT_SECRET must be set in production environment variables');
1011
}
1112

1213
export interface AuthRequest extends Request {

backend/src/resolvers/userResolver.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import { IUser } from '../models/User';
55
import { AuthRequest } from '../middleware/auth';
66
import logger from '../utils/logger';
77

8-
const JWT_SECRET = process.env.JWT_SECRET;
8+
const JWT_SECRET = process.env.JWT_SECRET || 'dev_secret_key_for_local_development';
99

10-
if (!JWT_SECRET) {
11-
logger.error('JWT_SECRET is not set in environment variables!');
12-
throw new Error('JWT_SECRET must be set in environment variables');
10+
// In production, JWT_SECRET must be properly set
11+
if (process.env.NODE_ENV === 'production' && (!JWT_SECRET || JWT_SECRET === 'dev_secret_key_for_local_development')) {
12+
logger.error('JWT_SECRET is not set in production environment!');
13+
throw new Error('JWT_SECRET must be set in production environment variables');
1314
}
1415

1516
const userResolver = {

0 commit comments

Comments
 (0)