Enrolling in MFA does not automatically verify the user for wallet operations. Once enrolled,
subsequent wallet actions will require MFA verification. See the verification
guides for how to complete MFA
verification.
Enroll users in MFA using SMS, where they authenticate with a 6-digit code sent to their phone 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 either be used as a login method to secure user accounts, or as an MFA method for additional security on the users’ wallets, but cannot be used for both.
First, prompt your user to enter the phone number they’d like to use for MFA. Then, call Privy’s initEnrollmentWithSms method. As a parameter, pass a JSON object with a phoneNumber field that contains the user’s provided phone number as a string.
Report incorrect code
Copy
Ask AI
// Prompt the user for their phone numberconst phoneNumberInput = 'insert-phone-number-from-user';// Send an enrollment code to their phone numberawait initEnrollmentWithSms({phoneNumber: phoneNumberInput});
Once initEnrollmentWithSms is called with a valid phone number, Privy will 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 submitEnrollmentWithSms method to complete enrollment. As a parameter, you must pass a JSON object with both the original phoneNumber that the user enrolled, and the mfaCode they received at that number.
Report incorrect code
Copy
Ask AI
// Prompt the user for the code sent to their phone numberconst mfaCodeInput = 'insert-mfa-code-received-by-user';await submitEnrollmentWithSms({ phoneNumber: phoneNumberInput, // From above mfaCode: mfaCodeInput,});
See an end-to-end example of enrolling users in MFA with SMS
The component below serves as a reference implementation for how to enroll your users in MFA with SMS!
Example enrolling a phone number for MFA
Report incorrect code
Copy
Ask AI
import {useMfaEnrollment} from '@privy-io/react-auth';export default function MfaEnrollmentWithSms() { const {initEnrollmentWithSms, submitEnrollmentWithSms} = useMfaEnrollment(); const [phoneNumber, setPhoneNumber] = useState<string | null>(null); const [mfaCode, setMfaCode] = useState<string | null>(null); const [pendingMfaCode, setPendingMfaCode] = useState<boolean>(false); // Handler for when the user enters their phone number to enroll in MFA. const onEnteredPhoneNumber = () => { await initEnrollmentWithSms({phoneNumber: phoneNumber}); // Sends an MFA code to the `phoneNumber` setPendingMfaCode(true); } // Handler for when the user enters the MFA code sent to their phone number. const onEnteredMfaCode = () => { await submitEnrollmentWithSms({phoneNumber: phoneNumber, mfaCode: mfaCode}); // See the error handling guide for details on how to handle errors setPendingMfaCode(false); } // If no MFA code has been sent yet, prompt the user for their phone number to enroll if (!pendingMfaCode) { // Input field for the user to enter the phone number they'd like to enroll for MFA return <> <input placeholder='(555) 555 5555' onChange={(event) => setPhoneNumber(event.target.value)}/> <button onClick={onEnteredPhoneNumber}>Enroll a Phone with MFA</button> </>; } // Input field for the user to enter the MFA code sent to their phone number return <> <input placeholder='123456' onChange={(event) => setMfaCode(event.target.value)}/> <button onClick={onEnteredMfaCode}>Submit Enrollment Code</button> </>;}
First, prompt your user to enter the phone number they’d like to use for MFA. Then, call Privy’s initMfaEnrollment method with the appropriate parameters:
Report incorrect code
Copy
Ask AI
// Prompt the user for their phone numberconst phoneNumberInput = 'insert-phone-number-from-user';// Send an enrollment code to their phone numberawait initMfaEnrollment({method: 'sms', phoneNumber: phoneNumberInput});
Once initMfaEnrollment is called with a valid phone number, Privy will send a 6-digit MFA enrollment code to the provided number.
Next, prompt the user to enter the 6-digit code that was sent to their phone number, and use the submitMfaEnrollment method to complete enrollment:
Report incorrect code
Copy
Ask AI
// Prompt the user for the code sent to their phone numberconst mfaCodeInput = 'insert-mfa-code-received-by-user';await submitMfaEnrollment({ method: 'sms', phoneNumber: phoneNumberInput, // From above code: mfaCodeInput,});
See an end-to-end example of enrolling users in MFA with SMS
The component below serves as a reference implementation for how to enroll your users in MFA with SMS!
Example enrolling a phone number for MFA
Report incorrect code
Copy
Ask AI
import {useMfaEnrollment} from '@privy-io/expo';export default function MfaEnrollmentWithSms() { const {initMfaEnrollment, submitMfaEnrollment} = useMfaEnrollment(); const [phoneNumber, setPhoneNumber] = useState<string | null>(null); const [mfaCode, setMfaCode] = useState<string | null>(null); const [pendingMfaCode, setPendingMfaCode] = useState<boolean>(false); // Handler for when the user enters their phone number to enroll in MFA. const onEnteredPhoneNumber = () => { await initMfaEnrollment({method: 'sms', phoneNumber: phoneNumber}); // Sends an MFA code to the `phoneNumber` setPendingMfaCode(true); } // Handler for when the user enters the MFA code sent to their phone number. const onEnteredMfaCode = () => { await submitMfaEnrollment({method: 'sms', phoneNumber: phoneNumber, code: mfaCode}); // See the error handling guide for details on how to handle errors setPendingMfaCode(false); } // If no MFA code has been sent yet, prompt the user for their phone number to enroll if (!pendingMfaCode) { // Input field for the user to enter the phone number they'd like to enroll for MFA return ( <YStack gap={12}> <Input value={phoneNumber} onChangeText={setPhoneNumber} flex={1} keyboardType="number-pad" /> <Button onPress={onEnteredPhoneNumber} flex={1}> <Text>Enroll a Phone with MFA</Text> </Button> </YStack> ) } // Input field for the user to enter the MFA code sent to their phone number return <> <Input value={mfaCode} onChangeText={setMfaCode}/> <Button onPress={onEnteredMfaCode}><Text>Submit Enrollment Code</Text></Button> </>;}
First, prompt your user to enter the phone number they’d like to use for MFA. Then, call Privy’s sendCode method with the user’s phone number:
Report incorrect code
Copy
Ask AI
guard let user = await privy.getUser() else { return }// Prompt the user for their phone numberlet phoneNumber = "+15555555555"// Send an enrollment code to their phone numbertry await user.mfa.sms.enroll.sendCode(to: phoneNumber)
Once sendCode is called with a valid phone number, Privy will send a 6-digit MFA enrollment code to the provided number. The method throws 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 submit method to complete enrollment. You must pass both the code that the user entered and the phoneNumber it was sent to:
Report incorrect code
Copy
Ask AI
// Prompt the user for the code sent to their phone numberlet mfaCode = "123456"let updatedUser = try await user.mfa.sms.enroll.submit(code: mfaCode, sentTo: phoneNumber)// The updated user object will contain the newly enrolled mfaMethodprint(updatedUser.mfaMethods)