The login endpoint was returning a 500 Internal Server Error due to:
- Poor error handling in the login route
- Potential MongoDB connection issues in serverless environment
- Using synchronous bcrypt methods that may fail in serverless
- Added input validation for username and password
- Added MongoDB connection state check before processing
- Changed from
bcrypt.compareSync()to asyncbcrypt.compare() - Added comprehensive error logging
- Added explicit return statements to prevent multiple responses
- Better error messages for debugging
- Added connection options optimized for serverless (Vercel)
- Added connection event handlers (error, disconnected, reconnected)
- Reduced
serverSelectionTimeoutMSfrom 30s to 5s - Added
socketTimeoutMSof 45s
- New endpoint:
GET /api/health - Returns database connection status
- Helps diagnose deployment issues
- Shows user count when DB is connected
Make sure these environment variables are set in your Vercel project:
MONGO_URI=mongodb+srv://jenilrupapara340_db_user:gPaASk6ZOa4Wa44L@sample-data.vyal4lo.mongodb.net/bavadiya-realty?appName=Sample-Data
JWT_SECRET=your_super_secret_jwt_key_change_this_in_production_123456789
NODE_ENV=production
DEFAULT_ADMIN_USERNAME=DharmeshBavadiya
DEFAULT_ADMIN_PASSWORD=BavadiyaRealtyAdmin!2024
DEFAULT_ADMIN_EMAIL=admin@bavadiyarealty.com
BCRYPT_ROUNDS=12
JWT_EXPIRES_IN=24h
ALLOWED_ORIGINS=https://bavadiyarealty.vercel.app
PORT=3000cd backend
vercel --prodgit add .
git commit -m "Fix: Enhanced login endpoint with better error handling"
git push origin maincurl https://bavadiya-realty-backend.vercel.app/api/healthExpected response:
{
"status": "healthy",
"timestamp": "2025-11-19T12:40:00.000Z",
"database": {
"state": "connected",
"stateCode": 1,
"userCount": 1
},
"environment": "production"
}curl -X POST https://bavadiya-realty-backend.vercel.app/api/login \
-H "Content-Type: application/json" \
-d '{"username":"DharmeshBavadiya","password":"BavadiyaRealtyAdmin!2024"}'Expected response:
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"user": {
"_id": "...",
"username": "DharmeshBavadiya",
"fullName": "Dharmesh Bavadiya",
"email": "admin@bavadiyarealty.com",
"role": "Admin",
...
}
}If issues persist:
- Go to Vercel Dashboard
- Select your backend project
- Click on "Deployments"
- Click on the latest deployment
- View "Functions" logs to see error messages
Symptom: Health check shows "connecting" or "disconnected" Solution:
- Verify MONGO_URI is correct in Vercel environment variables
- Check MongoDB Atlas network access allows Vercel IPs (0.0.0.0/0)
- Ensure MongoDB cluster is not paused
Symptom: Frontend can't reach backend Solution:
- Verify ALLOWED_ORIGINS includes your frontend URL
- No trailing slash in the origin URL
Symptom: Login returns "Invalid credentials" but password is correct Solution:
- Check health endpoint to see userCount
- If userCount is 0, the default admin wasn't created
- Manually create admin user via MongoDB Atlas
Symptom: Login succeeds but subsequent requests fail Solution:
- Verify JWT_SECRET is set in Vercel
- Check JWT_EXPIRES_IN is valid (e.g., "24h", "7d")
vercel logs bavadiya-realty-backend --prodSet up a cron job or monitoring service to ping:
https://bavadiya-realty-backend.vercel.app/api/health
If the new deployment causes issues:
- Go to Vercel Dashboard
- Find the previous working deployment
- Click "Promote to Production"
- Deploy the updated backend
- Test the health endpoint
- Test login from frontend
- Monitor logs for any errors
- Consider adding more detailed logging if issues persist
If you continue to experience issues:
- Check the health endpoint response
- Review Vercel function logs
- Verify all environment variables are set
- Test MongoDB connection directly from MongoDB Atlas