Help with custom action is needed

We have two simple rules that run on all our tenants when a user logs in. However, since AUTH-PIPELINE-RULES is not available in the newly created tenants, I want to know how I can convert them into custom action and achieve the same function. I have tried reading the documentation, but I still can’t grasp it. I hope someone can provide me with some assistance.

Rule # 1

function (user, context, callback) {
const namespace = ‘https://myapp.com/’;
context.accessToken[namespace + ‘email’] = user.email;
callback(null, user, context);
}

Rule #2

The value configuration.intercom_key is defined under the rules as an encrypted key object currently on AUTH0 old tenant.

function (user, context, callback) {
const namespace = ‘https://myapp.com/’;
const crypto = require(‘crypto’);
const hmac = crypto.createHmac(‘sha256’, configuration.intercom_key);

hmac.update(user.email);

const hash = hmac.digest(‘hex’);

context.idToken[namespace + ‘intercom_hash’] = hash;
context.accessToken[namespace + ‘intercom_hash’] = hash;
callback(null, user, context);
}

Thanks in advance!

Hey there @raymond.lee !

You’ll want to test these thoroughly, but I’ve converted these to Actions you can go off of:

Action # 1

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

  const namespace = 'https://myapp.com/';
  api.accessToken.setCustomClaim(namespace + 'email', event.user.email);
};

Action # 2

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

    const namespace = 'https://myapp.com/';
    const crypto = require('crypto');
    const hmac = crypto.createHmac('sha256', event.secrets.intercom_key);

    hmac.update(event.user.email);
    const hash = hmac.digest('hex');

    api.idToken.setCustomClaim(namespace + 'intercom_hash', hash);
    api.accessToken.setCustomClaim(namespace + 'intercom_hash', hash);
};

A couple things to note with Action # 2 is that I have added intercom to event.secrets and had to suppress errors associated with passing event.user.email to hmac.update() - These were just in relation to event.user.email potentially being undefined.

2 Likes

Hi tyf, Thanks for your help and definitely will try it out.

1 Like

No problem @raymond.lee happy to help! Keep us posted :slight_smile:

Hi Tyf,

It works like a charm after I added those two new create actions inside the FLOWS - LOGIN. I missed that at the beginning.

Thank you so much for your help; it’s greatly appreciated!!!

1 Like

That’s great news - Happy to help! :slight_smile:

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