Need help with setting up a Post user registration action

  • Failed Post User Registration Hook

  • Description

ArgumentError on post-user-registration: Must provide a domain

I keep getting this error even though I have provided the domain of my application in my action

and I am using a modified version of the code given in this blog post:

Please let me know how I can fix this

Hi there @AdmiralBot!

Do you mind sharing your exact Action code here so we can take a look and see what might be the issue? Iā€™d like to try and reproduce it as closely as possible.

Thanks!

hey I am attaching my code here please let me know what I need to fix

exports.onExecutePostUserRegistration= async (event, api) => {
const namespace = ā€œhttps://myappname.us.auth0.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 defaultRole = { id :ā€˜rol_R0LEZGNx7GPlmKXeā€™};
var data = { ā€œusersā€ : [ event.user.user_id]};

try {
if (event.authorization){
api.idToken.setCustomClaim(${namespace}/roles, event.secrets.defaultRole);
api.accessToken.setCustomClaim(${namespace}/roles, event.secrets.defaultRole);
await management.roles.assignUsers(defaultRole, data);
}
} catch (e) {
console.log(e);
}

};

Thanks for sharing your code!

This is a bit trickier than I expected given a couple things:

  • Post registration actions do not have access to the event.authorization object and therefore this needs to be broken out into a Post-login action.

  • Youā€™ll need to introduce login into the Post-login action in order to account for the fact that you want to include the event.secrets.defaultRole if itā€™s the userā€™s first login and event.authorization.roles if itā€™s any subsequent login. The reason being the role assigned in the Post-registration action will not be available via event.authorization.roles on first login.

Hereā€™s a Post-registration and Post-login action that in unison are working for me:

Post User Registration:

exports.onExecutePostUserRegistration = async (event, api) => {

const namespace = "https://myappname.us.auth0.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};
var data = { "roles" : ['your_default_role_id']};

management.assignRolestoUser(params, data, function (err) {
  if (err) {
    // Handle error.
    console.log(err)
  }
  console.log(`User ${event.user.email} successfully assigned default role.`)
  // User assigned roles.
});
};

Post Login:

exports.onExecutePostLogin = async (event, api) => {
  
const namespace = "https://example.com"
  
//if this is the users first login, use the role hardcoded in event.secrets.defaultRole
 if (event.stats.logins_count == 1) {
    api.accessToken.setCustomClaim(`${namespace}/roles`, event.secrets.defaultRole ),
    api.idToken.setCustomClaim(`${namespace}/roles`, event.secrets.defaultRole )
  } else {
  //if this isn't the users first login, assign roles from event.authorization. 
  if (event.authorization) {
    console.log(event.stats.logins_count)
    api.accessToken.setCustomClaim(`${namespace}/roles`, event.authorization.roles ),
    api.idToken.setCustomClaim(`${namespace}/roles`, event.authorization.roles )
    }
  }
};

Hope this helps!

hey thanks for the response. So i will need to use 2 actions here, one in the post registration flow and one in the post login flow am I correct??

Also it is showing this error for the post login action:

1 Like

hey there @tyf is there any update??, the code you shared also shows the same error

1 Like

Hey @AdmiralBot - That error looks to be related to a Post User Registration Hook as opposed to a Post User Registration Action. Are you able to confirm whether or not thatā€™s the case?

Youā€™ll need to add defaultRole to the actions events.secrets in order to resolve these errors - This is required for an initial login by a user as the role is not yet available in event.authorization.roles.

How do i add default role to event.secrets??

1 Like

In this example, I am just using the string defaultRole role in event.secrets and adding it as a custom claim to tokens for the userā€™s first login only. Once a user logs in again, the defaultRole is added via event.authorization.roles - Using the defaultRole via event.secrets is acting as a sort of placeholder in order to have the role in tokens on first login.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.