# What is the correct way to require MFA for ONLY auth0 connections?

**URL:** <https://community.auth0.com/t/what-is-the-correct-way-to-require-mfa-for-only-auth0-connections/97892>\
**Category:** Get Help\
**Tags:** mfa-api, mfa\
**Created:** [January 11, 2023, 12:30am UTC](https://community.auth0.com/t/what-is-the-correct-way-to-require-mfa-for-only-auth0-connections/97892 "2023-01-11T00:30:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![brady.dean](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/brady.dean/32/12851_2.png) [@brady.dean](https://community.auth0.com/u/brady.dean)\
**Post date:** [January 11, 2023, 12:30am UTC](https://community.auth0.com/t/what-is-the-correct-way-to-require-mfa-for-only-auth0-connections/97892/1 "2023-01-11T00:30:25Z")

</div>

What I have tried so far is to disable MFA requirements globally in the authentication settings, then added this action to login.

```auto
exports.onExecutePostLogin = async (event, api) => {
  if (event.connection.strategy === "auth0") {
    api.multifactor.enable("any", {"allowRememberBrowser": true});
  }
};

```

This works to require MFA during sign-in, but it breaks the refresh token flow. I came across these

> [@How to bypass MFA for refresh token requests?](https://community.auth0.com/t/how-to-bypass-mfa-for-refresh-token-requests/26008):
>
> Hi, Since we have enabled MFA for some users, refresh token functionality fails with error message:" Multifactor authentication required". Is there any way to disable MFA requirement on refresh token and we ask for it only on Login process?

[https://auth0.com/docs/secure/tokens/refresh-tokens/use-refresh-tokens#bypass-mfa](https://auth0.com/docs/secure/tokens/refresh-tokens/use-refresh-tokens#bypass-mfa)

and tried to translate from hook to action

```auto
exports.onExecutePostLogin = async (event, api) => {
  if (event.transaction.protocol === "oauth2-refresh-token") {
    return;
  }
  else if (event.connection.strategy === "auth0") {
    api.multifactor.enable("any", {"allowRememberBrowser": true});
  }
};

```

But this does not work, in my auth0 logs I still see errors about silent auth failing because MFA is required. Users cannot access the site because auth0-react redirects back to login after failing silent auth.

---

<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:** [January 11, 2023, 1:28am UTC](https://community.auth0.com/t/what-is-the-correct-way-to-require-mfa-for-only-auth0-connections/97892/3 "2023-01-11T01:28:09Z")

</div>

Hey @brady.dean welcome to the community!

> [@brady.dean](#):
>
> But this does not work, in my auth0 logs I still see errors about silent auth failing because MFA is required. Users cannot access the site because auth0-react redirects back to login after failing silent auth.

It sounds like your application is not fully configured to use refresh tokens - If it is, you should see successful refresh token exchange events (`sertft`) in your logging. I’d need to test with a rule/action in place, but I’m curious if you run into this with successful successful refresh token exchanges occurring.

If you’d like to share the SDK you’re working with and any related config that could be helpful - Keep us posted!

---

<div class="post-metadata">

**Author:** ![brady.dean](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/brady.dean/32/12851_2.png) [@brady.dean](https://community.auth0.com/u/brady.dean)\
**Post date:** [January 11, 2023, 1:55am UTC](https://community.auth0.com/t/what-is-the-correct-way-to-require-mfa-for-only-auth0-connections/97892/4 "2023-01-11T01:55:38Z")

</div>

Refresh tokens seem to work for SSO, I see a lot of successful refresh token exchange events for these connections (99% of our dev testing uses SSO connections). I’m assuming the application is set up correctly (I’m just a backend dev, not too familiar with the react sdk).

I found [Configure Silent Authentication](https://auth0.com/docs/authenticate/login/configure-silent-authentication#silent-authentication-with-multi-factor-authentication) and adapted it into an action and this seems to have fixed things, at least in our development env. Will continue testing this tomorrow.

```auto
exports.onExecutePostLogin = async (event, api) => {
  if (event.authentication && Array.isArray(event.authentication.methods)) {
    const authMethods = event.authentication.methods;
    const completedMfa = !!authMethods.find((method) => method.name === "mfa");
    if (!completedMfa && event.connection.strategy === "auth0") {
      api.multifactor.enable("any", {"allowRememberBrowser": true});
    }
  }
};

```

---

<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:** [March 9, 2023, 12:41am UTC](https://community.auth0.com/t/what-is-the-correct-way-to-require-mfa-for-only-auth0-connections/97892/5 "2023-03-09T00:41:24Z")

</div>

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