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. OracleAuthBackend by nosrednakram 3 years, 9 months ago
  2. Email or username authentication with masquerading by petrilli 3 years, 11 months ago
  3. MoinMoin auth backend by yourcelf 2 years, 7 months ago
  4. Use email addresses for user name for django 1.3 by kidzik 1 year, 11 months ago
  5. Use email addresses for user name by chris 6 years, 2 months ago

Comments

wozozo (on May 14, 2010):

thanks!

#

(Forgotten your password?)