Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

commented May 23, 2011

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

This comment has been minimized.

Copy link
Owner Author

commented May 23, 2011

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

This comment has been minimized.

Copy link

commented May 23, 2011

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

This comment has been minimized.

Copy link
Owner Author

commented May 23, 2011

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

This comment has been minimized.

Copy link

commented May 23, 2011

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
You can’t perform that action at this time.