Cannot login with user credentials

42 views
Skip to first unread message

Robin Lery

unread,
Jul 23, 2016, 3:19:52 AM7/23/16
to django...@googlegroups.com
I have a made custom user model.

class CustomUserManager(BaseUserManager):
    def _create_user(self, email, username, password, first_name, last_name, date_of_birth, gender, mobile_number,
                     is_active, is_admin, is_superuser):
        """
        Creates and saves a user with given email and password
        """
        if not email:
            raise ValueError('Email must be set')

        email = self.normalize_email(email)
        now = timezone.now()

        user = self.model(
            email=email,
            username=username,
            first_name=first_name,
            last_name=last_name,
            date_of_birth=date_of_birth,
            gender=gender,
            mobile_number=mobile_number,
            date_joined=now,
            is_active=is_active,
            is_admin=is_admin,
            is_superuser=is_superuser,
        )
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_user(self, email, username, password, first_name, last_name, date_of_birth, gender, mobile_number, **extra_fields):
        user = self._create_user(email, username, password, first_name, last_name, date_of_birth, gender, mobile_number, True, False, False, **extra_fields)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, username, password, first_name, last_name, date_of_birth, gender, mobile_number, **extra_fields):
        user = self._create_user(email, username, password, first_name, last_name, date_of_birth, gender, mobile_number, True, True, True, **extra_fields)
        user.save(using=self._db)
        return user


class CustomUser(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(_('email address'), max_length=254, unique=True)
    username = models.CharField(_('user name'), max_length=254, unique=True)
    first_name = models.CharField(_('first name'), max_length=30)
    last_name = models.CharField(_('last name'), max_length=30)
    date_of_birth = models.DateField()
    gender = models.CharField(choices=GENDERTYPE, max_length=1)
    mobile_number = models.IntegerField(unique=True)
    date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)

    objects = CustomUserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username', 'first_name', 'last_name', 'date_of_birth', 'gender', 'mobile_number']
    ...
    ...


And to process the model, I have a custom user creation forms.py

class UserCreationForm(forms.ModelForm):
    password = forms.CharField(label='Password', widget=forms.PasswordInput)

    class Meta:
        model = CustomUser
        fields = ('email', 'username', 'first_name', 'last_name', 'date_of_birth', 'gender', 'mobile_number')

    def save(self, commit=True):
        # Save the provided password in hashed format
        user = super(UserCreationForm, self).save(commit=False)
        user.set_password(self.cleaned_data["password"])
        print self.cleaned_data["password"]
        if commit:
            user.save()
        return user


class UserChangeForm(forms.ModelForm):
    password = ReadOnlyPasswordHashField()

    class Meta:
        model = CustomUser
        fields = (
            'email', 'username', 'first_name', 'last_name', 'date_of_birth', 'gender', 'mobile_number', 'is_active',
            'is_admin'
        )

    def clean_password(self):
        return self.initial["password"]

I can save and create a new user. Also I can see the hashed password in the admin. However when I try to login with the `email` and `password` of the new user, I am not being authenticated. To check if I am actually getting any password, I tried printing the self.cleaned_data["password"] in the UserCreationForm, and in the console I can see the password I gave. What am I doing wrong here? Please help me.

Thank you.

Robin Lery

unread,
Jul 23, 2016, 3:31:15 AM7/23/16
to django...@googlegroups.com
But again, with the superuser's credentials, I can login

James Schneider

unread,
Jul 24, 2016, 3:20:43 PM7/24/16
to django...@googlegroups.com


> I can save and create a new user. Also I can see the hashed password in the admin. However when I try to login with the `email` and `password` of the new user, I am not being authenticated. To check if I am actually getting any password, I tried printing the self.cleaned_data["password"] in the UserCreationForm, and in the console I can see the password I gave. What am I doing wrong here? Please help me.
>
>

If you're only using the default authentication back-end, did you set AUTH_USER_MODEL accordingly?

https://docs.djangoproject.com/en/1.9/topics/auth/customizing/#substituting-a-custom-user-model

I see you've set USERNAME_FIELD to 'email', but since you've mentioned you can log in using a super user (and I presume a user name and not an email address), I suspect you are missing the AUTH_USER_MODEL setting.

Are you using a custom authentication back-end that can handle looking for an email address as a username? If you want logins via both a user name and email address to work, you'll need to implement that with a custom back-end.

https://docs.djangoproject.com/en/1.9/topics/auth/customizing/#writing-an-authentication-backend

-James

Reply all
Reply to author
Forward
0 new messages