User post-authentication workflow
Workflows
Use workflows to add or remove claims from Kinde’s access or ID tokens.
Add the following code to your Workflow.ts file:
import { onUserTokenGeneratedEvent, WorkflowSettings, WorkflowTrigger, accessTokenCustomClaims,} from "@kinde/infrastructure";
// The setting for this workflowexport const workflowSettings: WorkflowSettings = { id: "addCustomClaimWorkflow", name: "Add Custom Claim Workflow", trigger: WorkflowTrigger.UserTokenGeneration, failurePolicy: { action: "stop", }, bindings: { "kinde.accessToken": {}, // required to modify access token claims url: {}, // required for URL parameters },};
// The workflow code to be executed when the event is triggeredexport default async function Workflow(event: onUserTokenGeneratedEvent) { const accessToken = accessTokenCustomClaims<{ companyName: string, }>()
accessToken.companyName = "Acme Inc."}The above code adds the companyName claim to the access token, with the value Acme Inc..
Add the following code to your Workflow.ts file:
import { onUserTokenGeneratedEvent, WorkflowSettings, WorkflowTrigger, accessTokenCustomClaims,} from "@kinde/infrastructure";
// The setting for this workflowexport const workflowSettings: WorkflowSettings = { id: "removeClaimWorkflow", name: "Remove Claim Workflow", trigger: WorkflowTrigger.UserTokenGeneration, failurePolicy: { action: "stop", }, bindings: { "kinde.accessToken": {}, // required to modify access token claims url: {}, // required for URL parameters },};
// The workflow code to be executed when the event is triggeredexport default async function Workflow(event: onUserTokenGeneratedEvent) { const accessToken = accessTokenCustomClaims<{ permissions: string[], feature_flags: Record<string, unknown>, }>()
accessToken.permissions = [] accessToken.feature_flags = {}}The above code removes the permissions and feature_flags claims from the access token, replacing them with an empty array and object respectively. This is useful for reducing the size of access tokens that are used in API calls.
Take a look at the available access token claims in the about access tokens doc. You may not be able to remove every default claim. See the prohibited claims section for more information.
Add the following code to your Workflow.ts file:
import { onUserTokenGeneratedEvent, WorkflowSettings, WorkflowTrigger, idTokenCustomClaims,} from "@kinde/infrastructure";
// The setting for this workflowexport const workflowSettings: WorkflowSettings = { id: "customIdTokenClaimsWorkflow", name: "Custom ID Token Claims Workflow", trigger: WorkflowTrigger.UserTokenGeneration, failurePolicy: { action: "stop", }, bindings: { "kinde.idToken": {}, // required to modify ID token claims url: {}, // required for URL parameters },};
// The workflow code to be executed when the event is triggeredexport default async function Workflow(event: onUserTokenGeneratedEvent) { const idToken = idTokenCustomClaims<{ house: string, name: string, }>()
idToken.house = "Gryffindor" idToken.name = "Harry Potter"}The above code adds a custom house claim to the ID token and updates the name claim as shown.
Take a look at the available ID token claims in the about ID tokens doc. You may not be able to modify every default claim. See the prohibited claims section for more information.
Declare the token binding that matches the JWT you are modifying:
kinde.accessToken: required to modify access token claimskinde.idToken: required to modify ID token claimskinde.m2mToken: required to modify M2M token claimsThe workflow examples on this page also declare the native url binding (for URL parameters).
You can’t modify the following token claims:
Common prohibited claims:
azpexpiatissnbfsidsubactaudAccess token prohibited claims:
jtiscpID token prohibited claims:
auth_timejtiupdated_atratM2M token prohibited claims:
gtyjtiscp