Home > front end >  Storing password works but logging in doesn't
Storing password works but logging in doesn't

Time:07-07

Trying to store the pw hashed works but trying to login with the first_name last_name doesn't work. Etc if the combined pw is named ThisPw I should be able to store it hashed and then login with it.

    if add_form.is_valid(): 
        post = request.POST
        print(post['email'])
        new_password = post['first_name'] post['last_name']
        new_user = Profile.objects.create_user(
            email = post['email'],
            password=make_password(new_password),
            first_name=post['first_name'],
            last_name=post['last_name'],
            phone_number=post['phone_number'],
        )
        print(new_password)
        new_user.save()
        print(add_form)
        return redirect('add')

CodePudding user response:

Do not use make_password() if you are passing the password to the default create_user() method.

    if add_form.is_valid(): 
        post = request.POST
        new_password = post['first_name'] post['last_name']
        new_user = Profile.objects.create_user(
            email = post['email'],
            password=new_password,
            first_name=post['first_name'],
            last_name=post['last_name'],
            phone_number=post['phone_number'],
        )
        new_user.save()
        return redirect('add')

The default create_user() method is calling user.set_password(password)

class UserManager(BaseUserManager):

    def _create_user(self, username, email, password, **extra_fields):
        """
        Create and save a user with the given username, email, and password.
        """
        # ...
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_user(self, username, email=None, password=None, **extra_fields):
        extra_fields.setdefault('is_staff', False)
        extra_fields.setdefault('is_superuser', False)
        return self._create_user(username, email, password, **extra_fields)


class AbstractBaseUser(models.Model):
    # Stores the raw password if set_password() is called so that it can
    # be passed to password_changed() after the model is saved.
    _password = None

    def set_password(self, raw_password):
        self.password = make_password(raw_password)
        self._password = raw_password
  • Related