flavors / django-graphql-jwt

JSON Web Token (JWT) authentication for Graphene Django
https://django-graphql-jwt.domake.io
MIT License
820 stars 171 forks source link

How can I create/set JWT Token in Django view and then redirect to Frontend? #302

Closed behconsci closed 2 years ago

behconsci commented 2 years ago

I am setting up SSO Login which works with Okta and my frontend is NextJs. After SSO Login, I want to redirect to frontend App with JWT Cookie already set. I can not call setup_jwt_cookie decorator because it is meant for GraphQL View.

I could not find anything in source code, is there some hack for this usecase?

behconsci commented 2 years ago

Okay, got it working:

from graphql_jwt.utils import jwt_payload, jwt_encode, set_cookie

payload = jwt_payload(user=request.user)
jwt_token = jwt_encode(payload=payload)
expires = datetime.utcnow() + settings.GRAPHQL_JWT.get('JWT_EXPIRATION_DELTA')

# set JWT cookie for GraphQL
set_cookie(
    response,
    settings.GRAPHQL_JWT.get('JWT_COOKIE_NAME'),
    jwt_token,
    expires=expires,
)