Skip to content

Latest commit

 

History

History
190 lines (148 loc) · 5.91 KB

File metadata and controls

190 lines (148 loc) · 5.91 KB

Frontend-Backend Integration Fix for Render Deployment

Problem Summary

The deployed frontend on Render was unable to communicate with the backend API, resulting in CORS (Cross-Origin Resource Sharing) errors that prevented any API requests from succeeding.

Root Cause

The backend's CORS middleware configuration only allowed requests from:

  • https://hackquest-ai.me
  • https://www.hackquest-ai.me

However, the frontend was deployed on Render at:

  • https://hackquest-ai-1.onrender.com

This URL mismatch caused the browser to block all requests due to CORS policy restrictions.

Solution Implemented

1. Updated Backend CORS Configuration

File: backend/app/main.py Lines: 101-105

Before:

origins = [
    "https://hackquest-ai.me",
    "https://www.hackquest-ai.me",
]

After:

origins = [
    "https://hackquest-ai.me",
    "https://www.hackquest-ai.me",
    "https://hackquest-ai-1.onrender.com",  # Render deployed frontend
    "http://localhost:5173",                 # Development environment
]

2. Verified Frontend Configuration

File: frontend/.env.production

# Backend API Configuration (Render deployment)
VITE_API_URL=https://hackquest-ai-1.onrender.com
VITE_WS_URL=wss://hackquest-ai-1.onrender.com

This configuration is already correct and properly points to the Render backend.

Deployment Architecture

┌─────────────────────────────────────────────────┐
│   PRODUCTION DEPLOYMENT (Render)                 │
├─────────────────────────────────────────────────┤
│                                                  │
│  Frontend (Vite + React)                        │
│  https://hackquest-ai-1.onrender.com            │
│  ↓ (API Requests)                               │
│  Backend (FastAPI)                              │
│  https://hackquest-ai-1.onrender.com            │
│  ↓ (Database Queries)                           │
│  MongoDB Atlas (Cloud Database)                 │
│                                                  │
└─────────────────────────────────────────────────┘

Key Environment Variables

Frontend Build Configuration

  • VITE_API_URL: https://hackquest-ai-1.onrender.com - Backend API base URL
  • VITE_WS_URL: wss://hackquest-ai-1.onrender.com - WebSocket secure URL
  • VITE_ENV: production
  • VITE_ENABLE_DEBUG: false

Backend Render Configuration

  • ENVIRONMENT: production
  • DEBUG: False
  • MONGODB_URL: mongodb+srv://[user]:[password]@[cluster].mongodb.net/?retryWrites=true&w=majority&tls=true
  • MONGODB_DB: hackquest

CORS Security Configuration

The backend now properly allows requests from multiple origins while maintaining security:

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,           # Only specific domains
    allow_credentials=True,          # Allow cookies/auth headers
    allow_methods=["*"],            # All HTTP methods
    allow_headers=["*"],            # All headers
    max_age=3600                     # Cache CORS 1 hour
)

Security Features:

Not using allow_origins=["*"] - Insecure in production ✅ Explicitly lists only real frontend domainsAllows credentials - Needed for authentication ✅ HTTPS/WSS only - All URLs use secure protocols ✅ Production debug disabled - DEBUG=False in production

Testing & Verification

Frontend Testing

  1. Open DevTools (F12) → Network tab
  2. Trigger an API call (e.g., login, fetch data)
  3. Expected: Request URL starts with https://hackquest-ai-1.onrender.com/api/
  4. Check: No CORS errors in Console tab

Backend Health Check

curl https://hackquest-ai-1.onrender.com/api/health

Expected response:

{
  "status": "healthy",
  "service": "hackquest-ai-backend",
  "version": "1.0.0",
  "environment": "production",
  "database": "connected"
}

CORS Validation

curl -H "Origin: https://hackquest-ai-1.onrender.com" \
     -H "Access-Control-Request-Method: GET" \
     https://hackquest-ai-1.onrender.com/api/health -v

Expected response headers:

Access-Control-Allow-Origin: https://hackquest-ai-1.onrender.com

Commit Changes

Commit Hash: See recent commits in this repository

Changes Made:

  • ✅ Added https://hackquest-ai-1.onrender.com to CORS allowed origins
  • ✅ Added http://localhost:5173 for local development support
  • ✅ Updated CORS logging to show configured origins

Troubleshooting

If CORS errors persist:

  1. Clear browser cache

    # Hard refresh in browser
    Ctrl+Shift+R (Windows/Linux)
    Cmd+Shift+R (Mac)
  2. Verify backend deployment

    • Check Render dashboard logs for any startup errors
    • Ensure CORS configuration was deployed (redeploy if needed)
  3. Check frontend build

    # Rebuild frontend with correct environment
    cd frontend
    npm run build
    # Verify dist/ contains correct API URL
  4. Monitor logs

    • Render Backend Logs: Watch for CORS middleware messages
    • Browser Console: Check for detailed CORS error messages
    • Network Tab: Inspect request/response headers

Summary

This fix ensures that the frontend and backend can communicate properly on Render's deployment platform by:

  1. ✅ Adding the Render frontend URL to the CORS allowed origins list
  2. ✅ Supporting local development with localhost
  3. ✅ Maintaining production security best practices
  4. ✅ Properly configuring environment variables in both applications

The integration is now complete and the frontend-backend communication should work seamlessly on Render.