Skip to content

Commit

Permalink
Add support for simpleJWT
Browse files Browse the repository at this point in the history
  • Loading branch information
ankurpandeyvns committed Sep 18, 2019
1 parent 624ad01 commit 794dac3
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 22 deletions.
3 changes: 2 additions & 1 deletion rest_auth/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ class JWTSerializer(serializers.Serializer):
"""
Serializer for JWT authentication.
"""
token = serializers.CharField()
access_token = serializers.CharField()
refresh_token = serializers.CharField()
user = serializers.SerializerMethodField()

def get_user(self, obj):
Expand Down
14 changes: 4 additions & 10 deletions rest_auth/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from six import string_types
from importlib import import_module
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
from rest_framework_simplejwt.views import TokenObtainPairView


def import_callable(path_or_callable):
Expand All @@ -17,13 +19,5 @@ def default_create_token(token_model, user, serializer):


def jwt_encode(user):
try:
from rest_framework_jwt.settings import api_settings
except ImportError:
raise ImportError("djangorestframework_jwt needs to be installed")

jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER

payload = jwt_payload_handler(user)
return jwt_encode_handler(payload)
refresh = TokenObtainPairSerializer.get_token(user)
return refresh.access_token, refresh
14 changes: 3 additions & 11 deletions rest_auth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def login(self):
self.user = self.serializer.validated_data['user']

if getattr(settings, 'REST_USE_JWT', False):
self.token = jwt_encode(self.user)
self.access_token, self.refresh_token = jwt_encode(self.user)
else:
self.token = create_token(self.token_model, self.user,
self.serializer)
Expand All @@ -76,7 +76,8 @@ def get_response(self):
if getattr(settings, 'REST_USE_JWT', False):
data = {
'user': self.user,
'token': self.token
'access_token': self.access_token,
'refresh_token': self.refresh_token,
}
serializer = serializer_class(instance=data,
context={'request': self.request})
Expand All @@ -85,15 +86,6 @@ def get_response(self):
context={'request': self.request})

response = Response(serializer.data, status=status.HTTP_200_OK)
if getattr(settings, 'REST_USE_JWT', False):
from rest_framework_jwt.settings import api_settings as jwt_settings
if jwt_settings.JWT_AUTH_COOKIE:
from datetime import datetime
expiration = (datetime.utcnow() + jwt_settings.JWT_EXPIRATION_DELTA)
response.set_cookie(jwt_settings.JWT_AUTH_COOKIE,
self.token,
expires=expiration,
httponly=True)
return response

def post(self, request, *args, **kwargs):
Expand Down

2 comments on commit 794dac3

@newbro
Copy link

@newbro newbro commented on 794dac3 Nov 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tried your fork for user registrationbut got following error:

KeyError: "Got KeyError when attempting to get a value for field access_token on serializer JWTSerializer.\nThe serializer field might be named incorrectly and not match any attribute or key on the dict instance.\nOriginal exception text was: 'access_token'."

I notice the default token model is rest_framework.authtoken.models.Token, do we need to define our custom model in setting REST_AUTH_TOKEN_MODEL to work with rest_framework_simplejwt?

@ankurpandeyvns
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tried your fork for user registrationbut got following error:

KeyError: "Got KeyError when attempting to get a value for field access_token on serializer JWTSerializer.\nThe serializer field might be named incorrectly and not match any attribute or key on the dict instance.\nOriginal exception text was: 'access_token'."

I notice the default token model is rest_framework.authtoken.models.Token, do we need to define our custom model in setting REST_AUTH_TOKEN_MODEL to work with rest_framework_simplejwt?

Is the normal authentication with SimpleJWT working correctly?

Please sign in to comment.