voice-clone / accounts /serializers.py
renator's picture
Setup Accounts and add Authentication on this
52a3fd6
raw
history blame
1.42 kB
from django.contrib.auth.models import User
from rest_framework import serializers
from django.contrib.auth import authenticate
class UserSerializer(serializers.ModelSerializer):
password = serializers.CharField(write_only=True)
def create(self, validated_data):
user = User.objects.create_user(
username=validated_data['username'],
password=validated_data['password']
)
return user
class Meta:
model = User
fields = ('id', 'username', 'password')
class LoginSerializer(serializers.Serializer):
username = serializers.CharField(max_length=150)
password = serializers.CharField(max_length=128, write_only=True)
def validate(self, data):
username = data.get('username')
password = data.get('password')
print(username,password)
if username and password:
user = authenticate(username=username, password=password)
print("user",user)
if user:
if user.is_active:
data['user'] = user
else:
raise serializers.ValidationError("User is not active.")
else:
raise serializers.ValidationError("Unable to log in with the provided credentials.")
else:
raise serializers.ValidationError("Must include 'username' and 'password'.")
return data