Search code examples
pythondjangographqlgraphene-django

How can I return the refresh token using python-social-auth and django-graphql-auth?


How can I return the refresh token when using social auth? I tried this solution here I don't know how to make this compatible with graphql.

Here is my attempt:

import graphql_social_auth
from graphql_jwt.shortcuts import get_token


class SocialAuth(graphql_social_auth.SocialAuthJWT):
    refresh_token = graphene.String()
    token = graphene.String()

    @classmethod
    def resolve(cls, root, info, social, **kwargs):
        return cls(
            user=social.user,
            token=get_token(social.user),
            refresh_token=social.extra_data['refresh_token']
        )

The user and token fields are alright when I comment out the refresh token. I just don't know where to get the refresh token, it seems to be not in the extra_data.

I am already dealing with this problem for hours and all the search results are no help. Please, any guidance is really appreciated.


Solution

  • You don't need to declare the token field again when extending SocialAuthJWT because it already has a token field from the JSONWebTokenMixin mixin unless you need to override it with a different type.

    As for the refresh token, you can do something like this to get it:

    import graphql_social_auth
    from graphql_jwt.shortcuts import get_token
    from graphql_jwt.shortcuts import create_refresh_token
    
    
    class SocialAuth(graphql_social_auth.SocialAuthJWT):
        refresh_token = graphene.String()
    
        @classmethod
        def resolve(cls, root, info, social, **kwargs):
            if social.user.refresh_tokens.count() >= 1:
                refresh_token = social.user.refresh_tokens.last()
            else:
                refresh_token = create_refresh_token(social.user)
    
            return cls(
                user=social.user,
                token=get_token(social.user),
                refresh_token=refresh_token
            )