Skip to main content
Once you’ve set up your app’s logic for guiding a user to complete MFA, you need to configure Privy to invoke this logic whenever MFA is required by the user’s embedded wallet.

Registering the listener

To set up this configuration, use Privy’s useRegisterMfaListener hook. As a parameter, you must pass a JSON object with an onMfaRequired callback.

onMfaRequired callback

Privy will invoke the onMfaRequired callback you set whenever the user is required to complete MFA to use the embedded wallet. When this occurs, any use of the embedded wallet will be “paused” until the user has successfully completed MFA with Privy.In this callback, you should invoke your app’s logic for guiding through completing MFA (done via the useMfa hook). Within this callback, you can also access an methods parameter that contains a list of available MFA methods that the user has enrolled in ('sms' and/or 'totp' and/or 'passkey').
MFAProvider.tsx
import {useRegisterMfaListener, MfaMethod} from '@privy-io/react-auth';

import {MFAModal} from '../components/MFAModal';

export const MFAProvider = ({children}: {children: React.ReactNode}) => {
  const [isMfaModalOpen, setIsMfaModelOpen] = useState(false);
  const [mfaMethods, setMfaMethods] = useState<MfaMethod[]>([]);

  useRegisterMfaListener({
    // Privy will invoke this whenever the user is required to complete MFA
    onMfaRequired: (methods) => {
      // Update app's state with the list of available MFA methods for the user
      setMfaMethods(methods);
      // Open MFA modal to allow user to complete MFA
      setIsMfaModalOpen(true);
    },
  });

  return (
    <div>
      {/* This `MFAModal` component includes all logic for completing the MFA flow with Privy's `useMfa` hook */}
      <MFAModal isOpen={isMfaModalOpen} setIsOpen={setIsMfaModalOpen} mfaMethods={mfaMethods} />
      {children}
    </div>
  );
};
In order for Privy to invoke your app’s MFA flow, the component that calls Privy’s useRegisterMfaListener hook must be mounted whenever the user’s embedded wallet requires that they complete MFA.We recommend that you render this component near the root of your application, so that it is always rendered whenever the embedded wallet may be used.

Example MFA modal

Catching MFA errors inline

The React and React Native SDKs do not support inline MFA errors, and require the listener to be set up for MFA flows.