# Login using email showing wrong error message?

**URL:** <https://forum.djangoproject.com/t/login-using-email-showing-wrong-error-message/3563>\
**Category:** Using Django\
**Created:** [July 22, 2020, 7:23am UTC](https://forum.djangoproject.com/t/login-using-email-showing-wrong-error-message/3563 "2020-07-22T07:23:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mtrazakhan](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mtrazakhan/32/1026_2.png) [@mtrazakhan](https://forum.djangoproject.com/u/mtrazakhan)\
**Post date:** [July 22, 2020, 7:23am UTC](https://forum.djangoproject.com/t/login-using-email-showing-wrong-error-message/3563/1 "2020-07-22T07:23:24Z")

</div>

My Email auth Backend:-

```
from django.contrib.auth import backends, get_user_model
from django.db.models import Q
UserModel = get_user_model()

class EmailAuthBackend(backends.ModelBackend):

    def authenticate(self, request, username=None, password=None, **kwargs):
        if username is None:
            username = kwargs.get(UserModel.USERNAME_FIELD)
        try:
            # user = UserModel._default_manager.get_by_natural_key(username)
            # You can customise what the given username is checked against, here I compare to both username and email fields of the User model
            user = UserModel.objects.get(Q(username__iexact=username) | Q(email__iexact=username))
        except UserModel.DoesNotExist:
            # Run the default password hasher once to reduce the timing
            # difference between an existing and a nonexistent user (#20760).
            UserModel().set_password(password)
        else:
            if user.check_password(password) and self.user_can_authenticate(user):
                return user
        return super().authenticate(request, username, password, **kwargs)

```

My Custom Auth Form:-

```
class CustomAuthForm(AuthenticationForm):
    username = UsernameField(label='Username/Email id',widget=forms.TextInput(attrs={'autofocus': True}))
    password = forms.CharField(
        label=_("Password"),
        strip=False,
        widget=forms.PasswordInput(attrs={'autocomplete': 'current-password'}),
    )

    error_messages = {
        'invalid_login': _(
            "Please enter a correct %(username)s and password."
        ),
        'inactive': _("User Profile is Blocked by Admin."),
        'deleted_user': _("User doesn't exist")
    }

    def __init__ (self, request=None, *args, **kwargs):

        self.request = request
        self.user_cache = None
        super(). __init__ (*args, **kwargs)

        # Set the max length and label for the "username" field.
        self.username_field = User._meta.get_field(User.USERNAME_FIELD)
        username_max_length = self.username_field.max_length or 254
        self.fields['username'].max_length = username_max_length
        self.fields['username'].widget.attrs['maxlength'] = username_max_length
        if self.fields['username'].label is None:
            self.fields['username'].label = capfirst(self.username_field.verbose_name)

    def clean(self):
        username = self.cleaned_data.get('username')
        password = self.cleaned_data.get('password')

        if username is not None and password:
            self.user_cache = authenticate(self.request, username=username, password=password)
            if self.user_cache is None:
                try:
                    user_temp = User.objects.get(username=username)
                except:
                    user_temp = None

                if user_temp is not None and user_temp.check_password(password):
                    self.confirm_login_allowed(user_temp)
                else:
                    raise forms.ValidationError(
                        self.error_messages['invalid_login'],
                        code='invalid_login',
                        params={'username': self.username_field.verbose_name},
                    )

        return self.cleaned_data

    def confirm_login_allowed(self, user):
        if user.deleted_at is not None:
            raise forms.ValidationError(
                self.error_messages['deleted_user'],
                code='deleted_user',
            )

        if not user.is_active:
            raise forms.ValidationError(
                self.error_messages['inactive'],
                code='inactive',
            )

    def get_user(self):
        return self.user_cache

    def get_invalid_login_error(self):
        return forms.ValidationError(
            self.error_messages['invalid_login'],
            code='invalid_login',
            params={'username': self.username_field.verbose_name},
        )

```

while login using email showing wrong message when user is inactive  
message for inactive user is “User Profile is Blocked by Admin.”  
when login using username, it is showing correct message

 ![wrongErrorMessage](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/9/955ca1650f63f4d806215e5b89f5a01e3166e02c.png)

Showing correct error message while login using username:-

![CorrectErrorMessage](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/6/628498c6ec8fef8e2640ba53f63884fe9ad3245f.png)

---

<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:** [July 22, 2020, 8:32am UTC](https://forum.djangoproject.com/t/login-using-email-showing-wrong-error-message/3563/3 "2020-07-22T08:32:07Z")

</div>

In your test, are you entering a wrong password, or are you leaving it blank?

(Your code will behave differently in those two situations.)

Ken

---

<div class="post-metadata">

**Author:** ![mtrazakhan](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mtrazakhan/32/1026_2.png) [@mtrazakhan](https://forum.djangoproject.com/u/mtrazakhan)\
**Post date:** [July 22, 2020, 8:33am UTC](https://forum.djangoproject.com/t/login-using-email-showing-wrong-error-message/3563/5 "2020-07-22T08:33:41Z")

</div>

I am entering correct password in both cases but error message is different

---

<div class="post-metadata">

**Author:** ![mtrazakhan](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mtrazakhan/32/1026_2.png) [@mtrazakhan](https://forum.djangoproject.com/u/mtrazakhan)\
**Post date:** [November 4, 2020, 4:36am UTC](https://forum.djangoproject.com/t/login-using-email-showing-wrong-error-message/3563/6 "2020-11-04T04:36:53Z")

</div>

I have solved this problem

---

<div class="post-metadata">

**Author:** ![anon-pradip](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anon-pradip/32/4486_2.png) [@anon-pradip](https://forum.djangoproject.com/u/anon-pradip)\
**Post date:** [May 10, 2021, 9:33am UTC](https://forum.djangoproject.com/t/login-using-email-showing-wrong-error-message/3563/7 "2021-05-10T09:33:55Z")

</div>

Hello! Can you share me the idea how did you solve the error?
