Calling registerTotpMfaDevice will return a URI and a secret key for the TOTP device.
You can use the URI to display a QR code to the user, so they can scan it with their authenticator app to get a TOTP code.Unlike passkeys, registering a TOTP device will not automatically authenticate it, so you need to call authenticateTotpMfaDevice after registering
a TOTP device for the user to complete an MFA challenge.
Calling authenticateTotpMfaDevice will verify the TOTP code and complete the MFA challenge.
The authentication will be successful if the user enters a valid TOTP code for the registered TOTP device.
Calling deleteMfaDevice will delete a TOTP device associated with the authenticated user.To delete a TOTP device, you first need to get the user to perform an authentication challenge with that TOTP device, and then use the deleteMfaDevice function with that MFA token.
import { deleteMfaDevice, authenticateTotpMfaDevice } from '@dynamic-labs-sdk/client';const deleteTotpDevice = async () => { // Authenticate with the TOTP device to be deleted // Replace '123456' with the actual TOTP code the user enters await authenticateTotpMfaDevice({ code: '123456', createMfaTokenOptions: { singleUse: true }, }); // Get the MFA token from the dynamic client const mfaToken = dynamicClient.mfaToken; // Replace 'device-id' with the actual ID of the TOTP device you want to delete // Replace 'mfa-auth-token' with the actual MFA authentication token await deleteMfaDevice({ deviceId: 'device-id', mfaAuthToken: mfaToken });};
The TOTP flow requires multiple steps: register the device with useRegisterTotpMfaDevice (its data holds the URI/QR code), then verify with useAuthenticateTotpMfaDevice:
import { useAuthenticateTotpMfaDevice, useRegisterTotpMfaDevice,} from '@dynamic-labs-sdk/react-hooks';import { useState } from 'react';function TotpEnrollment() { const [code, setCode] = useState(''); const { mutate: registerDevice, data: registration } = useRegisterTotpMfaDevice(); const { mutate: authenticateDevice, isSuccess: enrolled } = useAuthenticateTotpMfaDevice(); if (enrolled) return <p>TOTP device enrolled successfully.</p>; return ( <div> {!registration ? ( <button onClick={() => registerDevice()}>Set Up Authenticator App</button> ) : ( <form onSubmit={(e) => { e.preventDefault(); authenticateDevice({ code }); }} > <p>Scan this QR code with your authenticator app:</p> {/* render QR code from registration.uri using a QR library */} <input value={code} onChange={(e) => setCode(e.target.value)} placeholder="Enter 6-digit code" maxLength={6} /> <button type="submit">Verify</button> </form> )} </div> );}