Custom Password Less Authentication in Django

Reading Time : ~ .

Authentication backends allow the ability to change what method checks your users credentials.

An Example for the use of custom authentication backend:

For web services, ie Facebook authentication, you don't have access to user data like a password. Without password(not like random string) we can't create a user in django. 

Facebook connect provides you details of the currently authenticated user. But to maintain the login_required decorator or to a user request.user you still need to have them logged in using Django. 
That's where the Authentication Backend comes in.

from django.contrib.auth.backends import ModelBackend
from peeldb.models import User


class PasswordlessAuthBackend(ModelBackend):
    """Log in to Django without providing a password.

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

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

In your settings.py add


AUTHENTICATION_BACKENDS += (
    # ... your other backends
    'social.auth_backend.PasswordlessAuthBackend',
    # path to your custom authentication file
    # appname.filename.classname
)

then to authenticate a registered user, we can override authenticate function without password in our views like

   user = authenticate(username={{user_email}})

   login(request, user)
    By Posted On
SENIOR DEVELOPER at MICROPYRAMID

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
E-commerce (paypal) integration with Django Ashwin Kumar

E-commerce is integration is becoming almost essential for every web application now a days. There are so many payment gateways to integrate with our application. ...

Continue Reading...
Django Forms basics explained Vamsi Popuri

Django forms is powerful module to help django application development in rendering html from model, validate input from http request to model specifications. And we ...

Continue Reading...
Retrieve average, minimum, maximum values from model field using Django Aggregation Ravi Kumar Gadila

To retrieve maximum, minimum or average values from group of rows we can use django Aggregation. For example to retrieve max price or avg price ...

Continue Reading...

Subscribe To our news letter

Subscribe to our news letter to receive latest blog posts into your inbox. Please fill your email address in the below form.
*We don't provide your email contact details to any third parties