execreate
04/22/2022, 8:31 AMregenerate_access_token
method of session recipe?rp
04/22/2022, 8:34 AMexecreate
04/22/2022, 8:39 AMuser_id
in the supertokens core, and each Profile has some associated ProfileRoles. I have overriden the create_new_session
function of the session recipe to include the role mappings for the current user in the access tokens. If I understand it correctly, the create_new_session
function runs only on login/signup. Taking into account that a user session has a 100 days timeout, they won't login that often but their roles might change during a single session.rp
04/22/2022, 8:43 AMexecreate
04/22/2022, 8:45 AMrp
04/22/2022, 8:45 AMexecreate
04/22/2022, 8:45 AMrp
04/22/2022, 8:45 AMexecreate
04/22/2022, 9:15 AMoriginal_implementation.update_access_token_payload
for the supertokens_python.recipe.session.interfaces.RecipeInterface
?from typing import Union, Dict, Any
from django.apps import apps
from supertokens_python.recipe.session.interfaces import RecipeInterface
from supertokens_python.recipe.session.asyncio import get_session_information
def override_functions(original_implementation: RecipeInterface):
original_implementation_update_access_token_payload = original_implementation.update_access_token_payload
async def update_access_token_payload(session_handle: str,
new_access_token_payload: Dict[str, Any],
user_context: Dict[str, Any]) -> None:
profile_role_model = apps.get_model('profile', 'ProfileRole')
user_info = await get_session_information(session_handle)
if new_access_token_payload is None:
new_access_token_payload = {}
new_access_token_payload["roles"] = {}
profile_roles = profile_role_model.objects.filter(profile__supertokens_user_id=user_info.user_id)
for profile_role in profile_roles:
if profile_role.at not in new_access_token_payload["roles"]:
new_access_token_payload["roles"][profile_role.at] = [profile_role.role]
else:
new_access_token_payload["roles"][profile_role.at].append(profile_role.role)
return await original_implementation_update_access_token_payload(session_handle,
new_access_token_payload, user_context)
original_implementation.update_access_token_payload = update_access_token_payload
return original_implementation
rp
04/22/2022, 9:17 AMexecreate
04/22/2022, 9:17 AMrp
04/22/2022, 9:17 AMexecreate
04/22/2022, 9:17 AM