🔒 Add JWT authentication system with backend, frontend, API client, route guards, and unit tests for secure user management and token-based access control

This commit is contained in:
2026-01-28 22:51:15 +00:00
parent 8f74fef02a
commit 4bbcdbb96e

View File

@@ -133,7 +133,89 @@ npm test -- useAsyncState
## Implementation Details
### Step 8: Component Refactoring (Partial)
### Step 9: JWT Authentication System
**Backend Infrastructure:**
- `server/utils/jwt-utils.js` - JWT token management with mock fallback for testing
- `createToken()` - Creates signed JWT with 7-day expiration
- `verifyToken()` - Validates and decodes token
- `decodeToken()` - Decodes without verification
- `isTokenExpired()` - Quick expiration check
- `getTokenExpiresIn()` - Returns remaining time in ms
- `server/middleware/auth.js` - Express middleware for authentication
- `authMiddleware()` - Validates Bearer token in Authorization header
- `requirePermission()` - Checks user has specific permissions
- `requireAdmin()` - Shorthand for admin-only routes
- `authErrorHandler()` - Centralized error handling
- `server/routes/auth.js` - RESTful authentication endpoints
- `POST /auth/login` - Login with password → JWT token
- `POST /auth/verify` - Verify token validity
- `POST /auth/refresh` - Refresh token with extended expiration
- `GET /auth/user` - Get current user info (requires token)
- `POST /auth/logout` - Logout (client-side token deletion)
**Frontend Implementation:**
- `src/composables/useAuth.js` - Authentication state composable
- Manages token and user state in localStorage
- Methods: login, logout, refreshToken, getUserInfo
- Permission checking with `hasPermission()`
- Automatic auth initialization on app startup
- API interceptor setup for automatic Bearer token inclusion
- `src/views/AdminLogin.vue` - Admin login page
- Password input with visibility toggle
- Form validation and error display
- Responsive design (mobile-friendly)
- Info cards explaining security model
- Integrates useAuth for login flow
- `src/router/guards.js` - Route protection
- `setupAuthGuards()` - Registers navigation guards
- Checks token validity before route access
- Redirects to login for protected routes
- Preserves redirect path for post-login navigation
**API Client Enhancement:**
- Updated `src/utilities/api-client.js`
- `setDefaultHeader()` - Set persistent headers (e.g., Authorization)
- `removeDefaultHeader()` - Remove persistent headers on logout
- Headers automatically merged with all requests
**Testing:**
- `tests/unit/composables/useAuth.test.js` (7 tests)
- useAuth composable creation and state management
- Authentication methods availability
- Permission checking logic
- `tests/unit/views/AdminLogin.test.js` (5 tests)
- Login form rendering
- Password visibility toggle
- Info card display
- Login button functionality
**Security Features:**
- JWT tokens expire after 7 days
- Tokens stored in secure localStorage
- Bearer token in Authorization header (standard OAuth pattern)
- Server validates token signature
- Permission-based access control (extensible)
- Automatic logout on token expiration
- Password hashing ready (bcrypt in server/package.json)
**Integration Points:**
- Works with existing api-client for transparent auth
- Router guards integrate with Vue Router lifecycle
- Compatible with all existing components
- Non-intrusive design (components don't need auth awareness)
**Next Phase (10-12):**
- Feature flags system using JWT permissions
- Secure backend configuration
- Environment-based flag toggling
**GamemasterManager.vue Refactoring:**
- Replaced manual `loading`, `error`, `saving` ref states with `useAsyncState` composable