# Admin Routes 404 Issue - Proxy Configuration Fix ## ๐ŸŽฏ Problem Identified The admin routes were returning **404 Not Found** errors even though: - โœ… Backend was running - โœ… User was authenticated as admin - โœ… Routes were registered in the backend ## ๐Ÿ” Root Cause The issue was in the **AdminDebugPanel.vue** component. It was making API calls with: ```javascript // WRONG - Bypasses Vite proxy const response = await axios.get('/health', { baseURL: 'http://localhost:3000', // โŒ Direct backend URL timeout: 5000, }); ``` ### Why This Caused 404 Errors 1. **Vite Proxy Configuration** (`vite.config.js`): ```javascript proxy: { "/api": { target: "http://localhost:3000", changeOrigin: true, } } ``` - This tells Vite: "When you see `/api/*`, proxy it to `http://localhost:3000/api/*`" 2. **Axios Configuration** (`frontend/src/utils/axios.js`): ```javascript baseURL: import.meta.env.VITE_API_URL || '/api' ``` - Default baseURL is `/api` - This works with the Vite proxy 3. **AdminDebugPanel Issue**: - It was overriding the baseURL to `http://localhost:3000` - This bypassed the Vite proxy completely - Requests went directly to backend, missing the `/api` prefix - Backend routes are at `/api/admin/*`, not `/admin/*` - Result: 404 Not Found ## ๐Ÿ“Š Request Flow Comparison ### โŒ Before (Broken) ``` Frontend Request: GET /admin/config โ†“ AdminDebugPanel override: baseURL = 'http://localhost:3000' โ†“ Actual request: http://localhost:3000/admin/config โ†“ Backend looks for: /admin/config (doesn't exist) โ†“ Result: 404 Not Found ``` ### โœ… After (Fixed) ``` Frontend Request: GET /admin/config โ†“ Axios baseURL: /api โ†“ Full path: /api/admin/config โ†“ Vite Proxy intercepts: /api/* โ†“ Proxies to: http://localhost:3000/api/admin/config โ†“ Backend route exists: /api/admin/config โœ… โ†“ Result: 200 OK ``` ## ๐Ÿ”ง The Fix Changed AdminDebugPanel to use the same axios configuration as the rest of the app: ```javascript // โœ… CORRECT - Uses Vite proxy const response = await axios.get('/health', { timeout: 5000, // No baseURL override - uses default from axios.js }); ``` Now the requests flow through the Vite proxy correctly: - `/admin/config` โ†’ axios adds `/api` โ†’ `/api/admin/config` โ†’ proxy โ†’ backend โœ… ## ๐ŸŽ“ Key Lessons ### 1. Never Override baseURL in Components **โŒ Bad:** ```javascript await axios.get('/endpoint', { baseURL: 'http://localhost:3000' }) ``` **โœ… Good:** ```javascript await axios.get('/endpoint') // Uses configured baseURL ``` ### 2. Understand the Request Path In development with Vite: ``` Component: /admin/config โ†“ (axios adds baseURL) Axios: /api/admin/config โ†“ (Vite proxy intercepts /api/*) Proxy: http://localhost:3000/api/admin/config โ†“ Backend: Receives /api/admin/config โœ… ``` ### 3. Production vs Development **Development (with Vite proxy):** - Frontend: `http://localhost:5173` - Backend: `http://localhost:3000` - Requests: `/api/*` โ†’ proxied to backend - CORS: No issues (same origin due to proxy) **Production (no proxy):** - Frontend: `https://yourdomain.com` - Backend: `https://yourdomain.com/api` or separate domain - Requests: Direct to backend - CORS: Must be configured on backend ## ๐Ÿ“‹ Checklist for Similar Issues If you encounter 404 errors with API routes: - [ ] Check if routes are registered in backend - [ ] Check if server was restarted after adding routes - [ ] Check the actual URL being requested (Network tab) - [ ] Verify Vite proxy configuration - [ ] Verify axios baseURL configuration - [ ] Check for baseURL overrides in components - [ ] Ensure `/api` prefix is consistent - [ ] Test with curl to verify backend routes exist ## ๐Ÿงช Testing the Fix ### 1. Check Network Tab Open DevTools (F12) โ†’ Network tab: - URL should be: `http://localhost:5173/api/admin/config` - Status should be: 200 OK (not 404) ### 2. Use Debug Panel Navigate to `/admin` โ†’ Debug tab โ†’ Run Tests: - All 4 tests should pass โœ… - No 404 errors ### 3. Manual Test ```bash # This should work (proxied through Vite) curl http://localhost:5173/api/admin/config # This won't work in development (no /api prefix) curl http://localhost:3000/admin/config # 404 # This will work (correct backend path) curl http://localhost:3000/api/admin/config ``` ## ๐Ÿ“ Configuration Summary ### Vite Proxy (vite.config.js) ```javascript proxy: { "/api": { target: "http://localhost:3000", changeOrigin: true, } } ``` ### Axios Base URL (frontend/src/utils/axios.js) ```javascript baseURL: import.meta.env.VITE_API_URL || '/api' ``` ### Backend Routes (index.js) ```javascript await fastify.register(adminManagementRoutes, { prefix: "/api/admin" }); ``` ### Component Usage ```javascript // โœ… Correct - No baseURL override await axios.get('/admin/config') // Becomes: /api/admin/config via proxy // โŒ Wrong - Overrides baseURL await axios.get('/admin/config', { baseURL: 'http://localhost:3000' }) // Becomes: http://localhost:3000/admin/config (bypasses proxy, 404) ``` ## ๐ŸŽ‰ Result After the fix: - โœ… All admin routes accessible - โœ… Debug panel tests pass - โœ… Config tab loads settings - โœ… Users tab works - โœ… No 404 errors - โœ… Proxy working correctly ## ๐Ÿ”œ Prevention To prevent this issue in the future: 1. **Establish a pattern:** - Always use the shared axios instance from `@/utils/axios` - Never override baseURL in components - Let the configuration handle URL construction 2. **Code review checklist:** - Check for baseURL overrides - Verify proxy paths are correct - Test API routes after adding new ones 3. **Documentation:** - Document the request flow - Explain proxy configuration - Provide examples of correct usage ## ๐Ÿ“š Related Files - `frontend/vite.config.js` - Proxy configuration - `frontend/src/utils/axios.js` - Axios setup - `frontend/src/components/AdminDebugPanel.vue` - Fixed component - `index.js` - Backend route registration - `routes/admin-management.js` - Admin routes --- **Issue:** Admin routes returning 404 **Cause:** baseURL override bypassing Vite proxy **Solution:** Remove baseURL override, use default configuration **Status:** โœ… Fixed and tested **Build:** โœ… Successful