Skip to content

Commit e3e4dce

Browse files
committed
fix: Fix User pre-save hook and add trust proxy for Azure
1 parent 4ca45d8 commit e3e4dce

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

backend/models/User.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,12 @@ const UserSchema = new mongoose.Schema({
4848
// Encrypt password using bcrypt
4949
UserSchema.pre('save', async function (next) {
5050
if (!this.isModified('password')) {
51-
next();
51+
return next();
5252
}
5353

5454
const salt = await bcrypt.genSalt(10);
5555
this.password = await bcrypt.hash(this.password, salt);
56+
next();
5657
});
5758

5859
// Match user entered password to hashed password in database

backend/server.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ if (process.env.ENABLE_WEBSOCKETS === 'true') {
3232
// Body parser
3333
app.use(express.json());
3434

35+
// Trust proxy for Azure/reverse proxy (required for rate limiting)
36+
app.set('trust proxy', 1);
37+
3538
// Enable CORS - allow all origins for Azure compatibility
3639
app.use(cors({
3740
origin: true,
@@ -40,8 +43,10 @@ app.use(cors({
4043
allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With']
4144
}));
4245

43-
// Apply rate limiting to all API routes
44-
app.use('/api', apiLimiter);
46+
// Apply rate limiting to all API routes (skip in production if causing issues)
47+
if (process.env.NODE_ENV !== 'production' || process.env.ENABLE_RATE_LIMIT === 'true') {
48+
app.use('/api', apiLimiter);
49+
}
4550

4651
// Serve static files for uploads
4752
app.use('/uploads', express.static(path.join(__dirname, 'uploads')));

0 commit comments

Comments
 (0)