#27527: How to enable login for a custom written authentication backend
-------------------------------------+-------------------------------------
Reporter: Kireeti | Owner: nobody
Type: Uncategorized | Status: closed
Component: contrib.auth | Version: 1.9
Severity: Normal | Resolution: invalid
Keywords: Authenticate, | Triage Stage:
Login, Backend | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):
* status: new => closed
* resolution: => invalid
* severity: Release blocker => Normal
Old 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.
New 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.
--
Comment:
Please see TicketClosingReasons/UseSupportChannels for ways to get help
with usage questions.
--
Ticket URL: <https://code.djangoproject.com/ticket/27527#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/065.aabf211154de07439872d36aebf0854e%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.