# Conditional MFA Not Working

**URL:** <https://community.auth0.com/t/conditional-mfa-not-working/50267>\
**Category:** Get Help\
**Created:** [September 23, 2020, 12:59pm UTC](https://community.auth0.com/t/conditional-mfa-not-working/50267 "2020-09-23T12:59:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![paul.smith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/paul.smith/32/12809_2.png) [@paul.smith](https://community.auth0.com/u/paul.smith)\
**Post date:** [September 23, 2020, 12:59pm UTC](https://community.auth0.com/t/conditional-mfa-not-working/50267/1 "2020-09-23T12:59:33Z")

</div>

I am trying to implement some conditional MFA by following this article [Customize Multi-Factor Authentication Pages - Auth0 Docs](https://auth0.com/docs/mfa/customize-mfa-user-pages?_ga=2.236822529.1008315000.1600780779-173653597.1582123480#change-the-frequency-of-authentication-requests)

The idea is that users will only be asked to perform MFA once per session rather than every time the JWT expires. I have a JWT expiry of 15 minutes. My client is an SPA.

I have a rule which checks if MFA has been performed for the current session and if not requests it by setting the context.multifactor property. My rule looks like this:

```
  function multifactorAuthentication(user, context, callback) {
 
  const completedMfa = !!context.authentication.methods.find(
    (method) => method.name === 'mfa'
  );
  
  console.log('multifactorAuthentication - MFA completed was:', completedMfa);
 
  if (!completedMfa) {
    console.log('Request MFA');
    context.multifactor = {
      provider: 'any',
      allowRememberBrowser: false
    };
  }
 
  callback(null, user, context);
}

```

By looking at the real time logs, I can see that the first time the user logs in, the completedMfa flag is set to false and context.multifactor object is initialised.

When the JWT has expired, the SPA code calls checkSession() which results in my rule being executed again. This time the completedMfa flag is true and the conditional block setting up MFA is skipped. However, checkSession is returning an error saying login required. It appears that my script is not having any affect on whether MFA is required or not. What am I missing here?

---

<div class="post-metadata">

**Author:** ![jmangelo](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jmangelo/32/7923_2.png) [@jmangelo](https://community.auth0.com/u/jmangelo)\
**Post date:** [September 23, 2020, 3:46pm UTC](https://community.auth0.com/t/conditional-mfa-not-working/50267/2 "2020-09-23T15:46:54Z")

</div>

From a configuration perspective MFA can be triggered from rules or through the _Require Multi-factor Auth_ setting available in the MFA dashboard section. It’s a long shot, but have you already confirmed that this other settings is **not** configured to **always** require MFA?

---

<div class="post-metadata">

**Author:** ![paul.smith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/paul.smith/32/12809_2.png) [@paul.smith](https://community.auth0.com/u/paul.smith)\
**Post date:** [September 23, 2020, 4:06pm UTC](https://community.auth0.com/t/conditional-mfa-not-working/50267/3 "2020-09-23T16:06:35Z")

</div>

Well that was a simple fix. I’ve set the MFA to never in the dashboard and now the conditional stuff works. Thanks very much!!

---

<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:** [October 8, 2020, 4:06pm UTC](https://community.auth0.com/t/conditional-mfa-not-working/50267/4 "2020-10-08T16:06:41Z")

</div>

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