Changing user roles
# support-questions
l
hi all, I'm attempting to define a user role based on whether the user is in a list of emails, and the list could change over time. I see that I can assign a role on sign up (https://supertokens.com/docs/userroles/managing-roles-and-users#assign-roles-to-a-user-and-their-session), but if a user is already signed in and their email is added to the list later on, I'd like to automatically assign the role and have it propagate to the frontend in a reasonable amount of time. Any suggestions on how to do that? I see that I can call
await session.fetchAndSetClaim(UserRoleClaim)
, but I'm not sure how to go from an email to a
SessionContainer
r
Hey @lunis you can just update the roles of the user in the db and the change should be reflected on all their sessions within 5 mins (which is the default)
If you want that time to be shorter, you can add a maxAgeInSeconds param to a lower number when using the roles validator in your APIs.
l
got it, thanks!
oh, does that mean that
UserRoles.addRoleToUser
in the Node sdk would work since it'd propagate to the db and from there to the sessions?
r
Yes.
l
@rp I'm adding the role in the post sign up override (https://supertokens.com/docs/userroles/managing-roles-and-users#which-api-to-override-for-adding-roles-post-sign-up), but in the access token,
st-role.v
is empty. If I sign back in a few minutes later, it contains the role. Is that just the 5 minute delay that you mentioned above? Is there any way to cause it to happen immediately on sign up?
I tried using getSession from inside consumeCodePOST, but that just caused me to get an error when clicking the passwordless sign in link
ah, I see that session is on the originalImplementation.consumeCodePOST response
r
yea.. you can use that session object to run
await session.fetchAndSetClaim(roles claim)