How to set custom JWT in django using dj-rest-auth

  Kiến thức lập trình

I have setup a backend JWT authentication system using django, dj-rest-auth and simpleJWT. Currently, the dj-rest-auth is setting default JWT in the response cookies. To use custom cookies, I have to use simpleJWT’s /token and /token/refresh urls. So, while testing in postman or swagger, I’m not getting the custom JWTs neither in the response body nor in the cookies(except the default ones provided by dj-rest-auth).

Is there a way around so that I can receive those custom JWTs as cookies or response body? I am also using social authentication so those tokens have to be present in the response JWTs as well.

My urls.py file

urlpatterns = [
    path('', include(router.urls)),
    path('token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
    path('token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
    path('users/', include('dj_rest_auth.urls')),
    path('users/registration/', CustomRegisterView.as_view(), name='custom_rest_register'),
]

serializer.py

class EnrichedTokenPairSerializer(TokenObtainPairSerializer):
    @classmethod
    def get_token(cls, user):
        token = super().get_token(user)
        // custom token logic
        return token`
``

I'm using 
Django>=5.0.2
django-allauth>=0.61.1
djangorestframework>=3.14.0
djangorestframework-simplejwt>=5.2.2
dj-rest-auth>=5.0.2

Any suggestions would be helpful.

I have done the authentication using dj-rest-auth and simpleJWT. I have a custom token serializer function to create custom tokens which return from /token url of simpleJWT. But while loggin in through Postman, these custom tokens were not present. I want them to be sent from the server either in cookies or the response body.

New contributor

Smruti Ranjan Sahu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT