Login with email or username

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
from django.conf import settings
from django.contrib.auth.models import User

class EmailOrUsernameModelBackend(object):
    def authenticate(self, username=None, password=None):
        if '@' in username:
            kwargs = {'email': username}
        else:
            kwargs = {'username': username}
        try:
            user = User.objects.get(**kwargs)
            if user.check_password(password):
                return user
        except User.DoesNotExist:
            return None

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

More like this

  1. Email or username authentication with masquerading by petrilli 4 years, 9 months ago
  2. OracleAuthBackend by nosrednakram 4 years, 8 months ago
  3. Case Insensitive Authentication Backend by ericflo 5 years, 1 month ago
  4. New forms signup validation by Batiste 7 years ago
  5. email backend which use sendmail subprocess by pawnhearts 4 years, 3 months ago

Comments

wozozo (on May 14, 2010):

thanks!

#

(Forgotten your password?)