Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - Login with Email

Tags:

django

I want django to authenticate users via email, not via usernames. One way can be providing email value as username value, but I dont want that. Reason being, I've a url /profile/<username>/, hence I cannot have a url /profile/[email protected]/.

Another reason being that all emails are unique, but it happen sometimes that the username is already being taken. Hence I'm auto-creating the username as fullName_ID.

How can I just change let Django authenticate with email?

This is how I create a user.

username = `abcd28` user_email = `[email protected]` user = User.objects.create_user(username, user_email, user_pass) 

This is how I login.

email = request.POST['email'] password = request.POST['password'] username = User.objects.get(email=email.lower()).username user = authenticate(username=username, password=password) login(request, user) 

Is there any other of of login apart from getting the username first?

like image 967
PythonEnthusiast Avatar asked May 19 '16 19:05

PythonEnthusiast


People also ask

How do I login as user in Django?

from django.contrib.auth import authenticate, login def my_view(request): username = request.POST['username'] password = request.POST['password'] user = authenticate(request, username=username, password=password) if user is not None: login(request, user) # Redirect to a success page. ... else: # Return an 'invalid ...


1 Answers

You should write a custom authentication backend. Something like this will work:

from django.contrib.auth import get_user_model from django.contrib.auth.backends import ModelBackend  class EmailBackend(ModelBackend):     def authenticate(self, request, username=None, password=None, **kwargs):         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 

Then, set that backend as your auth backend in your settings:

AUTHENTICATION_BACKENDS = ['path.to.auth.module.EmailBackend'] 

Updated. Inherit from ModelBackend as it implements methods like get_user() already.

See docs here: https://docs.djangoproject.com/en/3.0/topics/auth/customizing/#writing-an-authentication-backend

like image 77
mipadi Avatar answered Oct 12 '22 22:10

mipadi