1

I have written a custom authentication middleware, that authenticates users based on email field and not username, but now I am unable to log into the Admin Panel of Django, using email of the Superuser/staff:

def authenticate(self, username=None, password=None):
    # Passing email as username
    try:
        user = User.objects.get(email=username)
        if user.check_password(password):
            return user
    except User.DoesNotExist:
        return None

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

It would be great if someone could help me through this?

Jerry
  • 23
  • 4
  • You need to post the code you wrote, how you implemented it, and any error you got. Otherwise I am not sure what kind of help can anyone give. – Burhan Khalid May 07 '13 at 10:42
  • 'code' def authenticate(self, username=None, password=None): # Passing email as username try: user = User.objects.get(email=username) 'code' if user.check_password(password): return user except User.DoesNotExist: return None – Jerry May 07 '13 at 10:44
  • Django does not spit out any errors for the same – Jerry May 07 '13 at 10:46
  • Please see this answer: http://stackoverflow.com/a/13661977/1474569 – Andrey Yarysh May 07 '13 at 10:48
  • I updated your question with your code. What happens if the password isn't correct? – Burhan Khalid May 07 '13 at 10:49
  • @ Andrey I have not defined a custom user model, I am just authenticating the user using email rather than username. – Jerry May 07 '13 at 10:52
  • @BurhanKhalid : it does not return anything when the password/username is incorrect. – Jerry May 07 '13 at 10:54
  • Where is the rest of the authentication backend? Does it have a `get_user()` method? – Bibhas Debnath May 07 '13 at 11:00
  • @Bibhas : Yes, the Auth Backend has the get_user() method as mentioned in the question – Jerry May 07 '13 at 11:04
  • Your code seems correct. There might be other things wrong, things we can't get from this code. E.g. - 1. These methods are inside a backend class, right? 2. It's added correctly as auth backend in `settings.py` under `AUTHENTICATION_BACKENDS`, 3. The given credentials are indeed superuser credentials, 4. the user account is active etc. Make sure these things are in place. It's hard to debug without any error message. You could also try using `pdb` inside the auth backend to be sure that it's working perfectly. – Bibhas Debnath May 07 '13 at 11:13
  • I have cross verified all the above, and as I said Django does not spit of any error. – Jerry May 07 '13 at 11:15

0 Answers0