Internal
Public Access
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>
96 lines
2.9 KiB
Python
96 lines
2.9 KiB
Python
"""
|
|
User serializers for KeepItGoing API.
|
|
"""
|
|
|
|
from django.contrib.auth import get_user_model
|
|
from django.contrib.auth.password_validation import validate_password
|
|
from rest_framework import serializers
|
|
from .models import DeviceToken
|
|
|
|
User = get_user_model()
|
|
|
|
|
|
class UserSerializer(serializers.ModelSerializer):
|
|
"""Serializer for user details."""
|
|
|
|
class Meta:
|
|
model = User
|
|
fields = [
|
|
'id', 'email', 'username', 'first_name', 'last_name',
|
|
'timezone', 'avatar', 'default_reminder_minutes',
|
|
'email_notifications', 'push_notifications',
|
|
'created_at', 'updated_at',
|
|
]
|
|
read_only_fields = ['id', 'email', 'created_at', 'updated_at']
|
|
|
|
|
|
class UserRegistrationSerializer(serializers.ModelSerializer):
|
|
"""Serializer for user registration."""
|
|
|
|
password = serializers.CharField(
|
|
write_only=True,
|
|
required=True,
|
|
validators=[validate_password],
|
|
style={'input_type': 'password'}
|
|
)
|
|
password_confirm = serializers.CharField(
|
|
write_only=True,
|
|
required=True,
|
|
style={'input_type': 'password'}
|
|
)
|
|
|
|
class Meta:
|
|
model = User
|
|
fields = ['email', 'username', 'password', 'password_confirm', 'first_name', 'last_name', 'timezone']
|
|
|
|
def validate(self, attrs):
|
|
if attrs['password'] != attrs['password_confirm']:
|
|
raise serializers.ValidationError({
|
|
'password_confirm': "Passwords do not match."
|
|
})
|
|
return attrs
|
|
|
|
def create(self, validated_data):
|
|
validated_data.pop('password_confirm')
|
|
user = User.objects.create_user(**validated_data)
|
|
return user
|
|
|
|
|
|
class ChangePasswordSerializer(serializers.Serializer):
|
|
"""Serializer for password change."""
|
|
|
|
old_password = serializers.CharField(
|
|
required=True,
|
|
style={'input_type': 'password'}
|
|
)
|
|
new_password = serializers.CharField(
|
|
required=True,
|
|
validators=[validate_password],
|
|
style={'input_type': 'password'}
|
|
)
|
|
|
|
def validate_old_password(self, value):
|
|
user = self.context['request'].user
|
|
if not user.check_password(value):
|
|
raise serializers.ValidationError("Current password is incorrect.")
|
|
return value
|
|
|
|
|
|
class DeviceTokenSerializer(serializers.ModelSerializer):
|
|
"""Serializer for device tokens."""
|
|
|
|
class Meta:
|
|
model = DeviceToken
|
|
fields = ['id', 'platform', 'token', 'device_name', 'is_active', 'created_at']
|
|
read_only_fields = ['id', 'created_at']
|
|
|
|
def create(self, validated_data):
|
|
validated_data['user'] = self.context['request'].user
|
|
# Update if token already exists, otherwise create
|
|
device_token, created = DeviceToken.objects.update_or_create(
|
|
user=validated_data['user'],
|
|
token=validated_data['token'],
|
|
defaults=validated_data
|
|
)
|
|
return device_token
|