# Always getting None in authenticate()

**URL:** <https://forum.djangoproject.com/t/always-getting-none-in-authenticate/29922>\
**Category:** Mystery Errors\
**Created:** [April 7, 2024, 11:50am UTC](https://forum.djangoproject.com/t/always-getting-none-in-authenticate/29922 "2024-04-07T11:50:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gaurav-Sarswat](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/gaurav-sarswat/32/20441_2.png) [@Gaurav-Sarswat](https://forum.djangoproject.com/u/Gaurav-Sarswat)\
**Post date:** [April 7, 2024, 11:50am UTC](https://forum.djangoproject.com/t/always-getting-none-in-authenticate/29922/1 "2024-04-07T11:50:52Z")

</div>

I am creating a web application using Django and in login i am getting non in authentication can you please help

Models.py

```auto
class CustomUser(AbstractBaseUser):
    email = models.EmailField(verbose_name='email address', max_length=255, unique=True)
    name = models.CharField(max_length=255)
    address = models.CharField(max_length=255)
    country = models.CharField(max_length=255)
    qualifications = models.TextField(blank=True)
    skills = models.TextField(blank=True)
    exprence = models.IntegerField(max_length=255)
    exp_details = models.CharField(max_length=255)
    role = models.CharField(max_length=50, default='user') # Add the 'role' field
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)
    is_staff = models.BooleanField(default=False)

    objects = CustomUserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['name']

    def __str__ (self):
        return self.email

    def has_perm(self, perm, obj=None):
        return self.is_admin

    def has_module_perms(self, app_label):
        return True

```

forms.py

```auto
class SignUpForm(UserCreationForm):
    qualifications = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Enter qualifications as comma-separated values'}), required=False)
    skills = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Enter skills as comma-separated values'}), required=False)

    class Meta:
        model = CustomUser
        fields = ['email', 'name', 'address', 'country', 'qualifications', 'skills', 'exprence', 'exp_details']

    def clean_qualifications(self):
        qualifications = self.cleaned_data.get('qualifications')
        return qualifications.split(',') if qualifications else []

    def clean_skills(self):
        skills = self.cleaned_data.get('skills')
        return skills.split(',') if skills else []

    def save(self, commit=True):
        user = super().save(commit=False)
        user.qualifications = self.cleaned_data["qualifications"]
        user.skills = self.cleaned_data["skills"]
        user.set_password(self.cleaned_data["password1"]) # Hash the password using make_password
        if commit:
            user.save()
        return user
class LoginForm(forms.Form):
    email = forms.EmailField(label='Email')
    password = forms.CharField(label='Password', widget=forms.PasswordInput)

```

views.py

```auto
def signup(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('login') # Redirect to login page after successful signup
    else:
        form = SignUpForm()
    return render(request, 'signup.html', {'form': form})

def custom_login(request):
    error_message = None
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data['email']
            password = form.cleaned_data['password']

            # Check if the email exists in the database
            user = authenticate(username=email, password=password)
            if user is not None:
                # Check the password using check_password
                if check_password(password, user.password):
                    # Passwords match, login the user
                    login(request, user)
                    messages.success(request, 'Login successful.')
                    return redirect('home') # Redirect to the home page after login
                else:
                    # Passwords do not match
                    error_message = 'Invalid email or password.'
            else:
                # User not found or authentication failed
                error_message = 'Invalid email or password.'
        else:
            # Form validation failed
            error_message = 'Form is not valid.'
    else:
        form = LoginForm()

    return render(request, 'login.html', {'form': form, 'error_message': error_message})

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 7, 2024, 12:42pm UTC](https://forum.djangoproject.com/t/always-getting-none-in-authenticate/29922/2 "2024-04-07T12:42:25Z")

</div>

First, is there a particular reason why you’re trying to do a low-level authentication instead of using the [Django-provided LoginViews](https://docs.djangoproject.com/en/5.0/topics/auth/default/#module-django.contrib.auth.views)?

Second, there’s no need for you to use a `check_password` function, that’s the purpose of the [`authenticate`](https://docs.djangoproject.com/en/5.0/topics/auth/default/#django.contrib.auth.authenticate) method. Also see the example in [the login function docs](https://docs.djangoproject.com/en/5.0/topics/auth/default/#django.contrib.auth.login).

Check your database to ensure that the user accounts are created with the correct data in them. Verify your settings regarding the custom user. Try testing the `authenticate` method in the Django shell.

Verify that your `CustomUserManager` does everything it needs to do as documented for [Writing a manager for a custom user model](https://docs.djangoproject.com/en/5.0/topics/auth/customizing/#writing-a-manager-for-a-custom-user-model)

In fact, you may want to review the entire section of the docs for [Substituting a custom User model](https://docs.djangoproject.com/en/5.0/topics/auth/customizing/#substituting-a-custom-user-model) to ensure you’ve done everything needed.

If you need further assistance, please post the `CustomUserManager`, the `signup.html` and `login.html` templates, your `AUTH_USER_MODEL` setting from your settings. Also verify that you started this project with your custom user model and that the database was first migrated with this custom user model being active.
