0

I am not able to login in django with correct password and email. here is my code.

backends.py

 from django.contrib.auth.models import User

    class Emailbackend(object):

        def authenticate(self, username=None, password=None, **kwargs):
            try:
                user = User.objects.get(email=username)
            except User.MultipleObjectsReturned:
                user = User.objects.filter(email=username).order_by('id').first()
            except User.DoesNotExist:
                return None
            if getattr(user, 'is_active') and user.check_password(password):
                return user
            return None

        def get_user(self, user_id):
            try:
                User.objects.get(pk=user_id)
            except User.DoesNotExist:
                return None

Settings.py

AUTHENTICATION_BACKENDS = (
    'users.backends.Emailbackend',
)
Cipher
  • 2,060
  • 3
  • 30
  • 58

2 Answers2

3

Actually, authenticate method expects an argument request should be passed. So you need to declare your Backend like this:

class Emailbackend(object):

    def authenticate(self, request, username=None, password=None, **kwargs):
        try:
        # rest of the code

Reference can be found here.

ruddra
  • 50,746
  • 7
  • 78
  • 101
0

first check the value of user in authenticate method try block. If it get correct result then check following block what user value return.

if getattr(user, 'is_active') and user.check_password(password):
    return user

If it doesn't work then go to your database and check is your password hash value available or not.

Try these link link1 link2

shafik
  • 6,098
  • 5
  • 32
  • 50