# Migrating MFA rule to action

**URL:** <https://community.auth0.com/t/migrating-mfa-rule-to-action/115683>\
**Category:** Get Help\
**Tags:** mfa-api, mfa\
**Created:** [September 7, 2023, 7:26pm UTC](https://community.auth0.com/t/migrating-mfa-rule-to-action/115683 "2023-09-07T19:26:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dfleming](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dfleming/32/42432_2.png) [@dfleming](https://community.auth0.com/u/dfleming)\
**Post date:** [September 7, 2023, 7:26pm UTC](https://community.auth0.com/t/migrating-mfa-rule-to-action/115683/1 "2023-09-07T19:26:37Z")

</div>

Hi,

I have one rule for conditionally controlling MFA based on org metadata. I chose a rule for this because the ‘mfa hook’ example Auth0 provided used a rule. I know rules are retiring next fall. Is there an example I can use to enable / disable a user’s MFA based on their organization metadata using an action?

Drew

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [September 7, 2023, 8:46pm UTC](https://community.auth0.com/t/migrating-mfa-rule-to-action/115683/2 "2023-09-07T20:46:14Z")

</div>

Hey @dfleming !

Can you share the rule code you’re working with here?

---

<div class="post-metadata">

**Author:** ![dfleming](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dfleming/32/42432_2.png) [@dfleming](https://community.auth0.com/u/dfleming)\
**Post date:** [September 12, 2023, 1:23pm UTC](https://community.auth0.com/t/migrating-mfa-rule-to-action/115683/4 "2023-09-12T13:23:12Z")

</div>

```auto
function multifactorAuthentication(user, context, callback) {
  var queryParmMfa = context.request && context.request.query ? context.request.query.m : "";
  
  var mfaProvider = "";
  var appData = user.app_metadata;
  if(appData !== undefined && appData.mfaProvider) {
      mfaProvider = appData.mfaProvider;
  }  
  
  if(!mfaProvider && queryParmMfa) {
    mfaProvider = queryParmMfa;
  }

  context.multifactor = [];
  if (mfaProvider && (context.clientID === configuration.MFA_APP_1 || 
                      context.clientID === configuration.MFA_APP_2)) {
   context.multifactor = {
      provider: mfaProvider,
      allowRememberBrowser: true
    };
  }
	
  callback(null, user, context);
}

```

---

<div class="post-metadata">

**Author:** ![dfleming](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dfleming/32/42432_2.png) [@dfleming](https://community.auth0.com/u/dfleming)\
**Post date:** [October 2, 2023, 1:44pm UTC](https://community.auth0.com/t/migrating-mfa-rule-to-action/115683/5 "2023-10-02T13:44:14Z")

</div>

I added the code sample for you.

---

<div class="post-metadata">

**Author:** ![dfleming](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dfleming/32/42432_2.png) [@dfleming](https://community.auth0.com/u/dfleming)\
**Post date:** [October 24, 2023, 1:37pm UTC](https://community.auth0.com/t/migrating-mfa-rule-to-action/115683/6 "2023-10-24T13:37:54Z")

</div>

Any movement on this? I’d really like to take care of this asap

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [October 25, 2023, 12:07am UTC](https://community.auth0.com/t/migrating-mfa-rule-to-action/115683/7 "2023-10-25T00:07:41Z")

</div>

Hey @dfleming !

Have you made any attempt at converting the code to an Action? We don’t have a 1:1 example per se, but here is an attempt at a conversion:

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

  const queryParmMfa = event.request.query ? event.request.query.m : "";
  let mfaProvider = "";
  const appData = event.user.app_metadata;

  if(appData && appData.mfaProvider) {
    mfaProvider = appData.mfaProvider;
  }

  if(!mfaProvider && queryParmMfa) {
    mfaProvider = queryParmMfa;
  }

  if (mfaProvider && (event.client.client_id === configuration.MFA_APP_1 || 
                      event.client.client_id === configuration.MFA_APP_2)) {
    api.multifactor.enable({
      provider: mfaProvider,
      allowRememberBrowser: true
    });
  }
};

```

I have not been able to test this thoroughly, but it is a place to start if you want to test yourself 🙂

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [November 8, 2023, 12:08am UTC](https://community.auth0.com/t/migrating-mfa-rule-to-action/115683/8 "2023-11-08T00:08:43Z")

</div>

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