# MFA migrating from rules to action causes OTP Auth failed

**URL:** <https://community.auth0.com/t/mfa-migrating-from-rules-to-action-causes-otp-auth-failed/137368>\
**Category:** Get Help\
**Tags:** one-time-password-otp-factor, mfa\
**Created:** [June 25, 2024, 1:59pm UTC](https://community.auth0.com/t/mfa-migrating-from-rules-to-action-causes-otp-auth-failed/137368 "2024-06-25T13:59:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![japrescott](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/japrescott/32/48801_2.png) [@japrescott](https://community.auth0.com/u/japrescott)\
**Post date:** [June 25, 2024, 1:59pm UTC](https://community.auth0.com/t/mfa-migrating-from-rules-to-action-causes-otp-auth-failed/137368/1 "2024-06-25T13:59:56Z")

</div>

Hello, we’ve migrated from rules to actions where we had conditional MFA enabled for certain users.  
This however is causing OTP Auth Failed errors for users which have had MFA enabled before the migration. Manually deleting the MFA and having the users setup MFA again solves the issue.  
How can I make this migration/transition uninterrupting for our users?

This is our action code used

```auto
const domainNeedsMFA = (userEmail) => {
	const domainsWithMFA = [
		'@example.com',
	];
	
	// check if the specific user needs MFA
	return domainsWithMFA.some((domain)=>{
		if (userEmail.endsWith(domain)){
			return true;
		}
		return false;
	});
}

/**
* Handler that will be called during the execution of a PostLogin flow.
*
* @param {Event} event - Details about the user and the context in which they are logging in.
* @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login.
*/
exports.onExecutePostLogin = async (event, api) => {
	/** 
 	 * The following code block will skip this Action if Rule "MFA enforcement" 
 	 * was previously executed in the transaction in order to avoid duplication 
 	 * of logic.
 	 */ 
 	if (api.rules.wasExecuted("rul_XXXXXXXXX")) { 
 		return;
 	} 

	// check if the specific user needs MFA
	const mfaRequired = domainNeedsMFA(event.user.email) 
	if (!mfaRequired) {
		return;
	}

	// bypass mfa for refresh token requests
	// see https://auth0.com/docs/secure/multi-factor-authentication/customize-mfa#bypass-mfa-for-refresh-token-requests
	if (event.transaction?.protocol === 'oauth2-refresh-token') {
		return;
	}

	// force the user to setup MFA
	const enrolledFactors = event.user.multifactor || [];
	const mfaProvider = event.user.multifactor[0] || 'any'
	if (enrolledFactors.length === 0) {
		// The user has not enrolled in any MFA factor yet, trigger an MFA enrollment
		api.multifactor.enable('any', { allowRememberBrowser: false });
	}
	else {
		// check if session already did a MFA
		// if the array of authentication methods is valid and contains a method named 'mfa', mfa has been done in this session already
		if (
			!event.authentication ||
			!Array.isArray(event.authentication.methods) ||
			!event.authentication.methods.find((method) => method.name === 'mfa')
		) {
			api.multifactor.enable(
				mfaProvider,
				{
					// optional, defaults to true. Set to false to force authentication every time.
					// See https://auth0.com/docs/secure/multi-factor-authentication/customize-mfa#change-frequency-of-mfa-prompts for details
					allowRememberBrowser: false
				}
			);
		}
	}
};

```

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [June 26, 2024, 4:07pm UTC](https://community.auth0.com/t/mfa-migrating-from-rules-to-action-causes-otp-auth-failed/137368/3 "2024-06-26T16:07:05Z")

</div>

Hi @japrescott,

Welcome to the Auth0 Community!

I have reviewed your code and can confirm that the logic is correct. I have tested your code on my end, and it worked as expected.

In this case, could you please check your tenant logs to see what might be causing the OTP Auth to fail?

And then, could you share your findings with me?

Cheers,  
Rueben

---

<div class="post-metadata">

**Author:** ![japrescott](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/japrescott/32/48801_2.png) [@japrescott](https://community.auth0.com/u/japrescott)\
**Post date:** [June 27, 2024, 1:05pm UTC](https://community.auth0.com/t/mfa-migrating-from-rules-to-action-causes-otp-auth-failed/137368/4 "2024-06-27T13:05:05Z")

</div>

Hey @rueben.tiow

Thanks for reviewing the code and confirming that its not an obvious error.

Both test-users facing issues had setup multifactor on `2021-08-12T07:45:42.915Z` and use `guardian` type. Have there been any changes to MFA/2FA in recent years at auth0?

Or is there any option/field within auth0 which would “reset” the MFA/2FA for all users somehow?

Following a redacted tenant log of a failed auth which just says OTP is invalid.

```auto
{
  "date": "2024-06-25T12:28:47.420Z",
  "type": "gd_auth_failed",
  "description": "Guardian - Second factor verification failed: invalid otp",
  "ip": "212.25.XX.XX",
  "user_agent": "Safari 17.5.0 / Mac OS X 10.15.7",
  "user_id": "google-oauth2|XXXXXXXXXXXXXXXXXXXXXXXX",
  "details": {
    "request": {
      "method": "POST",
      "path": "/api/verify-otp",
      "query": {},
      "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Safari/605.1.15",
      "body": {
        "type": "manual_input",
        "code": " *****"
      },
      "ip": "212.25.XX.XX",
      "auth": {
        "subject": "google-oauth2|XXXXXXXXXXXXXXXXXXXXXXXX",
        "strategy": "jwt_transaction_token",
        "scopes": ""
      }
    },
    "response": {}
  },
  "log_id": "00000000000000000000000000000000000000000",
  "tenant_name": "our-tenant-name",
  "_id": "00000000000000000000000000000000000000000",
  "isMobile": false,
  "id": "00000000000000000000000000000000000000000"
}

```

Any pointers/hints are greatly appreciated.

Best

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [June 28, 2024, 11:18pm UTC](https://community.auth0.com/t/mfa-migrating-from-rules-to-action-causes-otp-auth-failed/137368/5 "2024-06-28T23:18:02Z")

</div>

Hi @japrescott,

Thanks for the reply.

AFAIK, there have been no changes to MFA/2FA in recent years. I don’t believe there is any option that enforces an MFA/2FA reset for all users, so this shouldn’t be possible. However, you can delete (reset) a user’s MFA enrollments through the Management API’s [delete an authenticator](https://auth0.com/docs/api/authentication#delete-an-authenticator) endpoint.

Judging from the error log you shared, it looks to be an issue with the OTP being invalid.

There might be issues with the Guardian app being out of sync. Usually, in this case, you would need to re-enroll in MFA to resolve this issue.

Now, you mentioned previously that this issue originated after migrating from Rules to Actions.

Could you double-check if your test users can log in as usual if you disable your Action script? I am checking to see if back-tracking your changes addresses this issue.

Additionally, you can monitor your Rules/Action script success/failure using the [Real-time Webtask Logs Extension](https://auth0.com/docs/customize/extensions/real-time-webtask-logs) with `console.log()` statements.

Thanks,  
Rueben

---

<div class="post-metadata">

**Author:** ![japrescott](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/japrescott/32/48801_2.png) [@japrescott](https://community.auth0.com/u/japrescott)\
**Post date:** [July 2, 2024, 2:43pm UTC](https://community.auth0.com/t/mfa-migrating-from-rules-to-action-causes-otp-auth-failed/137368/6 "2024-07-02T14:43:27Z")

</div>

Hi @rueben.tiow

Thank you for your hints and validating that the code works as intended.

I now double checked the code with real customers and it indeed works as you had confirmed. Something must be wrong with our test users and I will thus reset their MFA.

Thanks for your support.

Best,  
JAP

---

<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:** [July 16, 2024, 2:44pm UTC](https://community.auth0.com/t/mfa-migrating-from-rules-to-action-causes-otp-auth-failed/137368/7 "2024-07-16T14:44:29Z")

</div>

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