flavors / django-graphql-jwt

JSON Web Token (JWT) authentication for Graphene Django

Home Page:https://django-graphql-jwt.domake.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

behconsci opened this issue · comments

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?

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,
)