4.6 KiB
4.6 KiB
Authentication Logic Consolidation
Overview
The authentication logic has been consolidated from multiple scattered files into a single, centralized AuthManager
class. This improves maintainability, reduces code duplication, and provides a consistent authentication interface.
Files Changed
1. New Centralized Module
static/auth-manager.js
- New centralized authentication manager class
2. Refactored Files
static/auth.js
- Simplified to use AuthManagerstatic/magic-login.js
- Simplified to use AuthManagerstatic/cleanup-auth.js
- Simplified to use AuthManager
AuthManager Features
Core Functionality
- Centralized State Management - Single source of truth for authentication state
- Cookie & localStorage Management - Consistent handling of auth data storage
- Magic Link Processing - Handles both URL-based and token-based magic login
- Authentication Polling - Periodic state checks with caching and debouncing
- User Session Management - Login, logout, and account deletion
Key Methods
initialize()
- Initialize the auth manager and handle magic loginsetAuthState(email, username, token)
- Set authentication stateclearAuthState()
- Clear all authentication dataisAuthenticated()
- Check current authentication statusgetCurrentUser()
- Get current user datalogout()
- Perform logout and redirectdeleteAccount()
- Handle account deletioncleanupAuthState(email)
- Clean up inconsistent auth state
Authentication Flow
- Magic Login Detection - Checks URL parameters for login tokens/success
- User Info Retrieval - Fetches email from
/api/me
endpoint - State Setting - Sets email as primary UID, username for display
- UI Updates - Updates body classes and initializes user session
- Navigation - Redirects to user profile page
Data Storage Strategy
localStorage Keys
uid
- Primary identifier (email-based)user_email
- Explicit email storageusername
- Display name (separate from UID)authToken
- Authentication tokenisAuthenticated
- Boolean authentication stateuid_time
- Session timestamp
Cookie Strategy
uid
- Email-based UID withSameSite=Lax
authToken
- Auth token withSameSite=Lax; Secure
isAuthenticated
- Boolean flag withSameSite=Lax
Removed Redundancy
Eliminated Duplicate Code
- User info fetching - Centralized in
fetchUserInfo()
- Auth state setting - Centralized in
setAuthState()
- Cookie management - Centralized in
setAuthState()
andclearAuthState()
- Magic login processing - Centralized in
processMagicLogin()
andprocessTokenLogin()
Removed Fields
confirmed_uid
- Was duplicate ofuid
, now eliminated
Backward Compatibility
Global Functions (Legacy Support)
window.getCurrentUser()
- Get current user datawindow.isAuthenticated()
- Check authentication statuswindow.logout()
- Perform logoutwindow.cleanupAuthState(email)
- Clean up auth state
Existing Function Exports
initMagicLogin()
- Maintained in magic-login.js for compatibilitycleanupAuthState()
- Maintained in cleanup-auth.js for compatibility
Benefits Achieved
1. Maintainability
- Single source of authentication logic
- Consistent error handling and logging
- Easier to debug and modify
2. Performance
- Reduced code duplication
- Optimized caching and debouncing
- Fewer redundant API calls
3. Reliability
- Consistent state management
- Proper cleanup on logout
- Robust error handling
4. Security
- Consistent cookie security attributes
- Proper state clearing on logout
- Centralized validation
Migration Notes
For Developers
- Import
authManager
from./auth-manager.js
for new code - Use
authManager.isAuthenticated()
instead of manual checks - Use
authManager.getCurrentUser()
for user data - Legacy global functions still work for existing code
Testing
- Test magic link login (both URL and token-based)
- Test authentication state persistence
- Test logout and account deletion
- Test authentication polling and state changes
Future Improvements
Potential Enhancements
- Token Refresh - Automatic token renewal
- Session Timeout - Configurable session expiration
- Multi-tab Sync - Better cross-tab authentication sync
- Audit Logging - Enhanced authentication event logging
- Rate Limiting - Protection against auth abuse
Configuration Options
Consider adding configuration for:
- Polling intervals
- Cache TTL values
- Debug logging levels
- Cookie security settings