Hook API
hook is the second argument passed to every exports.execute function. It provides methods to write output from your script: custom identity fields, token claims, metadata, errors, and shared state.
exports.execute = async (lrObject, hook) => {
// use hook.* methods to write output
};
Method Reference
1. hook.identity.setCustomField(key, value)
Sets a custom field on the user's identity profile.
keymust be a string: type error otherwise.valuecan be any serialisable value.
hook.identity.setCustomField("LastLoginDate", new Date().toISOString());
2. hook.identity.setMetaData(key, value)
Sets a metadata flag on the user's identity. Only the following keys are supported:
| Key | Type |
|---|---|
EmailVerified | boolean |
IsLoginLocked | boolean |
DisableLogin | boolean |
IsActive | boolean |
hook.identity.setMetaData("IsLoginLocked", true);
3. hook.accessToken.setCustomClaim(claim, value)
Injects a custom claim into the user's access token.
- Both
claimandvaluemust be strings: type error otherwise.
hook.accessToken.setCustomClaim("role", "admin");
hook.accessToken.setCustomClaim("plan", "enterprise");
4. hook.idToken.setCustomClaim(claim, value)
Injects a custom claim into the user's ID token.
- Both
claimandvaluemust be strings: type error otherwise.
hook.idToken.setCustomClaim("sca_verified", "true");
hook.idToken.setCustomClaim("risk_score", "12");
5. hook.sharedState.set(key, value)
Writes a key-value pair into the workflow's SharedState, making it available to downstream nodes.
- Both
keyandvaluemust be strings. - In SCA flows, values written here are available in Send Push Notification message templates as
{{sharedstate.key}}.
hook.sharedState.set("verified_amount", "89.99");
hook.sharedState.set("verified_currency", "USD");
hook.sharedState.set("verified_payee", "ShopCase");
hook.sharedState.set("sca_verified", "true");
6. hook.setError(code, message, description)
Sets a user-facing error that short-circuits the workflow. The workflow follows the matching error-code output edge on the Script node.
code: integer error code.message: short error string.description: longer explanation.
hook.setError(145, "You can't log in with Gmail", "Please use your work email to log in.");
Type Errors
Each method validates its argument types at runtime. Passing a wrong type produces a descriptive error:
| Method | Error |
|---|---|
setCustomClaim | Type error in setCustomClaim |
setCustomField | Type error in setCustomField |
setMetaData | Type error in setMetaData |
setError | Type error in setError |
- lrObject Reference: read request, identity, and session data
- Examples: full working script examples