2

I initially used to login using email and password but now want to change it in such a way that we can login using either mobile no or email address and password.I am not able to understand what parts to change in my code as I do not want to delete already existing user and data.

forms.py

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


class UserForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput)

    class Meta:
        model = User
        fields = ['first_name', 'last_name', 'email', 'password']

views.py

class UserFormView(View):

    form_class = UserForm
    template_name = 'main/registration_form.html'

    def get(self, request):
        form = self.form_class(None)
        return render(request, self.template_name, {'form': form})

    def post(self, request):
        form = self.form_class(request.POST)

        if form.is_valid():

            user = form.save(commit=False)

            # cleaned (normalized) data
            email = form.cleaned_data['email']
            password = form.cleaned_data['password']
            user.username = email
            user.set_password(password)
            user.save()

            # return user objects if credentials are correct
            user = authenticate(username=email, password=password)

            if user is not None:

                if user.is_active:
                    login(request, user)
                    # request.user.username       display their username
                    return redirect('main:register2')
        return render(request, self.template_name, {'form': form})

class LoginForm(View):

    form_class = UserForm
    template_name = 'main/login_form.html'

    def get(self, request):
        form = self.form_class(None)
        if error:
            return render(request, self.template_name, {'form': form}, 
{'error': error})
        else:
            return render(request, self.template_name, {'form': form})

    def post(self, request):
        email = request.POST.get('email', '')
        password = request.POST.get('password', '')
        user = auth.authenticate(username=email, password=password)

        if user is not None and user.is_active:
            # Correct password, and the user is marked "active"
            auth.login(request, user)
            # Redirect to a success page.
            return HttpResponseRedirect("/main/home/")
        else:
            # Show an error page
            error = True
            return HttpResponseRedirect("/main/", {'error': error})

Please answer considering the fact that I am new to Django. Thanks in advance :)

Nikhil Khandelwal
  • 198
  • 1
  • 2
  • 12

1 Answers1

0

In the post method of your view, identify what has the user given you. If it's an email, feed the Django's authenticate() method with the email. If not, then assume it's a phone number (you may also run additional validations in there) and use this in the authentication procedure.

Another approach would be to create a custom authenticate() method, which would look like this:

from django.contrib.auth.backends import ModelBackend


class MobilePhoneOrEmailModelBackend(ModelBackend):

    def authenticate(self, username=None, password=None):
        # the username could be either one of the two
        if '@' in username:
            kwargs = {'email': username}
        else:
            kwargs = {'mobile_phone': username}
        try:
            user = User.objects.get(**kwargs)
            if user.check_password(password):
                return user
        except User.DoesNotExist:
            return None

    def get_user(self, username):
        try:
            return User.objects.get(pk=username)
        except User.DoesNotExist:
            return None

Let me know if this worked out well for you.

Credits to user user3282276 in this thread as well: Log in user using either email address or username in Django

Kostas Livieratos
  • 965
  • 14
  • 33