Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Extend jwt payload with global user id

Jakub Draganek88ab99080748
diff --git a/saleor/graphql/utils.py b/saleor/graphql/utils.py
index bee34bbfad..43b0bd4d7f 100644
--- a/saleor/graphql/utils.py
+++ b/saleor/graphql/utils.py
@@ -167,6 +167,7 @@ def format_permissions_for_display(permissions):
def create_jwt_payload(user, context=None):
payload = jwt_payload(user, context)
+ payload["user_id"] = graphene.Node.to_global_id("User", user.id)
payload["is_staff"] = user.is_staff
payload["is_superuser"] = user.is_superuser
return payload
diff --git a/tests/api/test_account.py b/tests/api/test_account.py
index 789d88b7e3..1bca9662ed 100644
--- a/tests/api/test_account.py
+++ b/tests/api/test_account.py
@@ -4,6 +4,7 @@ import uuid
from unittest.mock import ANY, MagicMock, Mock, patch
import graphene
+import jwt
import pytest
from django.contrib.auth.tokens import default_token_generator
from django.core.exceptions import ValidationError
@@ -76,7 +77,7 @@ def query_staff_users_with_filter():
return query
-def test_create_token_mutation(admin_client, staff_user):
+def test_create_token_mutation(admin_client, staff_user, settings):
query = """
mutation TokenCreate($email: String!, $password: String!) {
tokenCreate(email: $email, password: $password) {
@@ -96,7 +97,10 @@ def test_create_token_mutation(admin_client, staff_user):
)
content = get_graphql_content(response)
token_data = content["data"]["tokenCreate"]
- assert token_data["token"]
+ token = jwt.decode(token_data["token"], settings.SECRET_KEY)
+ assert token["email"] == staff_user.email
+ assert token["user_id"] == graphene.Node.to_global_id("User", staff_user.id)
+
assert token_data["errors"] == []
incorrect_variables = {"email": staff_user.email, "password": "incorrect"}