Files
KeepItGoingServer/users/backends.py
T
Keith SmithandClaude Sonnet 4.5 21d9d01885 Add email verification, admin approval, and user profile enhancements
Implement comprehensive email verification and admin approval system for user registration. Users must verify their email before logging in, and admins must approve new users before they can access the system.

Major features:
- Email verification with UUID tokens (24hr expiry, one-time use)
- Admin approval workflow via Django admin interface
- Custom authentication backend enforcing verification and approval
- Password change functionality for authenticated users
- Enhanced profile page with proper styling and theme support
- Collect first name, last name, and timezone during registration
- Profile link added to header navigation

Email notifications:
- Verification email sent after registration
- Admin notification when users need approval
- Approval notification sent to users

Security features:
- Cryptographically secure UUID tokens
- Token expiration and one-time use enforcement
- Email enumeration protection
- Custom JWT token validation for API access
- Existing users auto-approved via data migration

Templates added:
- Email templates (verification, approval, admin notification)
- Web templates (password change, verification pages)
- Enhanced profile page with dark/light mode support

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-22 18:13:38 -07:00

46 lines
1.4 KiB
Python

"""
Custom authentication backends for KeepItGoing.
"""
from django.contrib.auth.backends import ModelBackend
from django.contrib.auth import get_user_model
User = get_user_model()
class EmailVerifiedApprovedBackend(ModelBackend):
"""
Authentication backend that requires email verification and admin approval.
This backend extends Django's ModelBackend to add additional checks:
- User must have verified their email address
- User must be approved by an admin
If authentication fails due to these checks, helpful error messages
are stored in the session for the web UI.
"""
def authenticate(self, request, username=None, password=None, **kwargs):
# Call parent to do the actual authentication
user = super().authenticate(request, username=username, password=password, **kwargs)
if user is None:
return None
# Check email verification
if not user.email_verified:
# Store reason for failed login (for web UI)
if request:
request.session['login_error'] = 'email_not_verified'
request.session['login_email'] = user.email
return None
# Check admin approval
if not user.is_approved:
if request:
request.session['login_error'] = 'not_approved'
request.session['login_email'] = user.email
return None
return user