Added some logic
This commit is contained in:
37
backend/apps/authentication/serializers.py
Normal file
37
backend/apps/authentication/serializers.py
Normal file
@@ -0,0 +1,37 @@
|
||||
from django.contrib.auth.password_validation import validate_password as django_validate_password
|
||||
from django.core.exceptions import ValidationError
|
||||
from rest_framework import serializers
|
||||
from apps.user.models import User
|
||||
|
||||
|
||||
class RegisterSerializer(serializers.ModelSerializer):
|
||||
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ["username", "email", "password"]
|
||||
extra_kwargs = {
|
||||
"password": {"write_only": True}
|
||||
}
|
||||
|
||||
def validate_email(self, value):
|
||||
if User.objects.filter(email=value).exists():
|
||||
raise serializers.ValidationError("Email already taken")
|
||||
return value
|
||||
|
||||
def validate_password(self, value):
|
||||
try:
|
||||
django_validate_password(value)
|
||||
except ValidationError as error:
|
||||
raise serializers.ValidationError(error.messages)
|
||||
|
||||
return value
|
||||
|
||||
def create(self, validated_data):
|
||||
user = User.objects.create_user(
|
||||
validated_data["username"],
|
||||
validated_data["email"],
|
||||
validated_data["password"]
|
||||
)
|
||||
|
||||
|
||||
return user
|
||||
Reference in New Issue
Block a user