import {
useAuthenticatePasskeyMFA,
useGetMfaToken,
useGetPasskeys,
useIsMfaRequiredForAction,
useRegisterPasskey,
} from "@dynamic-labs/sdk-react-core";
import { MFAAction } from '@dynamic-labs/sdk-api-core';
export function EnsurePasskeyMfaToken() {
const authenticatePasskeyMFA = useAuthenticatePasskeyMFA();
const { getMfaToken } = useGetMfaToken();
const getPasskeys = useGetPasskeys();
const registerPasskey = useRegisterPasskey();
const isMfaRequiredForAction = useIsMfaRequiredForAction();
const promptForMfaIfRequired = async () => {
const requires = await isMfaRequiredForAction({
mfaAction: MFAAction.WalletWaasSign,
});
if (!requires) {
// No MFA required for this action
return;
}
// Important: if you don't have MFA required at onboarding and you want to force the user
// to setup MFA prior to performing the action, you should skip any checks for MFA requirement above (isMfaRequiredForAction)
// and force a registration or authentication of the MFA method.
// If the user doesn't have a passkey yet, register one first
const passkeys = await getPasskeys();
if (passkeys.length === 0) {
await registerPasskey();
}
await authenticatePasskeyMFA({
createMfaToken: { singleUse: false } // or true for one-time
});
// Now perform the action that requires Action-Based MFA
};
}