Using custom UIs
By default, Privy’s wallet MFA feature will use Privy’s UIs for enrolling users in MFA, managing MFA methods, and having users complete MFA to authorize signatures and transactions for the embedded wallet.
This section is intended only for apps that would like to use wallet MFA with their own custom UIs, instead of Privy’s defaults.
Implementing wallet MFA with custom UIs is substantially more involved than integrating Privy’s out-of-the-box wallet MFA feature. Make sure to consider the development trade-offs of using custom UIs over Privy defaults before finalizing on your approach!
Configuring MFA to be used with custom UIs
If you plan to use your own custom UIs for wallet MFA, set the mfa.noPromptOnMfaRequired
field to true in the Privy provider.
This will configure Privy to not show its default UIs for wallet MFA, and instead rely on your custom implementation.
Enrolling in MFA
To enroll your users in MFA, use the hook from Privy. Currently, users can enroll in three MFA methods:
- SMS, where users authenticate with a 6-digit MFA code sent to their phone number
- TOTP, where users authenticate with a 6-digit MFA code from an authentication app, like Authy or Google Authenticator
- Passkey, where users verify with a previously registered passkey, generally through biometric authentication on their device
SMS
To enroll your users in MFA with SMS, use the and methods returned by the hook:
First, initiate enrollment by prompting your user to enter the phone number they’d like to use for MFA. Then, call Privy’s method. As a parameter to this , pass a JSON object with a field that contains the user’s provide phone number as a string.
Once is called with a valid , Privy will then send a 6-digit MFA enrollment code to the provided number. This method returns a Promise
that will resolve to void
if the code was successfully sent, or will reject with an error
if there was an error sending the code (e.g. invalid phone number).
Next, prompt the user to enter the 6-digit code that was sent to their phone number, and use the method to complete enrollment of that phone number. As a parameter to , you must pass a JSON object with both the original that the user enrolled, and the they received at that number.
If your app has enabled SMS as a possible login method, users will not be able to enroll SMS as a valid MFA method.
SMS must be either be used as a login method to secure user accounts, or as an MFA method for additional security on the users’ wallets.
TOTP
To enroll your users in MFA with TOTP, use the and methods returned by the hook:
First, initiate enrollment by calling Privy’s method with no parameters. This method returns a Promise
for an and that the user will need in order to complete enrollment.
Then, to have the user enroll, you can either:
- display the TOTP as a QR code to the user, and prompt them to scan it with their TOTP client (commonly, a mobile app like Google Authenticator or Authy)
- allow the user to copy the TOTP and paste it into their TOTP client
You can directly pass in the from above into a library like to render the URL as a QR code to your user.
Once your user has successfully scanned the QR code, an enrollment code for Privy will appear within their TOTP client. Prompt the user to enter this code in your app, and call Privy’s method. As a parameter to , pass a JSON object with an field that contains the MFA code from the user as a string.
Passkey
In order to use passkeys as a MFA method, make sure a valid passkey is linked to the user. You can set this up by following the steps here!
To enroll your users in MFA with passkeys, use the and methods returned by the hook:
First, initiate enrollment by calling Privy’s method with no parameters. This method returns a Promise
that will resolve to void
indicating success.
Then, to have the user enroll, you must call Privy’s submitEnrollmentWithPasskey
method with a list of the user’s passkey account credentialIds
. You can find this list by querying the user
’s linkedAccounts
array for all accounts of type: 'passkey'
:
Completing MFA
Once users have successfully enrolled in MFA with Privy, they will be required to complete MFA whenever the private key for their embedded wallet must be used. This includes signing messages and transactions, recovering the embedded wallet on new devices, exporting the wallet’s private key, and setting a password on the wallet.
To ensure users can complete MFA when required, your app must do the following:
- Set up a flow to guide the user through completing MFA when required.
- Register an event listener to configure Privy to invoke the flow from step (1) whenever MFA is required.
Once a user has completed MFA on a given device, they can continue to use the wallet on that device without needing to complete MFA for 15 minutes.
After 15 minutes have elapsed, Privy will require that the user complete MFA again to re-authorize use of the wallet’s private key.
Guiding the user through MFA
To set up a flow to have the user complete MFA, use Privy’s hook.
This flow has three core components:
- Requesting an MFA code be sent to the user’s MFA method ()
- Submitting the MFA code provided by the user ()
- Cancelling an in-progress MFA flow ()
Requesting an MFA challenge
To request an MFA challenge for the current user, call the method from the hook, passing the user’s desired MFA method ('sms'
, 'totp'
, or 'passkey'
) as a parameter.
The method will then prepare an MFA challenge for the desired MFA method, and returns a Promise
that resolves if the challenge was successfully created, and rejects with an error if there was an issue.
- If the MFA method is
'sms'
, the user will receive an SMS with their MFA code at the phone number they originally enrolled. - If the MFA method is
'totp'
, the user will receive the MFA code within their authenticator app. - If the MFA method is
'passkey'
,init
will return an object with options to pass to the native passkey system
Submitting the MFA verification
Once has resolved successfully, prompt the user to get their MFA code from their MFA method and to enter it within your app. Then, call the method from . As parameters to , pass the MFA method being used ('sms'
or 'totp'
) and the MFA code that the user entered.
Cancelling the MFA flow
After has been called and the corresponding call has not yet occurred, the user may cancel their in-progress MFA flow if they wish.
To cancel the current MFA flow, call the method from the hook.
Registering an event listener
Once you’ve set up your app’s logic for guiding a user to complete MFA, you lastly need to configure Privy to invoke this logic whenever MFA is required by the user’s embedded wallet.
To set up this configuration, use Privy’s hook. As a parameter to , you must pass a JSON object with an callback, described below.
onMfaRequired
Privy will invoke the 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 hook, as documented above). Within this callback, you can also access an parameter that contains a list of available MFA methods that the user has enrolled in ('sms'
and/or 'totp'
and/or 'passkey'
).
In order for Privy to invoke your app’s MFA flow, the component that calls Privy’s hook must be mounted whenever the user’s embedded wallet requires that they complete MFA.
In kind, 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.
Handling errors with code submission
When both enrolling in and completing MFA, Privy sends a 6-digit code to the user’s selected MFA method, that the user must submit to Privy in order to verify their identity.
When submitting this MFA code, Privy may respond with an error if the code is incorrect, if the user has reached the maximum number of attempts for this MFA flow, or if the MFA flow has timed out. If the user enters in an incorrect code (e.g. by mistyping), the user is allowed to retry code submission up to a maximum of four attempts.
To simplify handling errors with MFA code submission, Privy provides the following helper functions to parse errors raised by the MFA code submission methods listed above. Each of these functions accepts the raw raised as a parameter, and returns a Boolean
indicating if the error meets a certain condition:
- : indicates the user entered an incorrect MFA code
- indicates has reached the maximum number of attempts for this MFA flow, and that a new MFA code must be requested via
- indicates that the current MFA code has expired, and that a new MFA code must be requested via
As an example, to handle errors raised by , you might use these helpers like so:
Was this page helpful?