Hello, I’m pretty new to auth0 and what i’m trying to achieve is add specific roles that i have already created through the auth0 dashboard and assign them to certain users based on their email domain name
I used this thread as a reference and followed it step by step: How can I use the Management API in Actions?
I actually tried to pull it off in the post-registration flow as well as in the post-login flow and neither worked for me
Here’s what my action looks like
exports.onExecutePostUserRegistration= async (event, api) => {
if (event.stats.logins_count !== 1) {
return;
}
const namespace = "https://my-app.example.com";
const ManagementClient = require("auth0").ManagementClient;
const management = new ManagementClient({
domain: event.secrets.domain,
clientId: event.secrets.clientId,
clientSecret: event.secrets.clientSecret,
scope: "read:roles create:roles update:roles",
});
const params = { id: event.user.user_id };
const defaultRole = { "roles": [event.secrets.defaultRole] };
const adminRole = { "roles": [event.secrets.adminRole] };
try {
if (event.authorization) {
if (!event.user.email_verified) {
return;
} else if (
event.user.email &&
event.user.email.endsWith("@admin.com")
) {
api.idToken.setCustomClaim(
`${namespace}/roles`,
await management.users.assignRoles(params, adminRole)
);
api.accessToken.setCustomClaim(
`${namespace}/roles`,
await management.users.assignRoles(params, adminRole)
);
} else {
api.idToken.setCustomClaim(
`${namespace}/roles`,
await management.users.assignRoles(params, defaultRole)
);
api.accessToken.setCustomClaim(
`${namespace}/roles`,
await management.users.assignRoles(params, defaultRole)
);
}
}
} catch (e) {
console.log(e);
}
};
Could someone be kind enough to help me point out what i’m doing wrong please?
Thanks in advance