-
Notifications
You must be signed in to change notification settings - Fork 0
Implement comprehensive frontend infrastructure with role-based authentication #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Copilot
wants to merge
4
commits into
main
Choose a base branch
from
copilot/fix-4
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a74e4bc
Initial plan
Copilot f4e0a48
Implement basic authentication infrastructure with login/register forms
Copilot 34081f3
Fix role-based access control for dashboard admin panel link
Copilot 2cf4a0b
Add comprehensive frontend authentication integration guide
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,241 @@ | ||
# Frontend Authentication Integration Guide | ||
|
||
This document provides guidance for integrating the frontend authentication system with backend APIs and existing SDLC Core components. | ||
|
||
## Authentication API Endpoints | ||
|
||
The frontend expects the following API endpoints to be implemented: | ||
|
||
### POST /auth/api/login | ||
**Request:** | ||
```json | ||
{ | ||
"username": "string", | ||
"password": "string", | ||
"rememberMe": boolean (optional) | ||
} | ||
``` | ||
|
||
**Response (Success - 200):** | ||
```json | ||
{ | ||
"user": { | ||
"id": number, | ||
"username": "string", | ||
"email": "string", | ||
"roles": [ | ||
{ | ||
"id": number, | ||
"name": "string", | ||
"description": "string" | ||
} | ||
], | ||
"isActive": boolean, | ||
"memberSince": "string", | ||
"lastLogin": "string" | ||
}, | ||
"token": "string" (optional - for JWT implementation) | ||
} | ||
``` | ||
|
||
**Response (Error - 401):** | ||
```json | ||
{ | ||
"error": "Invalid credentials" | ||
} | ||
``` | ||
|
||
### POST /auth/api/register | ||
**Request:** | ||
```json | ||
{ | ||
"username": "string", | ||
"email": "string", | ||
"password": "string", | ||
"confirmPassword": "string" | ||
} | ||
``` | ||
|
||
**Response:** Same as login success response | ||
|
||
### POST /auth/api/logout | ||
**Request:** Empty body or JWT token in headers | ||
**Response:** 200 OK | ||
|
||
### GET /auth/api/user/profile | ||
**Request:** JWT token in Authorization header (if using JWT) | ||
**Response:** User object (same format as login response) | ||
|
||
## Role-Based Access Integration | ||
|
||
### Available Roles | ||
- `admin` - Full system administrator access | ||
- `user` - Standard user access | ||
- `moderator` - Content moderation access | ||
- `analyst` - Analytics and reporting access | ||
- `developer` - Development and deployment access | ||
|
||
### Using Authentication in Components | ||
|
||
```tsx | ||
import { useAuth, RequireRole, RequireAuth } from '../stores/authStore'; | ||
|
||
// Check authentication status | ||
const { isAuthenticated, user, hasRole } = useAuth(); | ||
|
||
// Protect entire components | ||
<RequireAuth> | ||
<ProtectedContent /> | ||
</RequireAuth> | ||
|
||
// Protect by specific role | ||
<RequireRole role="admin"> | ||
<AdminContent /> | ||
</RequireRole> | ||
|
||
// Protect by multiple roles | ||
<RequireRole role={["admin", "developer"]}> | ||
<DevContent /> | ||
</RequireRole> | ||
|
||
// Check roles in code | ||
if (hasRole('analyst')) { | ||
// Show analytics features | ||
} | ||
``` | ||
|
||
### Protecting API Calls | ||
|
||
```tsx | ||
import { useAuth } from '../stores/authStore'; | ||
|
||
const MyComponent = () => { | ||
const { user, isAuthenticated } = useAuth(); | ||
|
||
const callProtectedAPI = async () => { | ||
if (!isAuthenticated) { | ||
throw new Error('User not authenticated'); | ||
} | ||
|
||
// Include user token in API calls | ||
const response = await fetch('/api/protected-endpoint', { | ||
headers: { | ||
'Authorization': `Bearer ${user.token}`, // if using JWT | ||
'Content-Type': 'application/json' | ||
} | ||
}); | ||
|
||
return response.json(); | ||
}; | ||
}; | ||
``` | ||
|
||
## Backend Integration Steps | ||
|
||
### 1. Update Authentication Store | ||
Replace the mock API in `frontend/stores/authStore.tsx`: | ||
|
||
```tsx | ||
// Replace mockApi object with actual API calls | ||
const api = { | ||
login: async (credentials: LoginCredentials): Promise<User> => { | ||
const response = await fetch('/auth/api/login', { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify(credentials) | ||
}); | ||
|
||
if (!response.ok) { | ||
const error = await response.json(); | ||
throw new Error(error.message || 'Login failed'); | ||
} | ||
|
||
return response.json(); | ||
}, | ||
|
||
// ... implement other methods | ||
}; | ||
``` | ||
|
||
### 2. Add JWT Token Management (Optional) | ||
If using JWT tokens: | ||
|
||
```tsx | ||
// Add token to localStorage | ||
localStorage.setItem('authToken', user.token); | ||
|
||
// Include token in API requests | ||
const token = localStorage.getItem('authToken'); | ||
headers: { | ||
'Authorization': `Bearer ${token}` | ||
} | ||
|
||
// Remove token on logout | ||
localStorage.removeItem('authToken'); | ||
``` | ||
|
||
### 3. Environment Configuration | ||
Add environment variables for API endpoints: | ||
|
||
```env | ||
VITE_API_BASE_URL=http://localhost:3000 | ||
VITE_AUTH_ENDPOINT=/auth/api | ||
``` | ||
|
||
### 4. Error Handling | ||
Update error handling for production: | ||
|
||
```tsx | ||
// Add proper error boundaries | ||
// Implement user-friendly error messages | ||
// Add logging for authentication errors | ||
``` | ||
|
||
## Security Considerations | ||
|
||
1. **HTTPS Only**: Ensure all authentication happens over HTTPS | ||
2. **Token Expiration**: Implement proper token expiration and refresh | ||
3. **Rate Limiting**: Add rate limiting to prevent brute force attacks | ||
4. **Password Policies**: Enforce strong password requirements | ||
5. **Session Management**: Implement secure session handling | ||
6. **CSRF Protection**: Add CSRF token validation for forms | ||
|
||
## Testing | ||
|
||
The authentication system includes comprehensive test scenarios: | ||
|
||
1. **Login Flow**: Valid/invalid credentials, remember me functionality | ||
2. **Registration**: New user creation, validation, role assignment | ||
3. **Role Protection**: Access control for admin/user roles | ||
4. **Navigation**: Context-aware navigation based on auth state | ||
5. **Session Persistence**: Page refresh maintaining login state | ||
6. **Logout**: Clean session termination | ||
|
||
## Demo Credentials | ||
|
||
For development/testing: | ||
- Admin: `admin` / `admin123` | ||
- User: `user` / `user123` | ||
|
||
Remove these before production deployment. | ||
|
||
## Component Architecture | ||
|
||
``` | ||
AuthProvider (Context) | ||
├── App (Main routing logic) | ||
├── Navigation (Auth-aware navigation) | ||
├── LoginForm (Authentication form) | ||
├── RegisterForm (User registration) | ||
├── Dashboard (User profile & info) | ||
├── AdminPanel (Role-protected admin interface) | ||
└── AuthGuards (HOCs for access control) | ||
├── RequireAuth | ||
├── RequireRole | ||
├── RequireAdmin | ||
├── RequireModerator | ||
├── RequireAnalyst | ||
└── RequireDeveloper | ||
``` | ||
|
||
This architecture provides a solid foundation for secure, role-based authentication throughout the SDLC Core system. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / check-spelling
Unrecognized Spelling Error