This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Agrega manejador de sesiones por correo electrónico
- Loading branch information
Showing
2 changed files
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# encoding=utf-8 | ||
|
||
""" | ||
Módulo con funciones para complementar el manejo de sesiones | ||
de Django. | ||
""" | ||
|
||
from django.contrib.auth import get_user_model | ||
from django.contrib.auth.backends import ModelBackend | ||
|
||
class EmailBackend(ModelBackend): | ||
""" | ||
Implementa un manejador de autenticación que permite iniciar | ||
sesión con correo electrónico en vez de nombre de usuario. | ||
""" | ||
|
||
def authenticate(self, username=None, password=None, **kwargs): | ||
""" | ||
Verifica si existe un usuario con el correo electrónico PASADO | ||
COMO 'username', en cuyo caso intenta iniciar sesión. | ||
""" | ||
|
||
UserModel = get_user_model() | ||
try: | ||
user = UserModel.objects.get(email=username) | ||
except UserModel.DoesNotExist: | ||
return None | ||
else: | ||
if user.check_password(password): | ||
return user | ||
return None |
This file contains 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