简体   繁体   中英

Authenticate the django user using phone email and username

I am trying to achieve a login functionality like so that users can log into using their Email or Phone number or it's username as well but Django provides only email field in it's User model so that I've created an another model with One-To-One-Relation to User model and provided the phone number column but now I don't know how to achieve such functionality which I mentioned earlier.

my models.py

from django.db import models
from django.contrib.auth.models import User

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    user_type = models.IntegerField(choices=USER_TYPE, default=3)
    mobile = models.BigIntegerField(unique=True)

my views.py

(I know it's a mess but I can't figure out how to have such functionality)

def login(request):
    if request.method == "POST":
        username = request.POST['username']
        password = request.POST['password']

        user = auth.authenticate(username=username, password=password)
        if user is not None:
            auth.login(request, user)
            messages.success(request, 'You are logged in successfully!')
            return redirect('/dashboard')
        else:
            user = auth.authenticate(profile_phone=username, password=password)
            if user is not None:
                auth.login(request, user)
                messages.success(request, 'You are logged in successfully!')
                return redirect('/dashboard')
            else:
                user = auth.authenticate(email=username, password=password)
                if user is not None:
                    auth.login(request, user)
                    messages.success(request, 'You are logged in successfully!')
                    return redirect('/dashboard')
                else:
                    messages.error(request, 'Invalid username or password')
                    return redirect('/login')
    else:
        if request.user.is_authenticated:
            messages.success(request, 'You are already logged in')
            return redirect("/dashboard")
        return render(request, "accounts/login.html")

It is not true that the Django user model only provides an email field. It also has a username field as you want. Check documentation here . If you want to add a phone number or another field, you can implement like this:

from django.contrib.auth.models import User

class MyUser(User):
    user_type = models.IntegerField(choices=USER_TYPE, default=3)
    mobile = models.BigIntegerField(unique=True)

and the class will include everything that the base User model has.

Also, Django provides a built-in login method that you can also use. Check this tutorial .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM