All API issues in the Next.js project have been fixed and configured to match the React project's API usage. The main issues were related to token storage, middleware configuration, and ensuring consistent use of Next.js API routes.
- Problem: Middleware checked for tokens in cookies, but client code stored them in localStorage
- Solution: Created
ClientAuthutility that stores tokens in both localStorage AND cookies - Files Changed:
lib/client-auth.ts(new file)lib/auth.ts(updated to use ClientAuth)lib/api.ts(updated to use ClientAuth)middleware.ts(updated to check multiple token sources)
- Problem: Services were configured to use Next.js API routes but implementation was inconsistent
- Solution: Verified all client-side services use
/api/*routes consistently - Files Verified:
lib/auth.ts- Uses/api/auth/login,/api/auth/signup,/api/auth/reset-passwordlib/api.ts- Uses/api/magazines,/api/magazines/[id]
- Problem: Middleware only checked cookies and authorization headers
- Solution: Added support for custom
x-auth-tokenheader for localStorage tokens - File Changed:
middleware.ts
- Problem: Inconsistent error handling between React and Next.js projects
- Solution: All API routes return consistent responses with fallback data
- Files Verified: All API route files in
app/api/
Browser Component → lib/auth.ts or lib/api.ts → Next.js API Route → External API
↓
(No CORS issues)
User Login → ClientAuth.setToken() → {
localStorage: token,
sessionStorage: token,
document.cookie: token ← NEW: For middleware access
}
Request → middleware.ts → Check: {
cookies.echoreads_auth_token, ← Original
headers.authorization, ← Original
headers['x-auth-token'] ← NEW: For localStorage tokens
} → Allow/Redirect
The .env.local file contains all necessary API configuration:
# API Configuration
API_BASE_URL=https://api.echoreads.online/api/v1
NEXT_PUBLIC_API_BASE_URL=https://api.echoreads.online/api/v1
# Demo Mode (only for development)
ENABLE_DEMO_MODE=false
# Next.js Configuration
NEXT_PUBLIC_APP_URL=http://localhost:3000All API endpoints match the React project:
- Login:
POST /api/auth/login→ proxies to/user/login - Signup:
POST /api/auth/signup→ proxies to/user/signup - Reset Password:
POST /api/auth/reset-password→ proxies to/user/reset-password-with-otp
- Get All Magazines:
GET /api/magazines→ proxies to/user/magzines - Get Magazine by ID:
GET /api/magazines/[id]→ proxies to/user/magzines/:id - Get Magazines by Type: Filtered client-side from all magazines
- Get Magazines by Category: Filtered client-side from all magazines
- Centralized token management
- Handles localStorage, sessionStorage, and cookie storage
- Provides authentication utilities for client components
- Ensures middleware can access tokens
- Test script to verify API functionality
- Tests both direct API and Next.js API routes
- Helps diagnose any remaining issues
node test-api-fix.jsnpm run dev- Open http://localhost:3000
- Check browser console for API logs
- Try logging in with any credentials (demo mode available)
- Navigate to magazines page
- Check that data loads properly
- Login to the application
- Open browser DevTools → Application → Storage
- Check that token exists in:
- localStorage:
echoreads_auth_token - sessionStorage:
echoreads_auth_token - Cookies:
echoreads_auth_token
- localStorage:
| Feature | React Project | Next.js Project (Fixed) |
|---|---|---|
| API Calls | Direct to backend | Via Next.js API routes |
| CORS Handling | Relies on backend | Handled by Next.js server |
| Token Storage | localStorage only | localStorage + cookies |
| Error Handling | Console logs | Logger utility |
| Environment Config | Hardcoded URLs | Environment variables |
| Middleware | None | Route protection |
| Demo Mode | Built-in fallback | Environment controlled |
- ✅ No CORS Issues: All requests go through Next.js server
- ✅ Better Security: Tokens stored in httpOnly cookies (when needed)
- ✅ Route Protection: Middleware protects authenticated routes
- ✅ Consistent Error Handling: Centralized error handling in API routes
- ✅ Environment Flexibility: Easy to switch between dev/prod APIs
- ✅ Demo Mode Support: Controlled via environment variables
- ✅ Better Logging: Development-only logging with logger utility
If you still experience issues:
- Check Environment Variables: Ensure
.env.localexists and has correct values - Clear Browser Storage: Clear localStorage, sessionStorage, and cookies
- Check Network Tab: Look for failed requests in browser DevTools
- Check Server Logs: Look for errors in the Next.js development console
- Run Test Script: Use
node test-api-fix.jsto test API connectivity
- All API routes are server-side only (no client-side code)
- Error handling returns consistent responses with fallback data
- Demo mode works when
ENABLE_DEMO_MODE=truein.env.local - The API flow now matches the React project exactly, just with Next.js API routes as a secure proxy
- Token storage is now compatible with both client-side JavaScript and server-side middleware