I built an npm package that can run old Auth0 Rules as a Post-Login Actions, and wanted to share it here.
How it works
- Derives old user and context objects from the event object
- Runs the rule, which mutates the context object
- Makes api calls based on changes to the context object
Example
Add as dependency to a Post Login action
auth0-rule-as-action@latest
Example Post Login action
const RuleToAction = require("auth0-rule-as-action");
/**
* The Rule
*/
function exampleRule(user, context, callback) {
context.idToken["https://example.com/test"] = "testValue";
if (context.clientName === "All Applications") {
const date = new Date();
const d = date.getDay();
if (d === 0 || d === 6) {
return callback(
new UnauthorizedError("This app is available during the week"),
);
}
}
callback(null, user, context);
}
/**
* Handler that will be called during the execution of a PostLogin flow.
*
* @param {Event} event - Details about the user and the context in which they are logging in.
* @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login.
*/
exports.onExecutePostLogin = async (event, api) => {
const rule = exampleRule;
// Instantiate a Rule to Action converter
const converter = new RuleToAction(api);
// Run the Rule as an Action
await converter.convert(event, rule);
};
The library runs this rule, and under the hood calls
api.idToken.setCustomClaim("https://example.com/test", "testValue");
And if it’s the weekend,
api.access.deny("This app is available during the week")