Problem: Frontend was using inconsistent environment variable names (NEXT_API_PUBLIC_URL vs NEXT_PUBLIC_API_URL)
Solution:
- Updated
.envfile to use consistent naming - Updated API routes to use
NEXT_PUBLIC_SERVER_URLandNEXT_PUBLIC_API_URL
Problem: Two different AuthProvider implementations causing conflicts Solution:
- Consolidated to a single AuthProvider in
components/providers/auth-provider.tsx - Removed duplicate
hooks/use-auth.tsx(renamed to backup) - Added proper TypeScript interfaces and error handling
Problem: Mixed usage of direct backend calls and Next.js API routes in use-user-api.ts
Solution:
- Updated all API calls to use consistent Next.js API route pattern (
/api/...) - Removed the generic
makeRequestfunction - Added proper error handling and token management for all endpoints
Problem: Some API calls weren't properly sending authorization headers Solution:
- Standardized authorization header format:
Authorization: Bearer ${token} - Added proper token validation in all API routes
- Consistent error responses for unauthorized requests
Problem: Inconsistent error handling across API routes Solution:
- Standardized error response format
- Added proper HTTP status codes
- Improved error messages and logging
.env- Fixed environment variable nameshooks/use-user-api.ts- Complete rewrite for consistencycomponents/providers/auth-provider.tsx- Consolidated auth providerapp/api/user/verify-token/route.ts- Fixed environment variable usageapp/api/user/profile/[userId]/route.ts- Added profile management routesapp/api/user/profile/[userId]/complete/route.ts- Fixed error handlingapp/api/user/profile/[userId]/projects/route.ts- Fixed error handlingapp/api/upload/single/route.ts- Created upload routeapp/api/upload/multiple/route.ts- Created multiple upload routeapp/api/upload/[filename]/route.ts- Created delete file route
- Environment Variables: Consistent naming between frontend and backend
- CORS: Backend already configured to accept frontend requests
- Rate Limiting: Backend has proper rate limiting for different endpoint types
The application now follows this pattern:
Frontend Component β useUserAPI Hook β Next.js API Route β Backend Express Server
- User logs in through
/api/user/login - JWT token stored in localStorage
- All subsequent API calls include
Authorization: Bearer ${token}header - Next.js API routes forward requests to backend with proper authentication
- Backend validates JWT and processes requests
/api/user/
βββ login (POST)
βββ verify-token (GET)
βββ complete-profile (POST)
βββ profile/[userId]/
β βββ route.ts (GET, PUT)
β βββ complete/ (GET)
β βββ projects/ (GET, POST)
β βββ experiences/ (GET, POST)
β βββ achievements/ (GET, POST)
β βββ certifications/ (GET, POST)
β βββ profile-links/ (GET, POST)
βββ projects/[projectId]/ (PUT, DELETE)
βββ experiences/[experienceId]/ (PUT, DELETE)
βββ achievements/[achievementId]/ (PUT, DELETE)
βββ certifications/[certificationId]/ (PUT, DELETE)
βββ profile-links/[profileLinkId]/ (PUT, DELETE)
/api/upload/
βββ single (POST)
βββ multiple (POST)
βββ [filename] (DELETE)
β Frontend Development Server: Running on http://localhost:3000 β³ Backend Server: Needs to be started on http://localhost:5000 β³ Database: MongoDB connection needs verification
-
Start Backend Server:
cd server npm start -
Verify Database Connection:
- Check MongoDB is running (local or Atlas)
- Verify connection string in server/.env
-
Test API Endpoints:
- Login functionality
- Profile management
- File upload
- CRUD operations for projects, experiences, etc.
-
Production Deployment:
- Update environment variables for production URLs
- Configure CORS for production domains
- Set up proper SSL certificates
- Type Safety: Proper TypeScript interfaces throughout
- Error Handling: Comprehensive error handling with meaningful messages
- Security: Proper JWT token validation and secure headers
- Consistency: Uniform API patterns and response formats
- Maintainability: Clean separation of concerns and consistent code style