backend/users/serializers.py

24 lines
770 B
Python
Raw Normal View History

from rest_framework import serializers
from django.contrib.auth.models import User
from rest_framework_simplejwt.tokens import RefreshToken
class UserLoginSerializer(serializers.Serializer):
username = serializers.CharField()
password = serializers.CharField(write_only=True)
def validate(self, data):
from django.contrib.auth import authenticate
user = authenticate(username=data["username"], password=data["password"])
if not user:
raise serializers.ValidationError("Неверный логин или пароль")
refresh = RefreshToken.for_user(user)
return {
"username": user.username,
"access": str(refresh.access_token),
"refresh": str(refresh)
}