Hey, when I create a role in super tokens managed ...
# support-questions-legacy
k
Hey, when I create a role in super tokens managed service. It does not adds that role to the session when I call a url to get user data. I am using req.session!.getClaimValue(UserRoles.UserRoleClaim) to get the user role.
r
hey! have you added the roles after creating a session?
k
The setup is something like this: User signups => Buys a subscription using paypal => Paypal webhooks adds "PemiumUser" role to the user using his id => User refreshes the website and sees that they are a premium user.
The roles are added inside supertoken, but when the user refreshes the website nothing changes and the roles Array is empty, but when he signs out and sign in again, he sees that he is a premium user.
r
ah i see. So when you add the role to the user, you need to also update their session using
await req.session.fetchAndSetClaim(UserRoles.UserRoleClaim)
. If you don't do this, then the role change won't reflect in the session immediately. Instead, it will update after 5 mins on it's own.
k
Oh, before I was doing like this
const { UserRoleClaim, PermissionClaim, } = require("supertokens-node/recipe/userroles"); const { SessionContainer } = require("supertokens-node/recipe/session"); const Session = require("supertokens-node/recipe/session"); async function addRolesAndPermissionsToSession(session) { // we add the user's roles to the user's session await Session.fetchAndSetClaim(UserRoleClaim); // we add the permissions of a user to the user's session await Session.fetchAndSetClaim(PermissionClaim); } module.exports = addRolesAndPermissionsToSession;
Please add this clarity into your docs.
r
in our docs, we call the fetchAndSetClaim on the session container which is the
req.session
object.
but either way, we will further clarify it 🙂 Makes sense! thanks
k
Hmm, Thanks again for your quick and lovely support. 💓 from india
4 Views