简体   繁体   中英

How to customize errors with django-graphql-auth?

I'm working on a project with Django and Graphene, with the library of django_graphql_auth to handle authentication, and i was asked to customize the error message that we receive when we fail a login. I've readed the docs over how to do this, and it only mentions a setting called CUSTOM_ERROR_TYPE( https://django-graphql-auth.readthedocs.io/en/latest/settings/#custom_error_type ), but i think i'm not understanding how to use it, or maybe it doesn't work the way i think it does. On my files i've:

custom_errors.py

import graphene

class CustomErrorType(graphene.Scalar):
    @staticmethod
    def serialize(errors):
        return {"my_custom_error_format"}

settings.py

from .custom_errors import CustomErrorType

GRAPHQL_AUTH = {
    'CUSTOM_ERROR_TYPE': CustomErrorType,
}

user.py

class AuthRelayMutation(graphene.ObjectType):
    password_set = PasswordSet.Field()
    password_change = PasswordChange.Field()

    # # django-graphql-jwt inheritances
    token_auth = ObtainJSONWebToken.Field()
    verify_token = relay.VerifyToken.Field()
    refresh_token = relay.RefreshToken.Field()
    revoke_token = relay.RevokeToken.Field()
    unlock_user = UsuarioUnlock.Field()

class Mutation(AuthRelayMutation, graphene.ObjectType):
    user_create = UserCreate.Field()
    user_update = UserUpdate.Field()
    user_delete = UserDelete.Field()

schema = graphene.Schema(query=Query, mutation=Mutation)

Yet, when i test the login, i still receive the message: "Please, enter valid credentials." What should i do to change that message?

Update

class ObtainJSONWebToken(
    RelayMutationMixin, ObtainJSONWebTokenMixin, graphql_jwt.relay.JSONWebTokenMutation
):
    __doc__ = ObtainJSONWebTokenMixin.__doc__
    user = graphene.Field(UserNode)
    days_remaining = graphene.Field(graphene.String, to=graphene.String())
    unarchiving = graphene.Boolean(default_value=False)

    @classmethod
    def resolve(cls, root, info, **kwargs):
        user = info.context.user

        # Little logic validations

        unarchiving = kwargs.get("unarchiving", False)
        return cls(user=info.context.user, days_remaining=days_remaining)
        

    @classmethod
    def Field(cls, *args, **kwargs):
        cls._meta.arguments["input"]._meta.fields.update(
            {"password": graphene.InputField(graphene.String, required=True)}
        )
        for field in app_settings.LOGIN_ALLOWED_FIELDS:
            cls._meta.arguments["input"]._meta.fields.update(
                {field: graphene.InputField(graphene.String)}
            )
        return super(graphql_jwt.relay.JSONWebTokenMutation, cls).Field(*args, **kwargs)

I had the same issue. I found and referred to this GitHub thread: https://github.com/flavors/django-graphql-jwt/issues/147 it's a little outdated but I tweaked it and it worked:

import graphql_jwt
from graphql_jwt.exceptions import JSONWebTokenError


class CustomObtainJSONWebToken(ObtainJSONWebToken):
    @classmethod
    def mutate(cls, *args, **kwargs):
        try:
            return super().mutate(*args, **kwargs)
        except JSONWebTokenError:
            raise Exception('Your custom error message here')

I ran into this issue as well, but realized due to this line in the django-graphql-auth library in graphql_auth/bases.py you have to indicate the class in your settings.py file as a string in order to use your custom error. This seemed to fix the issue for me.

So I have, for example:

GRAPHQL_AUTH = {
    'CUSTOM_ERROR_TYPE': 'accounts.custom_errors.CustomErrorType'
}

(where CustomErrorType is a class similar to what you indicated in your original post)

I have tweaked the above code with the use of ErrorType provided by graphene_django for error format.

import graphene
import graphql_jwt

from ..types import UserType
from graphene_django.types import ErrorType

from graphql_jwt.exceptions import JSONWebTokenError
from graphql_jwt.mixins import ResolveMixin

class CreateToken(graphql_jwt.JSONWebTokenMutation, ResolveMixin):
    user = graphene.Field(UserType)
    errors = graphene.List(ErrorType)

    @classmethod
    def mutate(cls, *args, **kwargs):
        try:
            return super().mutate(*args, **kwargs)
        except JSONWebTokenError as e:
            errors = ErrorType.from_errors({'username/password': [str(e)]})
            return cls(errors=errors)

    
    @classmethod
    def resolve(cls, root, info, **kwargs):
        return cls(user=info.context.user, errors=[])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM