- Created:
frontend/.env.local- Set
NEXT_PUBLIC_API_URL=http://localhost:8000 - Frontend now knows where to find the backend API
- Set
- Features:
- Automatic JWT token injection for authenticated requests
- Automatic token refresh on 401 Unauthorized errors
- Helper functions:
apiGet(),apiPost(),apiDelete() - Error handling utilities
- Health check function
- Benefits: Centralized API communication, reduced code duplication
- Changes:
- Integrated with
api-client.ts - Simplified fetch calls
- Better error handling
- Automatic token refresh on authentication failures
- Integrated with
- Endpoints Used:
POST /api/auth/loginPOST /api/auth/registerPOST /api/auth/refreshGET /api/auth/me
- Changes:
- Integrated with
api-client.ts - Supports both streaming (SSE) and synchronous chat
- Automatic conversation creation
- Message history loading
- Export functionality
- Integrated with
- Endpoints Used:
POST /api/chat/query(streaming)POST /api/chat/query-sync(synchronous)GET /api/conversationsGET /api/conversations/{id}DELETE /api/conversations/{id}GET /api/conversations/{id}/export/{format}
- Tests:
- API connectivity test
- Authentication endpoints test
- CORS configuration test
- Test Page:
/test-integration- Visual dashboard for running tests
- Created:
INTEGRATION_GUIDE.md- Complete architecture overview
- Authentication flow diagram
- API endpoint documentation
- Streaming implementation details
- Troubleshooting guide
- Production considerations
- ✅ Backend running on
http://localhost:8000 - ✅ CORS properly configured for
http://localhost:3000 - ✅ Health endpoint responding
- ✅ All API endpoints accessible
- ✅ Frontend running on
http://localhost:3000 - ✅ Environment variables loaded
- ✅ All contexts properly configured
- ✅ UI components available
The backend .env file already has:
ALLOWED_ORIGINS=http://localhost:3000,http://localhost:8000CORS middleware is configured in backend/app/main.py:
app.add_middleware(
CORSMiddleware,
allow_origins=settings.allowed_origins_list,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)- Navigate to
http://localhost:3000/test-integration - Click "Run Tests"
- Verify all tests pass
- Go to
http://localhost:3000/loginor/register - Create an account or log in
- Navigate to the chat interface
- Send a message
- Verify streaming response works
// Test API connectivity
fetch("http://localhost:8000/health")
.then((r) => r.json())
.then(console.log);
// Test authentication (should fail with 422 - validation error)
fetch("http://localhost:8000/api/auth/login", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({}),
}).then((r) => console.log(r.status)); // Should be 422- Frontend environment configuration
- Backend CORS configuration
- API client utility created
- Auth context updated
- Chat context updated
- Integration tests created
- Test page created
- Documentation written
- Both servers running
- CORS verified
- User registration tested (manual)
- User login tested (manual)
- Chat streaming tested (manual)
- Conversation management tested (manual)
-
Register a new user:
- Go to
http://localhost:3000/register - Create an account
- Verify redirect to dashboard
- Go to
-
Test chat functionality:
- Send a message in the chat interface
- Verify streaming response
- Check that sources are displayed
- Test follow-up suggestions
-
Test conversation management:
- Create multiple conversations
- Switch between conversations
- Delete a conversation
- Export a conversation
- Error Boundaries: Add React error boundaries for better error handling
- Loading States: Improve loading indicators during API calls
- Toast Notifications: Add user feedback for actions (success/error)
- Retry Logic: Add automatic retry for failed requests
- Offline Detection: Detect when backend is unavailable
- Rate Limit Handling: Show user-friendly message when rate limited
-
Environment Variables:
- Set production API URL
- Use environment-specific configurations
-
Security:
- Consider httpOnly cookies instead of localStorage
- Implement CSRF protection
- Add request signing
-
Performance:
- Implement request caching
- Add request debouncing
- Optimize bundle size
-
Monitoring:
- Add error tracking (e.g., Sentry)
- Implement analytics
- Add performance monitoring
Symptom: Browser console shows CORS policy errors
Solution: Verify ALLOWED_ORIGINS in backend .env includes http://localhost:3000
Symptom: All requests fail with 401 Unauthorized Solution:
- Check localStorage for tokens
- Verify token format
- Check token expiration
- Try logging out and back in
Symptom: Chat responses don't stream, appear all at once Solution:
- Check browser network tab for SSE connection
- Verify
EventSourceor fetch streaming is working - Check for proxy/nginx buffering issues
Symptom: Network errors, "Failed to fetch" Solution:
- Verify backend is running:
curl http://localhost:8000/health - Check
NEXT_PUBLIC_API_URLin.env.local - Restart frontend dev server
{
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"token_type": "bearer"
}data: {"delta": "Based"}
data: {"delta": " on"}
data: {"delta": " the"}
data: {"context_sources": [...]}
data: {"suggested_followups": [...]}
data: {"conversation_id": 123, "message_id": 456}
{
"conversation_id": 123,
"message_id": 456,
"response": "Based on the ExploitDB data...",
"context_sources": [
{
"exploit_id": "EDB-16929",
"title": "AIX Calendar Manager...",
"similarity_score": 0.89,
"source_type": "exploitdb",
"metadata": {
"codes": "CVE-2009-3699",
"platform": "aix",
"author": "Metasploit"
}
}
],
"suggested_followups": [
"How can I mitigate this vulnerability?",
"Are there any patches available?"
]
}The integration is successful when:
- ✅ User can register and login
- ✅ Tokens are stored and automatically refreshed
- ✅ Chat messages stream in real-time
- ✅ Context sources are displayed with messages
- ✅ Conversations are persisted and can be retrieved
- ✅ Export functionality works
- ✅ Error handling works gracefully
- ✅ No CORS errors in browser console
frontend/.env.local- Environment configurationfrontend/lib/api-client.ts- API client utilityfrontend/lib/integration-test.ts- Integration testsfrontend/app/test-integration/page.tsx- Test dashboardINTEGRATION_GUIDE.md- Detailed integration guideINTEGRATION_SUMMARY.md- This file
frontend/lib/auth-context.tsx- Updated to use API clientfrontend/lib/chat-context.tsx- Updated to use API client
backend/app/main.py- CORS already configuredbackend/app/core/config.py- ALLOWED_ORIGINS already setbackend/.env- Already has correct ALLOWED_ORIGINS