4.0 KiB
M5: Authentication Implementation - Completed
Summary
Complete JWT-based authentication system implemented for Newzy news application with FastAPI backend and React TypeScript frontend.
Backend Components
Files Created/Modified
-
backend/app/auth.py - Core authentication logic
- PBKDF2 password hashing (120,000 iterations)
- JWT token creation/verification (HS256 algorithm)
- 7-day token expiry
get_current_userdependency for protected routes
-
backend/app/models.py - User model
username: unique, indexed String(60)password_hash: String(128)created_at: DateTime with default
-
backend/app/schemas.py - Pydantic request/response schemas
RegisterIn,LoginIn,TokenOut,MeOut
-
backend/app/database.py - Database utilities
get_db()dependency factoryutcnow()helper function
-
backend/main.py - Authentication endpoints added
- POST
/api/auth/register- User registration - POST
/api/auth/login- User login - POST
/api/auth/logout- Logout (stateless) - GET
/api/auth/me- Current user info
- POST
-
backend/requirements.txt - Added
python-jose[cryptography] -
.env - Added
HN_SECRET_KEYenvironment variable -
data/.gitignore - Added database file exclusions
Frontend Components
Files Created/Modified
-
frontend/src/types/auth.ts - Type definitions
TokenResponse,MeResponseinterfaces
-
frontend/src/api/client.ts - Auth API methods
login(),register(),logout(),me()- Token attachment to all authenticated requests
TOKEN_KEY = 'AUTH_TOKEN'
-
frontend/src/auth.tsx - Authentication context
AuthProvidercomponentuseAuth()custom hook- Session restoration on mount
-
frontend/src/pages/Login.tsx - Login page
- Form validation
- Error handling
- Link to registration
-
frontend/src/pages/Register.tsx - Registration page
- Username format validation (alphanumeric + underscores, 3-20 chars)
- Password strength validation
- Confirm password matching
-
frontend/src/pages/SignOut.tsx - Logout page
- Clears token and redirects to login
-
frontend/src/App.tsx - Route configuration
- Conditional routing based on auth state
- Protected routes for authenticated users
-
frontend/src/main.tsx - App wrapped with
AuthProvider -
frontend/src/components/ui/Header.tsx - Updated with user info and logout button
-
frontend/src/assets/styles/auth.css - Auth page styling
Security Features
- ✅ Passwords hashed with PBKDF2-HMAC-SHA256 (120,000 iterations)
- ✅ Random salt per password using
secrets.token_hex(16) - ✅ JWT signed with HS256 algorithm
- ✅ 7-day token expiry prevents long-term session hijacking
- ✅ Username validation prevents injection attacks
- ✅ Environment variable for JWT secret key
Test Coverage
- backend/tests/test_auth.py - Auth endpoint tests
- Registration creates account
- Duplicate username rejection
- Invalid username format rejection
- Login with valid/invalid credentials
- Protected
/meendpoint - Logout functionality
- Password hashing verification
API Endpoints Summary
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| POST | /api/auth/register | Register new user | No |
| POST | /api/auth/login | Login and get token | No |
| POST | /api/auth/logout | Logout (stateless) | Yes |
| GET | /api/auth/me | Get current user info | Yes |
Frontend Flow
- User lands on app → Unauthenticated route shown
- User logs in → Token stored in localStorage
- AuthContext updates state and redirects to home
- Protected routes check auth before rendering
- Session persists across page reloads (localStorage)
- Logout clears token and redirects to login
Next Steps (Future Milestones)
- Article creation linked to user account
- Profile management
- Password reset flow
- Email verification
- Rate limiting on auth endpoints
- Refresh token implementation