Skip to content

Commit

Permalink
fix(api): user password hash while serializing
Browse files Browse the repository at this point in the history
  • Loading branch information
moonlitgrace committed Nov 15, 2024
1 parent bf0cacc commit 0754cc1
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
5 changes: 1 addition & 4 deletions backend/backend/apps/user/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ def _create_user(self, email: str, password: str | None, **extra_fields: Any):
raise ValueError("Email is required")
email = self.normalize_email(email)
user: User = self.model(email=email, **extra_fields)
if password is not None:
user.set_password(password)
else:
user.set_unusable_password()
user.set_password(password)
user.save(using=self._db)
return user

Expand Down
1 change: 0 additions & 1 deletion backend/backend/apps/user/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from django.core.exceptions import ValidationError
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
from django.utils.translation import gettext_lazy as _
Expand Down
7 changes: 6 additions & 1 deletion backend/backend/apps/user/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('id', 'email', 'date_joined')
fields = ('id', 'email', 'password', 'date_joined')
read_only_fields = ('date_joined',)
extra_kwargs = {'password': {'write_only': True}}

def create(self, validated_data):
user = User.objects.create_user(**validated_data)
return user


class ProfileSerializer(serializers.ModelSerializer):
Expand Down

0 comments on commit 0754cc1

Please sign in to comment.