mirror of
https://github.com/TurTaskProject/TurTaskWeb.git
synced 2025-12-19 14:04:07 +01:00
40 lines
1.3 KiB
Python
40 lines
1.3 KiB
Python
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
|
|
from rest_framework import serializers
|
|
from .models import CustomUser
|
|
|
|
|
|
class MyTokenObtainPairSerializer(TokenObtainPairSerializer):
|
|
@classmethod
|
|
def get_token(cls, user):
|
|
"""
|
|
Get the token for the user and add custom claims, such as 'username'.
|
|
"""
|
|
token = super(MyTokenObtainPairSerializer, cls).get_token(user)
|
|
token['username'] = user.username
|
|
return token
|
|
|
|
|
|
class CustomUserSerializer(serializers.ModelSerializer):
|
|
"""
|
|
Serializer for CustomUser model.
|
|
"""
|
|
email = serializers.EmailField(required=True)
|
|
username = serializers.CharField(required=True)
|
|
password = serializers.CharField(min_length=8, write_only=True)
|
|
|
|
class Meta:
|
|
model = CustomUser
|
|
fields = ('email', 'username', 'password')
|
|
extra_kwargs = {'password': {'write_only': True}}
|
|
|
|
def create(self, validated_data):
|
|
"""
|
|
Create a CustomUser instance with validated data, including password hashing.
|
|
"""
|
|
password = validated_data.pop('password', None)
|
|
instance = self.Meta.model(**validated_data)
|
|
if password is not None:
|
|
instance.set_password(password)
|
|
instance.save()
|
|
return instance
|