[Fixed]-Django auth custom business logic – how do I raise custom error message?

1👍

If you dig into django-rest-framework-jwt you can find that “Unable to login with …” is set in JSONWebTokenSerializer (https://github.com/GetBlimp/django-rest-framework-jwt/blob/f07772769c0f72f971daefe1d9426a90ea099a63/rest_framework_jwt/serializers.py#L66). Since this message is hard-coded you can’t simply inject your own message.

To override it you will need to:

  1. Create your own serializer based on JSONWebTokenSerializer and
    override it’s validate method recording to your needs (changing
    error message for example)
  2. Create custom api view based on default ObtainJSONWebToken view (https://github.com/GetBlimp/django-rest-framework-jwt/blob/f07772769c0f72f971daefe1d9426a90ea099a63/rest_framework_jwt/views.py#L69) and change serializer_class attribute to your custom webtoken serializer.
  3. Use your custom api view in urls.py instead of the default one.
👤Nick

Leave a comment