django-graphql-auth icon indicating copy to clipboard operation
django-graphql-auth copied to clipboard

Customize ObtainJSONWebTokenMixin

Open Cuda-Chen opened this issue 5 years ago • 2 comments

Prerequisites

  • [ ] Is it a bug?
  • [ ] Is it a new feature?
  • [x] Is it a a question?
  • [ ] Can you reproduce the problem?
  • [x] Are you running the latest version?
  • [x] Did you check for similar issues?
  • [x] Did you perform a cursory search?

For more information, see the CONTRIBUTING guide.

Description

I'm using django-graphql-auth for user registration, login, and reset password. And I would like to thank all contributors for creating such awesome package.

Current I create a CustomUser Model for my needs:

class CustomUser(AbstractUser):
    email = models.EmailField(blank=False, max_length=254, verbose_name="email address")
    full_name = models.CharField(max_length=255)
    affiliation = models.CharField(max_length=255)
    position = models.CharField(max_length=255)
    is_manual_verfied = models.BooleanField(default=False)

    USERNAME_FIELD = "username"   # e.g: "username", "email"
    EMAIL_FIELD = "email"         # e.g: "email", "primary_email"

So here's the question: is it possible to customize ObtainJSONWebTokenMixin so that I can check is_manual_verfied field when user is trying to login without modifying the source code?

Cuda-Chen avatar Dec 08 '20 08:12 Cuda-Chen

@Cuda-Chen hi, on one my project I used custom user model and me too needed check some other field. I used next code:

class LoginMutation(MutationMixin, ObtainJSONWebTokenMixin, graphql_jwt.JSONWebTokenMutation):    
   user = graphene.Field(AccountType) # custom output field
   unarchiving = graphene.Boolean(default_value=False)

   @classmethod
   def resolve(cls, root, info, **kwargs):
       unarchiving = kwargs.get("unarchiving", False)
       user = info.context.user 
       print(type(user)) # <class 'accounts.models.Account'>
       return super(LoginMutation, cls).resolve(root,info, **kwargs)

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

This method is worked, but im not sure if it is correct.

ealoshinsky avatar Dec 23 '20 07:12 ealoshinsky

Hi @ealoshinsky, I use multiple inheritance to solve the problem in ease:

class MyObtainJSONWebTokenMixin(Output):
    # check is_manual_verified field additionally

class MyObtainJSONWebToken(MutationMixin, MyObtainJSONWebTokenMixin, graphql_jwt.JSONWebTokenMutation):
    __doc__ = MyObtainJSONWebTokenMixin.__doc__
    user = graphene.Field(UserNode)
    unarchiving = graphene.Boolean(default_value=False)

class AuthMutation(graphene.ObjectType):
    ...
    token_auth = MyObtainJSONWebToken.Field()
    ...

Cuda-Chen avatar Dec 27 '20 05:12 Cuda-Chen