|
| 1 | +# Frontend Authentication Integration Guide |
| 2 | + |
| 3 | +This document provides guidance for integrating the frontend authentication system with backend APIs and existing SDLC Core components. |
| 4 | + |
| 5 | +## Authentication API Endpoints |
| 6 | + |
| 7 | +The frontend expects the following API endpoints to be implemented: |
| 8 | + |
| 9 | +### POST /auth/api/login |
| 10 | +**Request:** |
| 11 | +```json |
| 12 | +{ |
| 13 | + "username": "string", |
| 14 | + "password": "string", |
| 15 | + "rememberMe": boolean (optional) |
| 16 | +} |
| 17 | +``` |
| 18 | + |
| 19 | +**Response (Success - 200):** |
| 20 | +```json |
| 21 | +{ |
| 22 | + "user": { |
| 23 | + "id": number, |
| 24 | + "username": "string", |
| 25 | + "email": "string", |
| 26 | + "roles": [ |
| 27 | + { |
| 28 | + "id": number, |
| 29 | + "name": "string", |
| 30 | + "description": "string" |
| 31 | + } |
| 32 | + ], |
| 33 | + "isActive": boolean, |
| 34 | + "memberSince": "string", |
| 35 | + "lastLogin": "string" |
| 36 | + }, |
| 37 | + "token": "string" (optional - for JWT implementation) |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +**Response (Error - 401):** |
| 42 | +```json |
| 43 | +{ |
| 44 | + "error": "Invalid credentials" |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +### POST /auth/api/register |
| 49 | +**Request:** |
| 50 | +```json |
| 51 | +{ |
| 52 | + "username": "string", |
| 53 | + "email": "string", |
| 54 | + "password": "string", |
| 55 | + "confirmPassword": "string" |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +**Response:** Same as login success response |
| 60 | + |
| 61 | +### POST /auth/api/logout |
| 62 | +**Request:** Empty body or JWT token in headers |
| 63 | +**Response:** 200 OK |
| 64 | + |
| 65 | +### GET /auth/api/user/profile |
| 66 | +**Request:** JWT token in Authorization header (if using JWT) |
| 67 | +**Response:** User object (same format as login response) |
| 68 | + |
| 69 | +## Role-Based Access Integration |
| 70 | + |
| 71 | +### Available Roles |
| 72 | +- `admin` - Full system administrator access |
| 73 | +- `user` - Standard user access |
| 74 | +- `moderator` - Content moderation access |
| 75 | +- `analyst` - Analytics and reporting access |
| 76 | +- `developer` - Development and deployment access |
| 77 | + |
| 78 | +### Using Authentication in Components |
| 79 | + |
| 80 | +```tsx |
| 81 | +import { useAuth, RequireRole, RequireAuth } from '../stores/authStore'; |
| 82 | + |
| 83 | +// Check authentication status |
| 84 | +const { isAuthenticated, user, hasRole } = useAuth(); |
| 85 | + |
| 86 | +// Protect entire components |
| 87 | +<RequireAuth> |
| 88 | + <ProtectedContent /> |
| 89 | +</RequireAuth> |
| 90 | + |
| 91 | +// Protect by specific role |
| 92 | +<RequireRole role="admin"> |
| 93 | + <AdminContent /> |
| 94 | +</RequireRole> |
| 95 | + |
| 96 | +// Protect by multiple roles |
| 97 | +<RequireRole role={["admin", "developer"]}> |
| 98 | + <DevContent /> |
| 99 | +</RequireRole> |
| 100 | + |
| 101 | +// Check roles in code |
| 102 | +if (hasRole('analyst')) { |
| 103 | + // Show analytics features |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +### Protecting API Calls |
| 108 | + |
| 109 | +```tsx |
| 110 | +import { useAuth } from '../stores/authStore'; |
| 111 | + |
| 112 | +const MyComponent = () => { |
| 113 | + const { user, isAuthenticated } = useAuth(); |
| 114 | + |
| 115 | + const callProtectedAPI = async () => { |
| 116 | + if (!isAuthenticated) { |
| 117 | + throw new Error('User not authenticated'); |
| 118 | + } |
| 119 | + |
| 120 | + // Include user token in API calls |
| 121 | + const response = await fetch('/api/protected-endpoint', { |
| 122 | + headers: { |
| 123 | + 'Authorization': `Bearer ${user.token}`, // if using JWT |
| 124 | + 'Content-Type': 'application/json' |
| 125 | + } |
| 126 | + }); |
| 127 | + |
| 128 | + return response.json(); |
| 129 | + }; |
| 130 | +}; |
| 131 | +``` |
| 132 | + |
| 133 | +## Backend Integration Steps |
| 134 | + |
| 135 | +### 1. Update Authentication Store |
| 136 | +Replace the mock API in `frontend/stores/authStore.tsx`: |
| 137 | + |
| 138 | +```tsx |
| 139 | +// Replace mockApi object with actual API calls |
| 140 | +const api = { |
| 141 | + login: async (credentials: LoginCredentials): Promise<User> => { |
| 142 | + const response = await fetch('/auth/api/login', { |
| 143 | + method: 'POST', |
| 144 | + headers: { 'Content-Type': 'application/json' }, |
| 145 | + body: JSON.stringify(credentials) |
| 146 | + }); |
| 147 | + |
| 148 | + if (!response.ok) { |
| 149 | + const error = await response.json(); |
| 150 | + throw new Error(error.message || 'Login failed'); |
| 151 | + } |
| 152 | + |
| 153 | + return response.json(); |
| 154 | + }, |
| 155 | + |
| 156 | + // ... implement other methods |
| 157 | +}; |
| 158 | +``` |
| 159 | + |
| 160 | +### 2. Add JWT Token Management (Optional) |
| 161 | +If using JWT tokens: |
| 162 | + |
| 163 | +```tsx |
| 164 | +// Add token to localStorage |
| 165 | +localStorage.setItem('authToken', user.token); |
| 166 | + |
| 167 | +// Include token in API requests |
| 168 | +const token = localStorage.getItem('authToken'); |
| 169 | +headers: { |
| 170 | + 'Authorization': `Bearer ${token}` |
| 171 | +} |
| 172 | + |
| 173 | +// Remove token on logout |
| 174 | +localStorage.removeItem('authToken'); |
| 175 | +``` |
| 176 | + |
| 177 | +### 3. Environment Configuration |
| 178 | +Add environment variables for API endpoints: |
| 179 | + |
| 180 | +```env |
| 181 | +VITE_API_BASE_URL=http://localhost:3000 |
| 182 | +VITE_AUTH_ENDPOINT=/auth/api |
| 183 | +``` |
| 184 | + |
| 185 | +### 4. Error Handling |
| 186 | +Update error handling for production: |
| 187 | + |
| 188 | +```tsx |
| 189 | +// Add proper error boundaries |
| 190 | +// Implement user-friendly error messages |
| 191 | +// Add logging for authentication errors |
| 192 | +``` |
| 193 | + |
| 194 | +## Security Considerations |
| 195 | + |
| 196 | +1. **HTTPS Only**: Ensure all authentication happens over HTTPS |
| 197 | +2. **Token Expiration**: Implement proper token expiration and refresh |
| 198 | +3. **Rate Limiting**: Add rate limiting to prevent brute force attacks |
| 199 | +4. **Password Policies**: Enforce strong password requirements |
| 200 | +5. **Session Management**: Implement secure session handling |
| 201 | +6. **CSRF Protection**: Add CSRF token validation for forms |
| 202 | + |
| 203 | +## Testing |
| 204 | + |
| 205 | +The authentication system includes comprehensive test scenarios: |
| 206 | + |
| 207 | +1. **Login Flow**: Valid/invalid credentials, remember me functionality |
| 208 | +2. **Registration**: New user creation, validation, role assignment |
| 209 | +3. **Role Protection**: Access control for admin/user roles |
| 210 | +4. **Navigation**: Context-aware navigation based on auth state |
| 211 | +5. **Session Persistence**: Page refresh maintaining login state |
| 212 | +6. **Logout**: Clean session termination |
| 213 | + |
| 214 | +## Demo Credentials |
| 215 | + |
| 216 | +For development/testing: |
| 217 | +- Admin: `admin` / `admin123` |
| 218 | +- User: `user` / `user123` |
| 219 | + |
| 220 | +Remove these before production deployment. |
| 221 | + |
| 222 | +## Component Architecture |
| 223 | + |
| 224 | +``` |
| 225 | +AuthProvider (Context) |
| 226 | +├── App (Main routing logic) |
| 227 | +├── Navigation (Auth-aware navigation) |
| 228 | +├── LoginForm (Authentication form) |
| 229 | +├── RegisterForm (User registration) |
| 230 | +├── Dashboard (User profile & info) |
| 231 | +├── AdminPanel (Role-protected admin interface) |
| 232 | +└── AuthGuards (HOCs for access control) |
| 233 | + ├── RequireAuth |
| 234 | + ├── RequireRole |
| 235 | + ├── RequireAdmin |
| 236 | + ├── RequireModerator |
| 237 | + ├── RequireAnalyst |
| 238 | + └── RequireDeveloper |
| 239 | +``` |
| 240 | + |
| 241 | +This architecture provides a solid foundation for secure, role-based authentication throughout the SDLC Core system. |
0 commit comments