Convert Rule into Action that converts user profile metadata upon request

Hi @kazuyuki,

Welcome to the Auth0 Community!

I understand that you have some questions about converting your existing Rule to an Action.

First, allow me to address your questions:

  1. To achieve the same functionality as context.idToken['${namespace}/usertype'], you can use the following command in your Action script: api.idToken.setCustomClaim(${namespace}/usertype, VALUE).

For additional information and guidance, you can refer to this FAQ: Adding custom claims to tokens

  1. The good news is that the callback is no longer required. You can safely omit it.

  2. For your scenario, the appropriate trigger is the Post-Login Action flow. Just a friendly reminder, Rules execute post-login.

To make things even more convenient for you, I have rewritten your Rule as an Action. Here’s the updated code snippet:

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

  const namespace = 'https://my.metadata.com';
  if(event.user.app_metadata.usertype) {
    api.idToken.setCustomClaim(${namespace}/usertype, eventuser.app_metadata.usertype);
  }
  if(event.user.app_metadata.coops) {
    api.idToken.setCustomClaim(${namespace}/coops, event.user.app_metadata.coops);
  }  
  if(event.user.app_metadata.coopnames) {
    api.idToken.setCustomClaim(${namespace}/coopnames, event.user.app_metadata.coopnames);
  }   
}

Please don’t hesitate to reach out if you have any further questions.

Thanks,
Rueben

2 Likes