Archived
- Backend: Django 5 + DRF with accounts, documents, collections, and reading apps - Custom User model with email-based auth, JWT via SimpleJWT - Full CRUD viewsets with ModelSerializer + DRF routers - pytest, Ruff, drf-spectacular (OpenAPI), whitenoise - Dockerfile for production deployment - Frontend: React 18 + TypeScript + Vite - Lazy-loaded routes with ProtectedRoute/PublicRoute guards - Auth context with useReducer, token refresh interceptor - Pages: Login, Register, Library, Document Detail, Reader, Collections, Settings - Dark theme, responsive grid layout, Vite proxy to Django backend - Mobile: Expo SDK 51 + React Native + Expo Router - File-based routing with login, register, and library screens - AsyncStorage for token persistence, token refresh interceptor - Shared API types via @cloud-reader/shared workspace package - Shared: TypeScript types (API responses, auth, documents, etc.) - CI/CD: 3 independent GitHub Actions pipelines (backend, frontend, mobile)
53 lines
1.8 KiB
Python
53 lines
1.8 KiB
Python
from django.contrib.auth import get_user_model
|
|
from rest_framework import serializers
|
|
|
|
from .models import User
|
|
|
|
UserModel = get_user_model()
|
|
|
|
|
|
class RegisterSerializer(serializers.ModelSerializer[User]):
|
|
password = serializers.CharField(write_only=True, min_length=8)
|
|
password_confirm = serializers.CharField(write_only=True, min_length=8)
|
|
|
|
class Meta:
|
|
model = User
|
|
fields = ["email", "username", "display_name", "password", "password_confirm"]
|
|
|
|
def validate(self, attrs):
|
|
if attrs["password"] != attrs.pop("password_confirm"):
|
|
raise serializers.ValidationError({"password_confirm": "Passwords do not match."})
|
|
return attrs
|
|
|
|
def create(self, validated_data):
|
|
password = validated_data.pop("password")
|
|
user = UserModel(**validated_data)
|
|
user.set_password(password)
|
|
user.save()
|
|
return user
|
|
|
|
|
|
class UserSerializer(serializers.ModelSerializer[User]):
|
|
avatar_url = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = User
|
|
fields = [
|
|
"id", "email", "username", "display_name", "avatar_url",
|
|
"date_joined", "is_verified", "reading_preferences",
|
|
]
|
|
read_only_fields = ["id", "email", "date_joined", "is_verified"]
|
|
|
|
def get_avatar_url(self, obj: User) -> str | None:
|
|
return obj.avatar_url
|
|
|
|
|
|
class ChangePasswordSerializer(serializers.Serializer):
|
|
old_password = serializers.CharField(required=True)
|
|
new_password = serializers.CharField(required=True, min_length=8)
|
|
|
|
def validate_old_password(self, value: str) -> str:
|
|
user = self.context["request"].user
|
|
if not user.check_password(value):
|
|
raise serializers.ValidationError("Current password is incorrect.")
|
|
return value |