Skip to content

Instantly share code, notes, and snippets.

@heygema
Last active March 26, 2017 15:51
Show Gist options
  • Save heygema/331e78d3ab1c1c8dfb54b6bc5810fa7c to your computer and use it in GitHub Desktop.
Save heygema/331e78d3ab1c1c8dfb54b6bc5810fa7c to your computer and use it in GitHub Desktop.
MyCustomAuthBackend for Django
# settings file:
AUTH_USER_MODEL = 'accounts.User'
AUTHENTICATION_BACKENDS = ('accounts.backend.MyBackend', )
# backend.py in usercp (APP) folder
from models import Customer
from django.contrib.auth.hashers import check_password
# Taken from http://www.djangorocks.com/tutorials/creating-a-custom-authentication-backend/creating-a-simple-authentication-backend.html
class MyAuth:
def authenticate(self, email=None, password=None):
try:
user = User.objects.get(email=email)
if check_password(password, user.password):
return user
else:
return None
except User.DoesNotExist:
# No user was found, return None - triggers default login failed
return None
# Required for your backend to work properly - unchanged in most scenarios
def get_user(self, 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