DigitalOcean Routing Fix - Production Deployment
🚨 Issue Identified
The RISWIS application was experiencing authentication failures in production due to routing mismatches between the frontend, DigitalOcean ingress, and backend services.
Error Symptoms:
❌
Cross-Origin Request Blocked: localhost:5000
(CORS issue)❌
Route POST /auth/login not found
(404 error)❌ WebSocket connection failures
🔧 Root Cause Analysis
1. CORS Issue (Fixed)
Problem: Frontend was using
INTERNAL_BACKEND_URL
which defaulted tolocalhost:5000
in productionSolution: Updated all frontend code to use
NEXT_PUBLIC_API_URL
environment variable
2. Routing Mismatch (Fixed)
Problem: DigitalOcean App Platform ingress strips the
/api
prefix when forwarding requestsFlow:
Frontend: https://ibank.modernsavingsandloans.com/api/auth/login ↓ (DigitalOcean Ingress) Backend Receives: /auth/login (prefix stripped) ↓ (Backend Expected) Backend Routes: Mounted at /api/auth/login ❌ MISMATCH
✅ Solutions Implemented
Frontend Changes
1. Auth Context (frontend/src/app/context/auth-context.tsx
)
frontend/src/app/context/auth-context.tsx
)// OLD (causing localhost issue)
const backendUrl = process.env.INTERNAL_BACKEND_URL || 'http://localhost:5000';
// NEW (production-ready)
const backendUrl = process.env.NEXT_PUBLIC_API_URL
? process.env.NEXT_PUBLIC_API_URL.replace('/api', '')
: 'http://localhost:5000';
2. API Helpers (frontend/src/lib/api-helpers.ts
)
frontend/src/lib/api-helpers.ts
)// Updated to use NEXT_PUBLIC_API_URL consistently
export function getBackendURL(): string {
const backendUrl = process.env.NEXT_PUBLIC_API_URL
? process.env.NEXT_PUBLIC_API_URL.replace('/api', '')
: 'http://localhost:5000';
return backendUrl.endsWith('/api') ? backendUrl.slice(0, -4) : backendUrl;
}
3. Config (frontend/src/lib/config.ts
)
frontend/src/lib/config.ts
)// Updated backend URL resolution
const getBackendUrl = () => {
return process.env.NEXT_PUBLIC_API_URL
? process.env.NEXT_PUBLIC_API_URL.replace('/api', '')
: LOCAL_API;
};
4. All API Proxy Routes
Updated all proxy routes in frontend/src/app/api/proxy/
to use NEXT_PUBLIC_API_URL
.
Backend Changes
1. App Configuration (backend/src/app.ts
)
backend/src/app.ts
)// NEW: Environment-aware API prefix
const apiPrefix = process.env.NODE_ENV === 'production' ? '' : '/api';
app.use(apiPrefix, apiRoutes);
2. Server Configuration (backend/src/server.ts
)
backend/src/server.ts
)// NEW: Consistent prefix handling
const apiPrefix = process.env.NODE_ENV === 'production' ? '' : '/api';
app.use(apiPrefix, (req, res, next) => { /* middleware */ });
app.use(apiPrefix, apiRoutes);
3. Config (backend/src/config/index.ts
)
backend/src/config/index.ts
)server: {
port: process.env.PORT || 5000,
env: process.env.NODE_ENV || 'development',
// Environment-aware API prefix
apiPrefix: process.env.NODE_ENV === 'production' ? '' : '/api'
},
🌐 Environment Variables
DigitalOcean App Spec (riswis-app-spec.yaml
)
riswis-app-spec.yaml
)# Frontend Service
- key: NEXT_PUBLIC_API_URL
value: "https://ibank.modernsavingsandloans.com/api"
scope: RUN_AND_BUILD_TIME
# Backend Service
- key: NODE_ENV
value: production
scope: RUN_AND_BUILD_TIME
🔄 Request Flow (After Fix)
Production Flow ✅
1. Frontend → https://ibank.modernsavingsandloans.com/api/auth/login
2. DigitalOcean Ingress → Strips /api prefix
3. Backend Receives → /auth/login
4. Backend Routes → Mounted at / (root level in production)
5. Route Match → /auth/login ✅ SUCCESS
Development Flow ✅
1. Frontend → http://localhost:5000/api/auth/login
2. Backend Receives → /api/auth/login
3. Backend Routes → Mounted at /api (in development)
4. Route Match → /api/auth/login ✅ SUCCESS
🧪 Testing
Verification Script
Run node test-routing-fix.js
to verify the routing logic:
✅ Development environment: Routes at
/api
✅ Production environment: Routes at
/
(root)✅ Environment variable processing
✅ URL construction logic
📋 Deployment Checklist
🚀 Deployment Steps
Commit Changes
git add . git commit -m "fix: resolve DigitalOcean routing and CORS issues"
Push to Production Branch
git push origin msl-production
Monitor Deployment
DigitalOcean will automatically deploy
Check app logs for any issues
Test login functionality
🔍 Troubleshooting
If Login Still Fails:
Check DigitalOcean app logs for backend errors
Verify environment variables are set correctly
Test health endpoints:
/api/health
and/api/health-check
Check browser network tab for actual request URLs
Common Issues:
404 Errors: Backend routes not mounted correctly
CORS Errors: Frontend using wrong backend URL
500 Errors: Backend configuration or database issues
📊 Expected Results
After deployment, you should see:
✅ Login page loads without CORS errors
✅ Authentication requests succeed (200 status)
✅ No more
localhost:5000
references in production✅ Proper error handling and user feedback
Created: 2025-05-25 Status: Ready for Deployment Priority: Critical - Production Fix
Last updated