flavors / django-graphql-social-auth

Python Social Auth support for Graphene Django
https://pypi.python.org/pypi/django-graphql-social-auth
MIT License
109 stars 33 forks source link

Option to set cookie #22

Closed ramyak-mehra closed 3 years ago

ramyak-mehra commented 4 years ago

Please add the set_cookie_auth to social mutation also for setting cookie and refresh_token

bloodwithmilk25 commented 3 years ago

@ramyak-mehra I had to overwrite SocialAuthJWT to achieve desired behaviour. I replaced JSONWebTokenMixin with my custom one.

class JSONWebTokenMixin:
    token = graphene.String()
    refresh_token = graphene.String()

    @classmethod
    def resolve(cls, root, info, social, **kwargs):
        try:
            from graphql_jwt.shortcuts import get_token, create_refresh_token
        except ImportError:
            raise ImportError(
                'django-graphql-jwt not installed.\n'
                'Use `pip install \'django-graphql-social-auth[jwt]\'`.')

        token = get_token(social.user)
        refresh_token = create_refresh_token(social.user)
        # set jwt_token to request, it will be picked up by jwt_cookie decorator
        # and cookie will be set
        info.context.jwt_token = token
        info.context.jwt_refresh_token = refresh_token
        return cls(token=token, refresh_token=refresh_token)

class SocialAuthJWT(JSONWebTokenMixin, graphql_social_auth.SocialAuthMutation):
    """Social Auth for JSON Web Token (JWT)"""

Note that you also should use jwt_cookie from graphql_jwt.decorators. It will pick up jwt_token and jwt_refresh_token request attributes and set a cookie.

from graphql_jwt.decorators import jwt_cookie

urlpatterns = [
    ...
    path('graphql/', jwt_cookie(GraphQLView.as_view(graphiql=settings.DEBUG))),
    ...
]

Finaly, just use your custom SocialAuthJWT like shown in the docs:

class Mutations(graphene.ObjectType):
    social_auth = SocialAuthJWT.Field()
ramyak-mehra commented 3 years ago

@ramyak-mehra I had to overwrite SocialAuthJWT to achieve desired behaviour. I replaced JSONWebTokenMixin with my custom one.

class JSONWebTokenMixin:
    token = graphene.String()
    refresh_token = graphene.String()

    @classmethod
    def resolve(cls, root, info, social, **kwargs):
        try:
            from graphql_jwt.shortcuts import get_token, create_refresh_token
        except ImportError:
            raise ImportError(
                'django-graphql-jwt not installed.\n'
                'Use `pip install \'django-graphql-social-auth[jwt]\'`.')

        token = get_token(social.user)
        refresh_token = create_refresh_token(social.user)
        # set jwt_token to request, it will be picked up by jwt_cookie decorator
        # and cookie will be set
        info.context.jwt_token = token
        info.context.jwt_refresh_token = refresh_token
        return cls(token=token, refresh_token=refresh_token)

class SocialAuthJWT(JSONWebTokenMixin, graphql_social_auth.SocialAuthMutation):
    """Social Auth for JSON Web Token (JWT)"""

Note that you also should use jwt_cookie from graphql_jwt.decorators. It will pick up jwt_token and jwt_refresh_token request attributes and set a cookie.

from graphql_jwt.decorators import jwt_cookie

urlpatterns = [
    ...
    path('graphql/', jwt_cookie(GraphQLView.as_view(graphiql=settings.DEBUG))),
    ...
]

Finaly, just use your custom SocialAuthJWT like shown in the docs:

class Mutations(graphene.ObjectType):
    social_auth = SocialAuthJWT.Field()

Thanks for the reply I already fixed it I guess I should close the issue