mirror of
https://github.com/thecyberlearn/netcop-ai.git
synced 2026-08-18 13:33:00 +00:00
Features: - Email-based user authentication with token auth - Stripe Checkout integration for wallet top-ups - n8n workflow triggering with automatic fee deduction ($0.10) - Comprehensive transaction and usage logging - Django Admin interface for monitoring - Rate limiting and security middleware - Production-ready deployment configuration - Modular settings (dev/prod environments) - UUID primary keys for enhanced security - Comprehensive test coverage 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
53 lines
1.7 KiB
Python
53 lines
1.7 KiB
Python
from rest_framework import serializers
|
|
from django.contrib.auth import authenticate
|
|
from .models import User
|
|
|
|
|
|
class UserRegistrationSerializer(serializers.ModelSerializer):
|
|
password = serializers.CharField(write_only=True, min_length=8)
|
|
password_confirm = serializers.CharField(write_only=True)
|
|
|
|
class Meta:
|
|
model = User
|
|
fields = ('email', 'password', 'password_confirm')
|
|
|
|
def validate(self, attrs):
|
|
if attrs['password'] != attrs['password_confirm']:
|
|
raise serializers.ValidationError("Passwords don't match.")
|
|
return attrs
|
|
|
|
def create(self, validated_data):
|
|
validated_data.pop('password_confirm')
|
|
user = User.objects.create_user(
|
|
email=validated_data['email'],
|
|
password=validated_data['password']
|
|
)
|
|
return user
|
|
|
|
|
|
class UserLoginSerializer(serializers.Serializer):
|
|
email = serializers.EmailField()
|
|
password = serializers.CharField()
|
|
|
|
def validate(self, attrs):
|
|
email = attrs.get('email')
|
|
password = attrs.get('password')
|
|
|
|
if email and password:
|
|
user = authenticate(username=email, password=password)
|
|
if not user:
|
|
raise serializers.ValidationError('Invalid credentials.')
|
|
if not user.is_active:
|
|
raise serializers.ValidationError('User account is disabled.')
|
|
attrs['user'] = user
|
|
else:
|
|
raise serializers.ValidationError('Must include email and password.')
|
|
|
|
return attrs
|
|
|
|
|
|
class UserSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = User
|
|
fields = ('id', 'email', 'wallet_balance', 'created_at')
|
|
read_only_fields = ('id', 'wallet_balance', 'created_at') |