Skip to content

Instantly share code, notes, and snippets.

@hadrien
Last active April 26, 2025 16:54
Show Gist options
  • Save hadrien/30b9c1c21bcdce97ec8a7bc119b5d4fd to your computer and use it in GitHub Desktop.
Save hadrien/30b9c1c21bcdce97ec8a7bc119b5d4fd to your computer and use it in GitHub Desktop.
Logto.io - getCustomJwtClaims: Set up custom claims in the access token. These claims can be used to pass additional information to your application.
/**
* logto.io
* This function is called during the access token generation process to get custom claims for the JWT token.
* Limit custom claims to under 50KB. It requests custom claims to an api configured by 2 environment variables:
* apiKey and apiUrl
*
* @param {Object} payload - The input payload of the function.
* @param {AccessTokenPayload} payload.token -The JWT token.
* @param {Context} payload.context - Logto internal data that can be used to pass additional information
* @param {EnvironmentVariables} [payload.environmentVariables] - The environment variables.
*
* @returns The custom claims.
*/
const getCustomJwtClaims = async ({ token, context, environmentVariables }) => {
const { apiKey, apiUrl } = environmentVariables;
const body = JSON.stringify({token: token, context:context});
const jsonPayload = await fetch(
`${apiUrl}/v1/users`,
{
method:"POST",
body: body,
headers: {
"Content-Type": "application/json;charset=UTF-8",
"Authorization": apiKey
},
}
).then( response => {
if (!response.ok) {
throw new Error(`Invalid response`);
}
return response.json();
});
return jsonPayload.data;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment