I am currently implementing the authentication for a Django application, I am writing. Following code of the Thinkster Django course, I implemented the whole registration process, but I cannot login, because the password is not getting hashed, when registering a user.
Here is my custom User model and the create_user
function.
class UserManager(BaseUserManager)
def create_user(self, username, email, password=None):
if username is None:
raise TypeError('Users must have a username.')
if email is None:
raise TypeError('Users must have an email address.')
user = self.model(username=username, email=self.normalize_email(email))
user.set_password(password)
user.save()
return user
def create_superuse(self, username, email, password):
if password is None:
raise TypeError('Superusers must have a password.')
user = self.create_user(username, email, password)
user.is_superuser = True
user.is_staff = True
user.save()
return user
class User(AbstractBaseUser, PermissionsMixin):
username = models.CharField(db_index=True, max_length=255, unique=True)
email = models.EmailField(db_index=True, unique=True)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username']
objects = UserManager()
As you can see, I am explicitly calling the set_password
function but I cannot find out, why it does not get properly executed?
My Serializer
, where I create the user is as follows:
class RegistrationSerializer(serializers.ModelSerializer):
password = serializers.CharField(
max_length=128,
min_length=8,
write_only=True
)
token = serializers.CharField(max_length=255, read_only=True)
class Meta:
model = User
fields = ['email', 'username', 'password', 'token']
def create(self, validated_data):
return User.objects.create_user(**validated_data)
Please note that instead of return User.objects.create_user(**validated_data)
, I also tried doing return get_user_model().objects.create_user(**validated_data)
, as it was a suggestion at another question, but that did not work either.
I also post my view, in case something is wrong there, but I really don't think that's the case.
class RegistrationAPIView(APIView):
permission_classes = (AllowAny,)
renderer_classes = (UserJSONRenderer,)
serializer_class = RegistrationSerializer
def post(self, request):
user = request.data.get('user', {})
serializer = self.serializer_class(data=user)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
Side Note: In case that is relevant, I am sending requests with Postman
and all the responses I get back seem completely right. But when I browse my SQLite, I see that the password was not hashed. That also results in the users not being able to log in, because in the login process the password gets hashed and then compared with the one in the database.
Side Note 2: When I register a user via the command line with python manage.py createsuperuser
, it gets a hashed password and everything works as I would normally expect.
PASSWORD_HASHERS
andmake_password
method?? docs.djangoproject.com/en/1.10/ref/settings/… docs.djangoproject.com/en/1.8/topics/auth/passwords/…PASSWORD_HASHERS
list, but where exactly should I write mymake_password
function?create_user
function declared insideUserManager
class?:)