jazzband / djangorestframework-simplejwt

A JSON Web Token authentication plugin for the Django REST Framework.
https://django-rest-framework-simplejwt.readthedocs.io/
MIT License
4k stars 662 forks source link

Return appropriate error for unactivated users #537

Open old-joker opened 2 years ago

old-joker commented 2 years ago

I am using Django 4.0 and Djoser 2.1.0 but when trying to get JWT token for an inactive user, it returns the same error as using a wrong password which makes it tricky to differentiate. I get HTTP STATUS 401 with the detail below

{ "detail": "No active account found with the given credentials }

how can i change it to check user activation status and not registered user or add some feature to set check activation status of users

Andrew-Chen-Wang commented 2 years ago

Does the is_active callable work? If not, you'll need to subclass the authentication class

old-joker commented 2 years ago

i'm new in django , can you help me how can i check this ?

bylly1 commented 2 years ago

I think you must override TokenObtainSerializer

Example:

#serializers.py
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer

class CustomTokenObtainPairSerializer(TokenObtainPairSerializer):
 default_error_messages = {
        'no_active_account': 'Username or Password does not matched.' # here you pass the message
    }

# views.py
from rest_framework_simplejwt.views import TokenObtainPairView
from . serializers import CustomTokenObtainSerializer

class CustomTokenObtainPairView(TokenObtainPairView):
 serializer_class = CustomTokenObtainPairSerializer

#urls.py
from . views import CustomTokenObtainPairView
urlpatterns = [
 ...
 path('token/', CustomTokenObtainPairView.as_view(), name='token_obtain_pair'),
 ...
]