Search code examples
djangodjango-serializer

How can a user password be stored in pbkdf2_sha256 format in django?


I am implementing custom register serializer. By the way, the password was encrypted on the admin page, so it was different from the pbkdf2_sha256. like this

!eWf3UsvTHU4dJ4F.....

I would like to get the user's password using the algorithm of pbkdf2_sha256 instead of this. What should I do? Here's my code.

class customRegisterSerializer (serializers.Serializer) :
    email = serializers.EmailField(required=allauth_settings.EMAIL_REQUIRED)
    password = serializers.CharField(required=True, write_only=True)
    user_Name = serializers.CharField(required=True)
    profile = serializers.ImageField(use_url=True)

    def validate_email(self, email):
        email = get_adapter().clean_email(email)
        if allauth_settings.UNIQUE_EMAIL:
            if email and email_address_exists(email):
                raise serializers.ValidationError(
                    _("A user is already registered with this e-mail address."))
        return email

    def validate_password (self, password: str) -> str:
        return get_adapter().clean_password(password)

    def get_cleaned_data(self):
        return {
            'email': self.validated_data.get('email', ''),
            'password': self.validated_data.get('password', ''),
            'userName': self.validated_data.get('userName', ''),
            'profile' : self.validated_data.get('profile', ''),
        }

    def save(self, request):
        adapter = get_adapter()
        user = adapter.new_user(request)
        self.cleaned_data = self.get_cleaned_data()
        adapter.save_user(request, user, self)
        setup_user_email(request, user, [])
        return user

Solution

  • I try your way to store the password but the password does not store in the right form. so, You can not store passwords by using the User model only. You need another model. create a profile model and create a serializer for it and then try this:-

    serializers.py

    class UserRegistrationSerializer(serializers.ModelSerializer):
    
        profile = ProfileSerializer(required=False)
    
        class Meta:
            model = User
            fields = ('phone', 'username', 'password', 'profile')
            extra_kwargs = {'password': {'write_only': True}}
    
        def create(self, validated_data):
            profile_data = validated_data.pop('profile')
            user = User.objects.create_user(**validated_data)
            users = Profile.objects.create(
                user=user,      
                state=profile_data['state'],
                city=profile_data['city'],
                date_Of_Birth=profile_data['date_Of_Birth'],
                address=profile_data['address']
                 )
            users.save()
            
            return users
    
    

    views.py

    class UserRegistrationView(CreateAPIView):
    
        serializer_class = UserRegistrationSerializer
        permission_classes = (IsAuthenticated,)
    
        def post(self, request):
            serializer = self.serializer_class(data=request.data)
            serializer.is_valid(raise_exception=True)
            serializer.save()
            response = {
                'success' : 'True',
                'status code' : status.HTTP_200_OK,
                'message': 'User registered  successfully',
                }
            status_code = status.HTTP_200_OK
            return Response(response, status=status_code)
    

    Hope The answer is useful.