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 2 years, 7 months ago
  2. Case Insensitive Authentication Backend by ericflo 2 years, 10 months ago
  3. OracleAuthBackend by nosrednakram 2 years, 5 months ago
  4. MoinMoin auth backend by yourcelf 1 year, 4 months ago
  5. Use email addresses for user name by chris 4 years, 11 months ago

Comments

wozozo (on May 14, 2010):

thanks!

#

(Forgotten your password?)