# MFA Actions Triggered Twice

**URL:** <https://community.auth0.com/t/mfa-actions-triggered-twice/150865>\
**Category:** Get Help\
**Tags:** mfa-prompt-new-experience, mfa\
**Created:** [November 1, 2024, 8:38pm UTC](https://community.auth0.com/t/mfa-actions-triggered-twice/150865 "2024-11-01T20:38:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bradyp](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/bradyp/32/61918_2.png) [@bradyp](https://community.auth0.com/u/bradyp)\
**Post date:** [November 1, 2024, 8:38pm UTC](https://community.auth0.com/t/mfa-actions-triggered-twice/150865/1 "2024-11-01T20:38:01Z")

</div>

I’m working on setting up conditional MFA based on a few factors. To start with I was just trying to set based on the last time they had an MFA session.

The issue I am seeing is that when I hit api.authentication.challengeWith or .enrollWith the actions start over again. If I don’t have a break condition it will just loop forever. I have 3 other actions in Post-Login and they all restart. Am I missing something in my MFA api.authentication.challengeWith()?

Code

```auto
const mfaSeconds = 60;

exports.onExecutePostLogin = async (event, api) => {
    console.log('MFA Filter - onExecutePostLogin');
    console.log(event.authentication.methods);

    if (event?.user?.email?.startsWith("mytestuser")) {
        if (event != null && event.user != null && event.user.app_metadata != null && event.user.app_metadata["last_mfa"] != null) {
            var timestamp = Math.floor(Date.now() / 1000);
            console.log("timestamp:", timestamp);
            var mfaTimestamp = Number(event.user.app_metadata["last_mfa"]);
            console.log("mfaTimestamp:", mfaTimestamp)
            console.log("mfaTimestamp + mfaSeconds: ", mfaTimestamp + mfaSeconds);
            if ((mfaTimestamp + mfaSeconds) > timestamp) {
                console.log("Skipping MFA check, only " + (mfaTimestamp + mfaSeconds - timestamp) + " seconds since last MFA");
                return event;
            }
        }
    }

    if (event?.user?.email?.startsWith("mytestuser")) {
        console.log('MFA enrollment');
        var enrolledFactors = event.user.enrolledFactors;
        if (enrolledFactors && enrolledFactors.length) {
            console.log("Already enrolled, challenging...");
            let challengeFactors = enrolledFactors.map(m => ({type: m.type}));
            for (let i = 0; i < enrolledFactors.length; i++) {
                console.log("enrolledFactors:" + enrolledFactors[i]);
                console.log("challengeFactors:" + challengeFactors[i]);
            }
            api.authentication.challengeWith({type: 'otp'});
            UpdateLastMFA(event, api);
        } else {
            console.log("No enrollment, enrolling...");
            api.authentication.enrollWith({type: 'otp'}, {type: 'phone'});
            UpdateLastMFA(event, api);
        }
    }
    return event;
};

function UpdateLastMFA(event, api) {
    if (event.user.email.startsWith("mytestuser")) {
        console.log('UpdateLastMFA');
        const FORM_ID = 'ap_heLu3AM1Rd8GDCg6MNdtqG';
        api.prompt.render(FORM_ID);
    }
}

```

Logs

```auto
3:27:15 PM: sandbox runtime ready
3:27:16 PM: Check Reset Password - onExecutePostLogin
3:27:16 PM: finished webtask request
3:27:16 PM: sandbox runtime ready
3:27:16 PM: finished webtask request
3:27:16 PM: sandbox runtime ready
3:27:16 PM: MFA Filter - onExecutePostLogin
3:27:16 PM: [{ name: 'federated', timestamp: '2024-11-01T20:27:15.305Z' }]
3:27:16 PM: timestamp: 1730492836
mfaTimestamp: 1730492443
mfaTimestamp + mfaSeconds: 1730492503
3:27:16 PM: MFA enrollment
Already enrolled, challenging...
enrolledFactors:[object Object]
3:27:16 PM: challengeFactors:[object Object]
3:27:16 PM: UpdateLastMFA
3:27:16 PM: finished webtask request
3:27:18 PM: sandbox runtime ready
3:27:18 PM: finished webtask request
3:27:19 PM: Check Reset Password - onExecutePostLogin
3:27:19 PM: finished webtask request
3:27:19 PM: sandbox runtime ready
3:27:20 PM: finished webtask request
3:27:20 PM: MFA Filter - onExecutePostLogin
[{ name: 'federated', timestamp: '2024-11-01T20:27:15.305Z' }]
3:27:20 PM: timestamp: 1730492840
3:27:20 PM: mfaTimestamp: 1730492837
mfaTimestamp + mfaSeconds: 1730492897
Skipping MFA check, only 57 seconds since last MFA
3:27:20 PM: finished webtask request
3:27:20 PM: sandbox runtime ready
3:27:20 PM: Add User Name - onExecutePostLogin
3:27:20 PM: finished webtask request

```

---

<div class="post-metadata">

**Author:** ![bradyp](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/bradyp/32/61918_2.png) [@bradyp](https://community.auth0.com/u/bradyp)\
**Post date:** [November 12, 2024, 8:16pm UTC](https://community.auth0.com/t/mfa-actions-triggered-twice/150865/3 "2024-11-12T20:16:43Z")

</div>

I worked with support on this issue so I’ll update here. The issue was an exception in the node.js action code. There seems to be a bug (or maybe it’s intentional) where if you hit an exception in the action it just re-runs all the actions again. This can get you stuck in a never ending loop if you have no break condition.

---

<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:** [November 12, 2024, 9:52pm UTC](https://community.auth0.com/t/mfa-actions-triggered-twice/150865/4 "2024-11-12T21:52:02Z")

</div>

Hi @bradyp,

Thanks for sharing your solution here with the rest of the community!

---

<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 26, 2024, 9:53pm UTC](https://community.auth0.com/t/mfa-actions-triggered-twice/150865/5 "2024-11-26T21:53:00Z")

</div>

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