Use case
- Store or pass member information: Pass temporary session state or member metadata to your application.
- Integrate with 3rd-party services: Integrate with providers that support JWT-based authentication.
- Implement custom authorization logic: Use custom claim templates to pass RBAC information to sessions.
Add custom claims to a session
Include thesession_custom_claims argument on any authenticate method to add custom claims to a session. This argument takes in an arbitrary JSON object—which may be represented as a map, dictionary, or object:
Remove claims
Supply anull value to delete a key:
Using nested claim objects
Using nested claim objects
If the value of a custom claim is a JSON object, the nested values can be updated or deleted using the same methods as above:
Custom claim templates
Use custom claim templates to add the same set of claims to every session.- The JSON object output for a particular member will be used as the initial set of custom claims for all of that member’s sessions.
- Claims from templates can still be added, updated, or deleted by passing in a
session_custom_claimsargument in an API request. - Updates to the custom claim template or member metadata propagate to existing sessions the next time a JWT is minted.
- Previously minted JWTs are immutable and cannot be updated.
Markup syntax
Custom claim template markup language uses{{ variable }} syntax to denote information that should be passed in at runtime.
Member or Organization metadata
Use dot notation to access nested fields when handling Member or Organization metadata. For example,{{ member.trusted_metadata.subscription.level }} accesses the level field of the subscription object in the trusted_metadata on a Member object.
Member RBAC values
The variable{{ member.rbac.roles }} is evaluated as an array of all role IDs assigned to the member, e.g. ["stytch_member", "editor", "support_admin"].
The variable {{ member.rbac.$RESOURCE_ID.actions }} is evaluated as an array of all actions the member can perform on the specific resources. For example, {{ member.rbac.documents.actions }} might be evaluated as ["create", "read", "delete"].
List of supported template variables
{{ member.external_id }}{{ member.member_id }}{{ member.name }}{{ member.email_address }}{{ member.rbac.roles }}{{ member.rbac.$RESOURCE_ID.actions }}{{ member.trusted_metadata.$PATH }}{{ organization.external_id }}{{ organization.organization_id }}{{ organization.organization_name }}{{ organization.trusted_metadata.$PATH }}
Example
Example
The template:could be combined with the member’s information:to produce the final set of custom claims:
Restrictions
Both API-driven custom claims and custom claim templates share these restrictions:- These claims are reserved and result in an error if set:
isssubaudexpnbfiatjtihttps://stytch.com/* - Total claims size cannot exceed 4kb.
- A JWT cannot be modified once it is minted. You must authenticate again to update claims.