Opened 8 years ago
Last modified 8 years ago
#27527 closed Uncategorized
How to enable login for a custom written authentication backend — at Initial Version
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
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.