6.2 KiB
6.2 KiB
Authentication Implementation
Overview
This document describes the authentication system implementation with proper error handling, separate signup/signin routes, and email verification flow.
Architecture
Backend (User Auth Service)
- Port: 8011
- Base URL:
http://localhost:8011 - Database: PostgreSQL
- Features: JWT authentication, email verification, session management
Frontend (Next.js App)
- Port: 3001
- Base URL:
http://localhost:3001 - Framework: Next.js 15.4.6 with TypeScript
- UI: Tailwind CSS + shadcn/ui components
Routes Structure
Frontend Routes
/signup - User registration page
/signin - User login page
/auth - Redirects to /signin (legacy support)
/verify-email - Email verification page (handled by backend redirect)
Backend API Endpoints
POST /api/auth/register - User registration
POST /api/auth/login - User login
GET /api/auth/verify-email - Email verification (redirects to frontend)
POST /api/auth/logout - User logout
POST /api/auth/refresh - Token refresh
GET /api/auth/me - Get user profile
User Flow
1. Registration Flow
- User visits
/signup - Fills out registration form
- Submits form →
POST /api/auth/register - Backend creates user account and sends verification email
- Frontend shows success message and redirects to
/signinafter 3 seconds - User receives email with verification link
2. Email Verification Flow
- User clicks verification link in email
- Link points to:
http://localhost:8011/api/auth/verify-email?token=<token> - Backend verifies token and redirects to:
http://localhost:3001/signin?verified=true - Frontend displays success message: "Email verified successfully! You can now sign in to your account."
3. Login Flow
- User visits
/signin - Fills out login form
- Submits form →
POST /api/auth/login - Backend validates credentials and returns JWT tokens
- Frontend stores tokens and redirects to dashboard (
/)
Error Handling
Backend Error Responses
All API endpoints return consistent error format:
{
"success": false,
"error": "Error type",
"message": "Detailed error message"
}
Frontend Error Handling
- Network Errors: Display user-friendly messages
- API Errors: Show specific error messages from backend
- Validation Errors: Client-side validation with immediate feedback
- Authentication Errors: Clear messaging for login/registration issues
Common Error Scenarios
Registration Errors
- Email already exists: "An account with this email already exists"
- Username taken: "Username is already taken"
- Invalid email format: "Please enter a valid email address"
- Weak password: "Password must be at least 8 characters long"
- Missing fields: "Please fill in all required fields"
Login Errors
- Invalid credentials: "Invalid email or password"
- Email not verified: "Please verify your email before signing in"
- Account locked: "Account is temporarily locked due to multiple failed attempts"
Email Verification Errors
- Invalid token: "Verification link is invalid or has expired"
- Already verified: "Email is already verified"
- Token expired: "Verification link has expired. Please request a new one"
Components Structure
Signup Flow
/signup
├── SignUpPage (main container)
├── SignUpForm (form component)
└── Success State (after registration)
Signin Flow
/signin
├── SignInPage (main container)
├── SignInForm (form component)
└── Verification Messages (from URL params)
API Integration
Authentication Handler (authenticationHandler.tsx)
- Handles API calls to backend
- Proper error propagation
- TypeScript interfaces for type safety
Key Functions
registerUser(data: RegisterData): Promise<ApiResponse>
loginUser(email: string, password: string): Promise<ApiResponse>
Security Features
Backend Security
- JWT token authentication
- Password hashing (bcrypt)
- Rate limiting on auth endpoints
- CORS configuration
- Helmet security headers
- Session management
Frontend Security
- Token storage in localStorage
- Automatic token refresh
- Secure API communication
- Input validation and sanitization
Environment Configuration
Backend (.env)
PORT=8011
FRONTEND_URL=http://localhost:3001
POSTGRES_HOST=localhost
POSTGRES_DB=user_auth
JWT_SECRET=your-secret-key
SMTP_HOST=smtp.gmail.com
SMTP_PORT=587
SMTP_USER=your-email@gmail.com
SMTP_PASS=your-app-password
Frontend (.env.local)
NEXT_PUBLIC_API_URL=http://localhost:8011
NEXT_PUBLIC_FRONTEND_URL=http://localhost:3001
Testing the Implementation
1. Start Services
# Backend
cd automated-dev-pipeline
docker-compose up user-auth
# Frontend
cd codenuk-frontend-dark-theme
npm run dev
2. Test Registration
- Visit
http://localhost:3001/signup - Fill out registration form
- Submit and check for success message
- Check email for verification link
3. Test Email Verification
- Click verification link in email
- Should redirect to
http://localhost:3001/signin?verified=true - Verify success message appears
4. Test Login
- Visit
http://localhost:3001/signin - Enter credentials
- Should redirect to dashboard on success
Troubleshooting
Common Issues
-
CORS Errors
- Check backend CORS configuration
- Verify frontend URL in allowed origins
-
Email Not Sending
- Check SMTP configuration in backend
- Verify email credentials
-
Verification Link Not Working
- Check frontend URL in backend configuration
- Verify token expiration settings
-
Login Fails After Verification
- Check if user is properly verified in database
- Verify JWT token generation
Debug Steps
- Check browser network tab for API calls
- Check backend logs for errors
- Verify database connections
- Test API endpoints directly with Postman/curl
Future Enhancements
- Password Reset Flow
- Two-Factor Authentication
- Social Login Integration
- Account Lockout Protection
- Session Management Dashboard
- Audit Logging