Question

My django application has two kind of users.

  1. Employer. Uses the default model based authentication backend.
  2. Candidate. Must use the token based authentication backend.

I have the backend in place for employers but how can I use the second token based backend for the candidate related part of the application?

Was it helpful?

Solution

django provides an AUTHENTICATION_BACKENDS setting which should do what you require.

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
)

Ours looks like this ....

AUTHENTICATION_BACKENDS = (
    'social_auth.backends.facebook.FacebookBackend',  # fb backend par social_auth
    'social_auth.backends.twitter.TwitterBackend',  # twitter backend par social_auth
    'backends.EmailAuthBackend',
    'backends.APIAuthBackend',
    'django.contrib.auth.backends.ModelBackend',
)

In out case, the backends.APIAuthBackend is where we check the supplied user auth token. You simply need to write your authentication code in the def authenticate method of that class which returns a user if the token exists.

The django docs provides more info.

https://docs.djangoproject.com/en/dev/topics/auth/customizing/#writing-an-authentication-backend

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top