# MFA Failing with Action but not Rule

**URL:** <https://community.auth0.com/t/mfa-failing-with-action-but-not-rule/91152>\
**Category:** Get Help\
**Created:** [September 22, 2022, 7:26pm UTC](https://community.auth0.com/t/mfa-failing-with-action-but-not-rule/91152 "2022-09-22T19:26:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rogupta](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rogupta/32/12835_2.png) [@rogupta](https://community.auth0.com/u/rogupta)\
**Post date:** [September 22, 2022, 7:26pm UTC](https://community.auth0.com/t/mfa-failing-with-action-but-not-rule/91152/1 "2022-09-22T19:26:23Z")

</div>

Hello,

I want to add MFA to my application via Actions. Here is my existing code:

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

  /* add code to check user_metadata for mfa_enabled. if not there, then 
  look at organizations. when that happens, update user_metadata at the end
  */

  // initialize ManagementClient to access Auth0 Management API
  const { DOMAIN, CLIENT_ID, CLIENT_SECRET } = event.secrets;
  const ManagementClient = require('auth0').ManagementClient;
  const management = new ManagementClient({
      domain: DOMAIN,
      clientId: CLIENT_ID,
      clientSecret: CLIENT_SECRET,
  });

  const orgs = await management.users.getUserOrganizations({ id: event.user.user_id }).catch((err) => {
    console.log(err);
  })
  

  for (const org of orgs) {
    if (org.metadata && org.metadata.mfa_enabled === 'true') {
      api.multifactor.enable("any");
      break;
    }
  }
 
};

```

The MFA prompt is triggered, but the user is not logged in when being redirected back to the application page.  
A log event of type “Failed Silent Auth” with description “Multifactor authentication required” always appears.

**Update:**

Interestingly, we are able to get MFA working with Rules with the following code:

```auto
function requireMfaEnrollment(user, context, callback) {
  
  
  // in case of samlp , auth0 is just a service provider (not identity). So skip
  if(context.protocol === 'samlp') {
     console.log('SKIP MFA');
      context.multifactor = {
        provider: 'none'
      }; 
    callback(null, user, context);
    return ;
  }
			
  console.log(user);
  
  if(user.user_metadata && user.user_metadata.mfa_enabled && user.user_metadata.mfa_enabled === 'true' || (context.organization && context.organization.metadata.mfa_enabled && context.organization.metadata.mfa_enabled === 'true')) {
      console.log("mfa enabled for user");
      const completedMfa = !!context.authentication.methods.find(
      (method) => method.name === 'mfa'
    );
   
    if (completedMfa) {
      console.log("user completed mfa");
      return callback(null, user, context);
    }
   
    context.multifactor = {
      provider: 'any',
      allowRememberBrowser: false
    };
    callback(null, user, context);

  }

  callback(null, user, context);
}

```

However, we would still like to implement with Actions instead of Rules.

Even a simple action that triggers MFA and does nothing else (as shown below) fails:

```auto
exports.onExecutePostLogin = async (event, api) => {
  api.multifactor.enable("any");
  return;
}

```

I would appreciate help with this. Thank you in advance!

---

<div class="post-metadata">

**Author:** ![rogupta](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rogupta/32/12835_2.png) [@rogupta](https://community.auth0.com/u/rogupta)\
**Post date:** [September 29, 2022, 3:51pm UTC](https://community.auth0.com/t/mfa-failing-with-action-but-not-rule/91152/2 "2022-09-29T15:51:44Z")

</div>

> [@rogupta](#):
>
> ```auto
> const completedMfa = !!context.authentication.methods.find(
> (method) => method.name === 'mfa'
> );
> 
> ```

The key difference lies in the line above. In the Action, we were not checking this value, while in the Rule, we were.

---

<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:** [January 8, 2024, 8:49am UTC](https://community.auth0.com/t/mfa-failing-with-action-but-not-rule/91152/3 "2024-01-08T08:49:53Z")

</div>

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