# Refresh token request failed because of mfa\_required

**URL:** <https://community.auth0.com/t/refresh-token-request-failed-because-of-mfa-required/121596>\
**Category:** Get Help\
**Tags:** new-universal-login-experience, login-experience\
**Created:** [November 24, 2023, 3:34pm UTC](https://community.auth0.com/t/refresh-token-request-failed-because-of-mfa-required/121596 "2023-11-24T15:34:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rezgui.aziz](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rezgui.aziz/32/51642_2.png) [@rezgui.aziz](https://community.auth0.com/u/rezgui.aziz)\
**Post date:** [November 24, 2023, 3:34pm UTC](https://community.auth0.com/t/refresh-token-request-failed-because-of-mfa-required/121596/1 "2023-11-24T15:34:35Z")

</div>

Hello, I am using Next js and auth0.  
I am build a mfa system, so the users will be able to choose what MFA they want to use in the profile settings within the app I am building.  
This is the setup I’ve made to allow them to do so:

```auto
  const { accessToken } = await getAccessToken();
  const enrollSMS2FA = async (phoneNumber: string) => {
    "use server";
    const response = await fetch(
      `${process.env.AUTH0_ISSUER_BASE_URL}/mfa/associate`,
      {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          Authorization: `Bearer ${accessToken}`,
        },
        body: JSON.stringify({
          authenticator_types: ["oob"],
          oob_channels: ["sms"],
          phone_number: phoneNumber,
        }),
      }
    );
    const result = await response.json();
    return {
      status: response.status,
      oobCode: result.oob_code,
    };
  };

```

```auto
  const confirmSMS2FA = async (userCode: number, oobCode: string) => {
    "use server";
    const mfaToken: string = accessToken ?? "";

    const response = await fetch(
      `${process.env.AUTH0_ISSUER_BASE_URL}/oauth/token`,
      {
        method: "POST",
        headers: {
          Authorization: `Bearer ${mfaToken}`,
          "Content-Type": "application/x-www-form-urlencoded",
        },
        body: new URLSearchParams({
          grant_type: "http://auth0.com/oauth/grant-type/mfa-oob",
          client_id: process.env.AUTH0_CLIENT_ID || "",
          client_secret: process.env.AUTH0_CLIENT_SECRET || "",
          mfa_token: mfaToken,
          oob_code: oobCode,
          binding_code: userCode.toString(),
        }).toString(),
      }
    );

    const result = await response.json();
    const status = response.status;
    return { status: status, result: result };
  };

```

All works fine, but for some reason the access token gets expired after 5 minutues and the refresh token request fails because of the mfa\_required.  
I am not forcing the users to use MFA on login , I disabled it in the dashboard.  
Also I wrote this action to make users that have active MFA use it on the login.

```auto
exports.onExecutePostLogin = async (event, api) => {
  if (event.user.enrolledFactors.find(m => m.type === 'phone')) {
    api.authentication.challengeWithAny([{ type: 'phone' }]);
  }
}

```

Please let me know how to handle the refresh token with the MFA.

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [November 29, 2023, 8:11pm UTC](https://community.auth0.com/t/refresh-token-request-failed-because-of-mfa-required/121596/3 "2023-11-29T20:11:33Z")

</div>

Hi @rezgui.aziz,

It looks like your Action is challenging for MFA on every authentication (including refresh token use) with an existing phone factor. You will need to add a conditional here.

You can differentiate between the different types of requests (refresh token, normal login, etc.) in an Action via the `event.transaction.protocol` object.

Here’s a doc that has more info:

> **[Actions Triggers: post-login - Event Object](https://auth0.com/docs/customize/actions/flows-and-triggers/login-flow/event-object#:~:text=protocol%20Optional%20string,Possible%20values%20include%3A)**
>
> Learn about the post-login Action trigger's event object, which provides contextual information about a single user logging in via Auth0.

---

<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:** [December 13, 2023, 8:12pm UTC](https://community.auth0.com/t/refresh-token-request-failed-because-of-mfa-required/121596/4 "2023-12-13T20:12:01Z")

</div>

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