start auth apis and packages, redis, celery and run with docker
This commit is contained in:
42
core/apps/accounts/serializers/auth.py
Normal file
42
core/apps/accounts/serializers/auth.py
Normal file
@@ -0,0 +1,42 @@
|
||||
from django.db import transaction
|
||||
from django.contrib.auth import get_user_model
|
||||
|
||||
from rest_framework import serializers
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
class LoginSerializer(serializers.Serializer):
|
||||
phone = serializers.CharField()
|
||||
password = serializers.CharField()
|
||||
|
||||
def validate(self, data):
|
||||
try:
|
||||
user = User.objects.get(phone=data.get('phone'))
|
||||
except User.DoesNotExist:
|
||||
raise serializers.ValidationError({'detail': 'User not found'})
|
||||
else:
|
||||
if not user.check_password(data.get('password')):
|
||||
raise serializers.ValidationError({'detail': 'User not found'})
|
||||
data['user'] = user
|
||||
return data
|
||||
|
||||
|
||||
class RegisterSerializer(serializers.Serializer):
|
||||
phone = serializers.CharField()
|
||||
password = serializers.CharField()
|
||||
|
||||
def validate(self, data):
|
||||
if User.objects.filter(phone=data.get('phone')).exists():
|
||||
raise serializers.ValidationError({'detail': "User with this phone number already exists"})
|
||||
return data
|
||||
|
||||
def create(self, validated_data):
|
||||
with transaction.atomic():
|
||||
new_user = User.objects.create_user(
|
||||
phone=validated_data.pop('phone'),
|
||||
)
|
||||
new_user.set_password(validated_data.pop('password'))
|
||||
new_user.save()
|
||||
return new_user
|
||||
|
||||
|
||||
Reference in New Issue
Block a user