Opened 7 years ago

Closed 7 years ago

#27527 closed Uncategorized (invalid)

How to enable login for a custom written authentication backend

Reported by: Kireeti Owned by: nobody
Component: contrib.auth Version: 1.9
Severity: Normal Keywords: Authenticate, Login, Backend
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tim Graham)

Hello Django Team, Thanks for creating such powerful framework and making it open. For one of my projects i wanted to let users login with OTP, for which i am doing the verification manually. I want to use the login() to let the user login to his account, order products. For which i created my own backend to query the model with the phone number all this part until authenticate is working fine. but i am not able to figure out how to use login from here. below is a snippet of what i created.

Backend:

from .models import Customer

class CustomerModelBackend(object):
    def authenticate(self, phone_number=None, password=None):
        try:
            user = Customer.objects.get(phone=phone_number)
            if user is not None:
                return user
            else:
                return False
        except user.DoesNotExist:
            return False

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

Views.py

customer_backend = CustomerModelBackend()
customer_exist = customer_backend.authenticate(phone_number=phone)
login(request, customer_exist)

Here customer_exist is getting customer object as expected but login function is throwing global name not defined. importing login from contrib/auth is throwing backend not defined.

Someone please advice what to do.

Thanks.

Change History (1)

comment:1 by Tim Graham, 7 years ago

Description: modified (diff)
Resolution: invalid
Severity: Release blockerNormal
Status: newclosed

Please see TicketClosingReasons/UseSupportChannels for ways to get help with usage questions.

Note: See TracTickets for help on using tickets.
Back to Top