Skip to content

Latest commit

 

History

History
174 lines (146 loc) · 4.63 KB

File metadata and controls

174 lines (146 loc) · 4.63 KB

Deployment Fix Guide - Login 500 Error

Issue Identified

The login endpoint was returning a 500 Internal Server Error due to:

  1. Poor error handling in the login route
  2. Potential MongoDB connection issues in serverless environment
  3. Using synchronous bcrypt methods that may fail in serverless

Changes Made

1. Enhanced Login Endpoint (/api/login)

  • Added input validation for username and password
  • Added MongoDB connection state check before processing
  • Changed from bcrypt.compareSync() to async bcrypt.compare()
  • Added comprehensive error logging
  • Added explicit return statements to prevent multiple responses
  • Better error messages for debugging

2. Improved MongoDB Connection

  • Added connection options optimized for serverless (Vercel)
  • Added connection event handlers (error, disconnected, reconnected)
  • Reduced serverSelectionTimeoutMS from 30s to 5s
  • Added socketTimeoutMS of 45s

3. Added Health Check Endpoint

  • New endpoint: GET /api/health
  • Returns database connection status
  • Helps diagnose deployment issues
  • Shows user count when DB is connected

Deployment Steps for Vercel

Step 1: Verify Environment Variables

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=3000

Step 2: Deploy to Vercel

Option A: Using Vercel CLI

cd backend
vercel --prod

Option B: Using Git Push

git add .
git commit -m "Fix: Enhanced login endpoint with better error handling"
git push origin main

Step 3: Test the Deployment

Test Health Check

curl https://bavadiya-realty-backend.vercel.app/api/health

Expected response:

{
  "status": "healthy",
  "timestamp": "2025-11-19T12:40:00.000Z",
  "database": {
    "state": "connected",
    "stateCode": 1,
    "userCount": 1
  },
  "environment": "production"
}

Test Login

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",
    ...
  }
}

Step 4: Check Vercel Logs

If issues persist:

  1. Go to Vercel Dashboard
  2. Select your backend project
  3. Click on "Deployments"
  4. Click on the latest deployment
  5. View "Functions" logs to see error messages

Common Issues and Solutions

Issue 1: MongoDB Connection Timeout

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

Issue 2: CORS Errors

Symptom: Frontend can't reach backend Solution:

  • Verify ALLOWED_ORIGINS includes your frontend URL
  • No trailing slash in the origin URL

Issue 3: User Not Found

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

Issue 4: JWT Token Issues

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")

Monitoring

Check Backend Logs

vercel logs bavadiya-realty-backend --prod

Monitor Health

Set up a cron job or monitoring service to ping:

https://bavadiya-realty-backend.vercel.app/api/health

Rollback Plan

If the new deployment causes issues:

  1. Go to Vercel Dashboard
  2. Find the previous working deployment
  3. Click "Promote to Production"

Next Steps

  1. Deploy the updated backend
  2. Test the health endpoint
  3. Test login from frontend
  4. Monitor logs for any errors
  5. Consider adding more detailed logging if issues persist

Support

If you continue to experience issues:

  1. Check the health endpoint response
  2. Review Vercel function logs
  3. Verify all environment variables are set
  4. Test MongoDB connection directly from MongoDB Atlas