Reputation: 142
Upon using a login system bases on authentication tokens stored in cookies, I am encountering issues while trying to refresh the token without using login and password again. Is it possible to refresh the jwt tokens using PyJWT ?
def get(self, request):
token = request.COOKIES.get('userJwt')
if token is None:
raise AuthenticationFailed('Unauthenticated!')
response = Response(status=200)
try:
payload = jwt.decode(token, 'secret', algorithms=['HS256'])
except jwt.ExpiredSignatureError:
# payload['exp'] = datetime.utcnow() + timedelta(seconds=20)
# new_token = jwt.encode(payload, "secret", algorithm="HS256")
# response.set_cookie(key='userJwt', value=new_token, httponly=True)
return Response({"message": "Expired"}, status=200)
user = User.objects.filter(id=payload['id']).first()
serializer = UserSerializer(user)
response.data = serializer.data
return response
Upvotes: 0
Views: 360