Created
February 8, 2012 09:55
-
-
Save rdandy/1767557 to your computer and use it in GitHub Desktop.
django backend: member logging in useing username or email
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AUTHENTICATION_BACKENDS = ( | |
'project.backends.UsernameEmailAuthBackend.UsernameEmailAuthBackend', | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib.auth.models import User | |
class UsernameEmailAuthBackend(object): | |
""" | |
Authentication with username or E-mail | |
Allows user to login using username or E-mail and password pair. | |
""" | |
def authenticate(self, username=None, password=None): | |
try: | |
if '@' in username: | |
user = User.objects.get(email=username) | |
else: | |
user = User.objects.get(username=username) | |
if user.check_password(password): | |
return user | |
except User.DoesNotExist: | |
return None | |
def get_user(self, user_id): | |
""" | |
Get a User object from the user_id. | |
""" | |
try: | |
return User.objects.get(pk=user_id) | |
except User.DoesNotExist: | |
return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment