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

Commit 0e3b5c6

Browse files
committed
Fixes #290 -- Pass request object on authenticate()
Django 1.11 introduced def authenticate(request, ...). This would be useful for some authentications with additional data. i.e. multiple user types.
1 parent 0a0bd40 commit 0e3b5c6

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

rest_framework_jwt/compat.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from django.contrib.auth import get_user_model
1+
import django
2+
from django.contrib.auth import authenticate as dj_authenticate, get_user_model
23

34
from rest_framework import serializers
45

@@ -35,3 +36,10 @@ def get_username(user):
3536
username = user.username
3637

3738
return username
39+
40+
41+
def authenticate(request=None, **credentials):
42+
if django.version < (1, 11):
43+
return dj_authenticate(**credentials)
44+
else:
45+
return dj_authenticate(request=request, **credentials)

rest_framework_jwt/serializers.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
from calendar import timegm
44
from datetime import datetime, timedelta
55

6-
from django.contrib.auth import authenticate, get_user_model
6+
from django.contrib.auth import get_user_model
77
from django.utils.translation import ugettext as _
88
from rest_framework import serializers
9-
from .compat import Serializer
9+
from .compat import Serializer, authenticate
1010

1111
from rest_framework_jwt.settings import api_settings
1212
from rest_framework_jwt.compat import get_username_field, PasswordField
@@ -47,7 +47,8 @@ def validate(self, attrs):
4747
}
4848

4949
if all(credentials.values()):
50-
user = authenticate(**credentials)
50+
request = self.context.get('request')
51+
user = authenticate(request=request, **credentials)
5152

5253
if user:
5354
if not user.is_active:

0 commit comments

Comments
 (0)