Skip to content

Instantly share code, notes, and snippets.

@mkouhei
Last active August 29, 2015 14:28
Show Gist options
  • Select an option

  • Save mkouhei/0dd5dfa29df5c5ed6d69 to your computer and use it in GitHub Desktop.

Select an option

Save mkouhei/0dd5dfa29df5c5ed6d69 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
from django.contrib.auth import authenticate, login
from django.http import HttpResponseRedirect
from django.conf import settings
class CustomAuthMiddleware(object):
def process_request(self, request):
_token = None
if request.META.get('HTTP_X_AUTH_TOKEN'):
_token = request.META.get('HTTP_X_AUTH_TOKEN')
if request.method == 'POST' and request.POST.get('auth_token'):
_token = request.POST['auth_token']
if _token:
user = authenticate(auth_token=_token)
if user is not None:
request.user = user
login(request, user)
if not request.META.get('HTTP_X_AUTH_TOKEN'):
return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment