I am trying to return JWT from my rest API through django-rest-auth but it gives a different type of token like for eg. 9054f7aa9305e012b3c2300408c3dfdf390fcddf
.
I have my own TokenObtainPairSerializer for JWT like this:
class MyTokenObtainPairSerializer(TokenObtainPairSerializer):
@classmethod
def get_token(cls, user):
email = user.email
token = super(MyTokenObtainPairSerializer, cls).get_token(user)
# Add custom claims
token['first_name'] = user.first_name
token['last_name'] = user.last_name
token['email'] = user.email
return token
I would like the django-rest-auth
to return my custom JWT through the login.
Any help will be greatly appreciated.
P.S: I searched through google and other stack-overflow questions, but couldn't find what I'm actually looking for.
MyTokenObtainPairSerializer
and you have JWT then it should work. Did you enable JWT ? to use JWT with django rest_auth, you'd have to set the following config in the settings.py
:
REST_USE_JWT = True
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
],
}
Also remember to install rest_framework_jwt
and add to your INSTALLED_APPS
UPDATE 2023: I use simple jwt these days so an updated version would be:
#---------- REST FRAMEWORK Settings-----------
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework_simplejwt.authentication.JWTAuthentication',
],
}