By continuing to navigate on this website, you accept the use of cookies to serve you more relevant services & content.
For more information and to change the setting of cookies on your computer, please read our Cookie Policy.

Custom Password Less Authentication in Django

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)
    Posted On
  • 31 July 2015
  • By
  • Micropyramid

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
Creating Django App

Django is a high-level, free and open-source Python Web framework that encourages rapid development. Django follows the model–view–controller (MVC) architectural pattern. Django's primary goal is ...

Continue Reading...
How to index binary files in django haystack

Now we are going to index text content which is stored in structured files such as PDFs, Microsoft Office documents, images, etc using haystack and ...

Continue Reading...
How to Create your own e-commerce shop using Django-Oscar.

Oscar is an open-source ecommerce framework for Django. Django Oscar provides a base platform to build an online shop. Oscar is built as a highly ...

Continue Reading...
open source packages

Subscribe To our news letter

Subscribe and Stay Updated about our Webinars, news and articles on Django, Python, Machine Learning, Amazon Web Services, DevOps, Salesforce, ReactJS, AngularJS, React Native.
* We don't provide your email contact details to any third parties