miguelstevensbe
04/28/2023, 8:17 PMrp_st
04/29/2023, 5:38 AMmiguelstevensbe
04/29/2023, 5:48 AMmiguelstevensbe
04/29/2023, 5:53 AMfor (const handle of sessionHandles) {
const sessionInformation = await Session.getSessionInformation(handle)
if (sessionInformation === undefined) {
throw Error(`No session information found for user ${ userId }`)
}
const sessionPayload = sessionInformation.accessTokenPayload;
await Session.updateAccessTokenPayload(handle,
{ role: updatedRole, ...sessionPayload }
);
}
Is this the same as?
await session.fetchAndSetClaim(UserRoleClaim)
rp_st
04/29/2023, 5:54 AMrp_st
04/29/2023, 5:56 AMmiguelstevensbe
04/29/2023, 6:08 AMrp_st
04/29/2023, 6:08 AMrp_st
04/29/2023, 6:09 AMmiguelstevensbe
04/29/2023, 6:12 AMrp_st
04/29/2023, 6:12 AMrp_st
04/29/2023, 6:12 AMmiguelstevensbe
04/29/2023, 7:25 AMmiguelstevensbe
04/29/2023, 8:01 AMawait session.fetchAndSetClaim(UserRoleClaim)
?miguelstevensbe
04/29/2023, 8:02 AMrp_st
04/29/2023, 9:22 AMrp_st
04/29/2023, 9:22 AMmiguelstevensbe
04/29/2023, 9:38 AMmiguelstevensbe
04/29/2023, 10:37 AMsession.fetchAndSetClaim(UserRoleClaim)
this assumes it's the current user. In my case I have an admin user changing the role for another user, how can I perform that?miguelstevensbe
04/29/2023, 11:11 AMsession.fetchAndSetClaim(UserRoleClaim)
for another user, not the one that is currently signed in? Thanks @rp_st!rp_st
04/29/2023, 1:25 PMaddRoleToUser
and removeUserRole
functions for the target user. And within 5 mins, the user's session would be updated.
You can make this 5 mins a lower number by passing the maxAgeInSeconds
param to the UserRoleClaim
validators. For example, you can use:
import { verifySession } from "supertokens-node/recipe/session/framework/express";
import express from "express";
import { SessionRequest } from "supertokens-node/framework/express";
import UserRoles from "supertokens-node/recipe/userroles";
let app = express();
app.post(
"/update-blog",
verifySession({
overrideGlobalClaimValidators: async (globalValidators) => [
...globalValidators,
UserRoles.UserRoleClaim.validators.includes("admin", 10),
],
}),
async (req: SessionRequest, res) => {
// All validator checks have passed and the user is an admin.
}
);
Notice the 10
in the includes
function call above - this will make sure that the roles is synced with the db every 10 seconds.