Giter VIP home page Giter VIP logo

Comments (2)

bloodwithmilk25 avatar bloodwithmilk25 commented on July 17, 2024

@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()

from django-graphql-social-auth.

ramyak-mehra avatar ramyak-mehra commented on July 17, 2024

@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

from django-graphql-social-auth.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.