Jake
10/23/2022, 11:04 AMcreateNewSession
, what is the best way to refresh this information? I use input.userId
to lookup the info to put into the access token. Also wondering if it's the right spot, I need to access the info on the client and the serverrp
10/23/2022, 3:21 PMJake
10/23/2022, 11:49 PMoverride: {
functions: (originalImplementation) => {
return {
...originalImplementation,
createNewSession: async (input) => {
console.log('creating session', input)
const queryExecutor = this.options.getQueryExecutor(
input.userId,
)
const claims = await getClaimsForUser(
queryExecutor,
input.userId,
)
const user = await getUserById(input.userId)
const accessTokenPayload = {
email: user?.email,
organizations: claims.existingOrganizationClaims,
}
// This goes in the access token, and is availble to read on the frontend
// This is stored in the db against the sessionHandle for this session
return originalImplementation.createNewSession({
...input,
accessTokenPayload,
})
},
}
},
},
rp
10/24/2022, 4:02 AMJake
10/24/2022, 6:55 AMrp
10/24/2022, 8:38 AMporcellus
10/24/2022, 10:36 AMPrimitiveClaim
or PrimitiveArrayClaim
. I think you'd need PrimitiveArrayClaim
(assuming existingOrganizationClaims
is an array of strings).
This way, you can add validators to verifySession
to check the value before it gets to the API implementation (it'll return a 403 if the validator doesn't pass). These validators will refresh the value in the access token if necessary.
The PrimitiveArrayClaim
constructor takes a few options:
`key`: an identifier specifying where we should store the value in the access token.
`fetchValue`: an async function that returns the value you want to store (e.g.: claims.existingOrganizationClaims
)
`defaultMaxAgeInSeconds`: you can specify a claim-level default for the validators that check the last time the claim was fetched.
You can add them during session creation like this:
Session.init({
override: {
functions: (oI) => ({
...oI,
createNewSession: async (input) => {
input.accessTokenPayload = {
...input.accessTokenPayload,
...(await CustomClaim.build(input.userId, input.userContext)),
};
return oI.createNewSession(input);
},
}),
},
}),
Other than that, you could use it exactly like the UserRoleClaim
from the UserRoles recipe. For example check here to see how to check the value in your APIs: https://supertokens.com/docs/emailpassword/user-roles/protecting-routesJake
10/25/2022, 3:04 AMporcellus
10/25/2022, 12:01 PMsession.fetchAndSet(CustomClaim)
that'll handle mergeIntoAccessTokenPayloadmaxAgeInSeconds
parameters) that'll refresh it from the DBJake
10/25/2022, 12:07 PMporcellus
10/25/2022, 12:08 PMfetchAndSet
Jake
10/25/2022, 12:12 PMporcellus
10/25/2022, 12:13 PMJake
10/25/2022, 12:13 PMporcellus
10/25/2022, 12:13 PMJake
10/25/2022, 12:13 PMupdateAccessTokenPayload
be enough?porcellus
10/25/2022, 12:26 PMfetchAndSetClaim
on the session object.