Skip to content
This repository has been archived by the owner on May 26, 2020. It is now read-only.

Commit

Permalink
Fixes #290 -- Pass request object on authenticate()
Browse files Browse the repository at this point in the history
Django 1.11 introduced `def authenticate(request, ...)`.
This would be useful for some authentications with additional data.
i.e. multiple user types.
  • Loading branch information
Jezeniel Zapanta committed Nov 7, 2017
1 parent 0a0bd40 commit 68a23cc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
10 changes: 9 additions & 1 deletion rest_framework_jwt/compat.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.contrib.auth import get_user_model
import django
from django.contrib.auth import authenticate as dj_authenticate, get_user_model

from rest_framework import serializers

Expand Down Expand Up @@ -35,3 +36,10 @@ def get_username(user):
username = user.username

return username


def authenticate(request=None, **credentials):
if django.version < (1, 11):
return dj_authenticate(**credentials)
else:
return dj_authenticate(request=request, **credentials)
7 changes: 4 additions & 3 deletions rest_framework_jwt/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
from calendar import timegm
from datetime import datetime, timedelta

from django.contrib.auth import authenticate, get_user_model
from django.contrib.auth import get_user_model
from django.utils.translation import ugettext as _
from rest_framework import serializers
from .compat import Serializer
from .compat import Serializer, authenticate

from rest_framework_jwt.settings import api_settings
from rest_framework_jwt.compat import get_username_field, PasswordField
Expand Down Expand Up @@ -47,7 +47,8 @@ def validate(self, attrs):
}

if all(credentials.values()):
user = authenticate(**credentials)
request = self.context.get('request')
user = authenticate(request=request, **credentials)

if user:
if not user.is_active:
Expand Down

0 comments on commit 68a23cc

Please sign in to comment.