Created
January 17, 2023 20:01
-
-
Save tomleo/eedd988750a680b3f25e426c5fc3574a to your computer and use it in GitHub Desktop.
Revisions
-
tomleo created this gist
Jan 17, 2023 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,11 @@ # Login Users with Email A response to: [Django: How to Log Users In With Their Email](https://ctrlzblog.com/how-to-log-users-in-with-their-email/) There's an easier way than creating a custom user type. All you need to change is the `AUTHENTICATION_BACKENDS` setting! See the example `account` app files in this gist! Thanks! -- [Tom](https://tomleo.com) 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,23 @@ # accounts/backends.py from django.contrib.auth.backends import ModelBackend from django.contrib.auth import get_user_model UserModel = get_user_model() class AccountModelBackend(ModelBackend): """ Tweaked ModelBackend so that users can be authenticated by passing in their email instead of username """ def authenticate(self, request, email=None, password=None, **kwargs): if email is None or password is None: return try: user = UserModel._default_manager.get(email=email) except UserModel.DoesNotExist: # Run the default password hasher once to reduce the timing # difference between an existing and a nonexistent user (#20760). UserModel().set_password(password) else: if user.check_password(password) and self.user_can_authenticate(user): return user 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,14 @@ # accounts/controllers.py from account.exceptions import InvalidCredentialsException from django.utils.translation import gettext_lazy as _ from django.contrib.auth.models import UserManager from django.contrib.auth import authenticate, login as contrib_login def login(email, password, request): normalized_email = UserManager.normalize_email(email) user = authenticate(email=normalized_email, password=password) if not user: raise InvalidCredentialsException() else: contrib_login(request, user) return user 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,3 @@ # accounts/exceptions.py class InvalidCredentialsException(Exception): pass 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,3 @@ AUTHENTICATION_BACKENDS = ( 'account.backends.AccountModelBackend', )