Login

Use email addresses for user name for django 1.3

Author:
kidzik
Posted:
June 13, 2011
Language:
Python
Version:
1.3
Score:
5 (after 5 ratings)

Chris' code adapted to django 1.3. Basicly E-mail authorisation backend.

Put it as one of your AUTHENTICATION_BACKENDS in settings.py:

AUTHENTICATION_BACKENDS = (
    'community.auth.EmailBackend',
)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""
@author: chris http://djangosnippets.org/snippets/1845/
updated to 1.3 by Łukasz Kidziński
"""
from django.contrib.auth.models import User
from django.core.validators import email_re

class EmailBackend:
    """
    Authenticate with e-mail.

    Use the  e-mail, and password
    
    Should work with django 1.3
    """

    supports_object_permissions = False
    supports_anonymous_user = False
    supports_inactive_user = False

    def authenticate(self, username=None, password=None):
        if email_re.search(username):
            try:
                user = User.objects.get(email=username)
            except User.DoesNotExist:
                return None
        else:
            #We have a non-email address username we should try username
            try:
                user = User.objects.get(username=username)
            except User.DoesNotExist:
                return None
        if user.check_password(password):
            return user
        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. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

kidzik (on June 14, 2011):

Link to orginal version was (and still is) in comment on right. Nevertheless I've added it to code as well. Sorry for inconvenience :)

#

darek (on June 15, 2011):

:-)

Because I'm a little bit persnickety, I have to say that you've made a mistake "cris => chris" ;-p

Regards.

#

trevorc (on June 9, 2012):

That snippet uses the undocumented 'email_re' which has broken and could break again. Besides, it didn't work for me. It also assumes that a valid email string couldn't be a username. Here's my version:

def authenticate(self, username=None, password=None):
    user = None
    if '@' in username:
        try:
            user = User.objects.get(email=username)
        except User.DoesNotExist:
            pass

    if not user:
        #We have a non-email address username we should try username
        try:
            user = User.objects.get(username=username)
        except User.DoesNotExist:
            return None

    if user.check_password(password):
        return user
    return None

#

Please login first before commenting.