Feature: User Authentication & Authorization
Status: ⏳ Planned
Priority: High
Complexity: Medium
Estimate: 4-6 hours
Assignee: -
Created: May 31, 2025
Target Completion: -
PR: -
Related Features: Infrastructure Setup, Lesson Management
📌 Overview
Purpose
Implement user authentication and authorization system using ASP.NET Core Identity with JWT token-based authentication.
User Story
As a user, I want to register, login, and access my personalized learning content so that I can track my progress and continue from where I left off.
Acceptance Criteria
📋 Requirements
Functional Requirements
| ID |
Requirement |
Priority |
| FR-001 |
User registration endpoint |
High |
| FR-002 |
User login endpoint |
High |
| FR-003 |
JWT token generation and validation |
High |
| FR-004 |
Protected routes require authentication |
High |
| FR-005 |
Current user endpoint |
Medium |
| FR-006 |
Password reset functionality |
Low |
| FR-007 |
Email verification (optional for MVP) |
Low |
Non-Functional Requirements
- Security: Passwords hashed with bcrypt or similar
- Security: JWT tokens expire after 24 hours
- Security: Refresh tokens for seamless UX
- Performance: Authentication < 500ms
- Compatibility: Works with React frontend
🏗️ Technical Design
Components Involved
- Backend: AuthController, AuthService, JWT configuration
- Database: Users table (from initial schema)
- Models: User, LoginDto, RegisterDto, AuthResponse
- Middleware: JWT authentication middleware
Data Flow
User Registration:
1. Frontend POST /api/auth/register with {username, email, password}
2. Backend validates input
3. Backend hashes password
4. Backend creates user in database
5. Backend generates JWT token
6. Returns {userId, token} to frontend
User Login:
1. Frontend POST /api/auth/login with {email, password}
2. Backend validates credentials
3. Backend generates JWT token
4. Returns {userId, token} to frontend
Protected Endpoint:
1. Frontend includes token in Authorization header
2. Backend middleware validates token
3. Backend processes request if valid
4. Returns 401 if token invalid/expired
API Endpoints
| Endpoint |
Method |
Description |
Auth Required |
/api/auth/register |
POST |
Register new user |
No |
/api/auth/login |
POST |
Login existing user |
No |
/api/auth/me |
GET |
Get current user info |
Yes |
/api/auth/logout |
POST |
Invalidate token |
Yes |
/api/auth/refresh |
POST |
Refresh expired token |
Yes |
Database Schema (from application-plan.md)
CREATE TABLE Users (
Id SERIAL PRIMARY KEY,
Username VARCHAR(50) UNIQUE NOT NULL,
Email VARCHAR(100) UNIQUE NOT NULL,
PasswordHash VARCHAR(255) NOT NULL,
CurrentLevel VARCHAR(10) DEFAULT 'A1',
Streak INT DEFAULT 0,
TotalPoints INT DEFAULT 0,
CreatedAt TIMESTAMP DEFAULT NOW()
);
🚀 Implementation Plan
Phase 1: Backend Authentication (3-4 hours)
Phase 2: Database Integration (1-2 hours)
Phase 3: Token Management (1 hour)
Phase 4: Frontend Integration (Optional - if doing full stack)
Milestones
| Milestone |
Date |
Status |
| Backend Auth Complete |
- |
⏳ |
| Database Integration |
- |
⏳ |
| Token Management |
- |
⏳ |
| Frontend Integration |
- |
⏳ |
✅ Tasks
Backend
Database
Token Management
Frontend (Optional)
🔗 Dependencies
Feature Dependencies
Technical Dependencies
- ASP.NET Core Identity
- JWT Bearer Authentication package
- BouncyCastle or similar for password hashing
Blockers
✅ Definition of Done
General Criteria (All Features)
Authentication-Specific Criteria
🧪 Testing Strategy
Testing Approach
| Test Type |
Coverage |
Tools |
Responsibility |
| Unit Tests |
80%+ code coverage |
MsTest, Moq |
Backend Dev |
| Integration Tests |
All service interactions |
MsTest, TestContainers |
Backend Dev |
| API Tests |
All endpoints |
MsTest, HttpClient |
Backend Dev |
| Frontend Unit Tests |
Component logic |
Vitest |
Frontend Dev |
| Frontend Integration |
Service integration |
Vitest |
Frontend Dev |
| E2E Tests |
Critical user journeys |
Playwright |
QA/Dev |
| Manual Testing |
Exploratory, edge cases |
BrowserStack |
QA |
Authentication-Specific Tests
Backend Tests
Frontend Tests
📝 Notes & Decisions
Decisions Made
| Date |
Decision |
Rationale |
| May 31, 2025 |
Use JWT over cookies |
Stateless, works well with SPAs, scalable |
| May 31, 2025 |
ASP.NET Core Identity |
Built-in, well-tested, integrates with EF Core |
| May 31, 2025 |
24-hour token expiration |
Balance between security and UX |
Technical Notes
- Store only password hash, never plain text
- Use HttpOnly cookies for refresh tokens if possible
- Sanitize username/email inputs to prevent injection
- Rate limit login attempts to prevent brute force
Gotchas
- ⚠️ JWT secret must be long and random (>32 characters)
- ⚠️ Token must be stored securely on frontend (HttpOnly cookie or secure localStorage)
- ⚠️ CORS must be configured to accept credentials if using cookies
- ⚠️ Password hashing should use work factor appropriate for your hardware
📊 Progress History
| Date |
Status Change |
Notes |
| May 31, 2025 |
Created |
Initial plan based on application-plan.md |
📎 Related Files & Links
Feature created from application-plan.md