Skip to content

Instantly share code, notes, and snippets.

@rainerborene
Created September 18, 2010 21:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rainerborene/586056 to your computer and use it in GitHub Desktop.
Save rainerborene/586056 to your computer and use it in GitHub Desktop.
from django.contrib.auth.backends import ModelBackend
from django.contrib.auth.models import User
class EmailBackend(ModelBackend):
def authenticate(self, **credentials):
if 'username' in credentials:
return super(EmailBackend, self).authenticate(**credentials)
try:
user = User.objects.get(email=credentials.get('email'))
if user.check_password(credentials.get('password')):
return user
except User.DoesNotExist:
return None
@thiagosoara
Copy link

i'm still noobie on django framework, could you tell me what's the path to place this code? i really got no idea.

@rainerborene
Copy link
Author

All you have to do is change the AUTHENTICATION_BACKENDS on settings.py. Read the [documentation](http://docs.djangoproject.com/en/1.3/ref/settings
/#authentication-backends) for more information. Remember that you should save this file (normally named backend.py) in any Django app you want.

@thiagosoara
Copy link

on the settings.py , i created:

AUTHENTICATION_BACKENDS = (
'myAutenticate',
'django.contrib.auth.backends.ModelBackend',
)

then, i created an app named myAutenticate. After, i created the backends.py inside the folder myAutenticate and placed the that you suggested on this gist topic.

now, i'm getting the following error message: "Error importing authentication backend myAutenticat: "No module named myAutenticat"

do i miss something?

@rainerborene
Copy link
Author

Yes, you missed the complete path of your backend. You should use the following pattern: projectname.applicationname.backends.EmailBackend. Remember that Google is your friend ;-)

@thiagosoara
Copy link

thanks dude,

Now i'm getting this(I swear I have researched this one enough on the internet before ask you ;) ):

Exception Value:
Module "goow.myAutenticate" does not define a "EmailBackend" authentication backend

Maybe you'll need:
Django Version: 1.2.5
Exception Type: ImproperlyConfigured
Exception Location: /usr/local/lib/python2.6/dist-packages/django/contrib/auth/init.py in load_backend, line 22
Python Executable: /usr/bin/python2.6
Python Version: 2.6.6

I really appreciate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment