I would like to create an authentication backend that allows users to log_in only using their email (no username, no password).
Here is what I tried.
backends.py:
from django.conf import settings from django.contrib.auth.models import User class EmailAuthBackend(object): def authenticate(self, username=None, password=None): try: user = User.objects.get(email=username) if user: return user except User.DoesNotExist: return None settings.py:
AUTHENTICATION_BACKENDS = ( 'path_to.backends.EmailAuthBackend', 'django.contrib.auth.backends.ModelBackend', ) html:
<form method="post" action="{% url myproject.views.test %}"> {% csrf_token %} <input type="text" name="email" value=""/> <button type="submit">Valider</button> </form> view:
def test(request): email = '' if 'email' in request.POST: email = request.POST.get('email') if not User.objects.filter(email=email): User.objects.create(email=email) user = authenticate(username=email) if user is not None: if user.is_active: auth_login(request, user) return HttpResponseRedirect(reverse('home')) It doesn't work, the user is not authenticated. And I also have this error when I go to the /admin:
AttributeError at /admin/logout/ 'EmailAuthBackend' object has no attribute 'get_user'