Advertisement
Guest User

Email and Username Authentication Backend

a guest
Jul 11th, 2016
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.66 KB | None | 0 0
  1. from django.contrib.auth.backends import ModelBackend
  2. from django.contrib.auth.hashers import check_password
  3. from django.contrib.auth import get_user_model
  4.  
  5. class LoginBackend(ModelBackend):
  6.  
  7.     def authenticate(self, username=None, password=None, **kwargs):
  8.         user = super(LoginBackend, self).authenticate(username, password, **kwargs)
  9.         if user:
  10.             return user
  11.  
  12.         UserModel = get_user_model()
  13.         try:
  14.             user = UserModel.objects.get(email=username)
  15.         except UserModel.DoesNotExist:
  16.             return None
  17.  
  18.         if check_password(password, user.password):
  19.             return user
  20.         return None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement