|
| 1 | +--- |
| 2 | +title: Multi-factor Auth |
| 3 | +description: Increase security by adding Multi-factor authentication to your app. |
| 4 | +next: /firestore/usage |
| 5 | +previous: /auth/phone-auth |
| 6 | +--- |
| 7 | + |
| 8 | +# iOS Setup |
| 9 | + |
| 10 | +Make sure to follow [the official Identity Platform |
| 11 | +documentation](https://cloud.google.com/identity-platform/docs/ios/mfa#enabling_multi-factor_authentication) |
| 12 | +to enable multi-factor authentication for your project and verify your app. |
| 13 | + |
| 14 | +# Enroll a new factor |
| 15 | + |
| 16 | +> Before a user can enroll a second factor they need to verify their email. See |
| 17 | +> [`User`](/reference/auth/user#sendEmailVerification) interface is returned. |
| 18 | +
|
| 19 | +Begin by obtaining a [`MultiFactorUser`](/reference/auth/multifactoruser) |
| 20 | +instance for the current user. This is the entry point for most multi-factor |
| 21 | +operations: |
| 22 | + |
| 23 | +```js |
| 24 | +import auth from '@react-native-firebase/auth'; |
| 25 | +const multiFactorUser = await auth.multiFactor(auth()); |
| 26 | +``` |
| 27 | + |
| 28 | +Request the session identifier and use the phone number obtained from the user |
| 29 | +to send a verification code: |
| 30 | + |
| 31 | +```js |
| 32 | +const session = await multiFactorUser.getSession(); |
| 33 | +const phoneOptions = { |
| 34 | + phoneNumber, |
| 35 | + session, |
| 36 | +}; |
| 37 | + |
| 38 | +// Sends a text message to the user |
| 39 | +const verificationId = await auth().verifyPhoneNumberForMultiFactor(phoneOptions); |
| 40 | +``` |
| 41 | + |
| 42 | +Once the user has provided the verification code received by text message, you |
| 43 | +can complete the process: |
| 44 | + |
| 45 | +```js |
| 46 | +const cred = auth.PhoneAuthProvider.credential(verificationId, verificationCode); |
| 47 | +const multiFactorAssertion = auth.PhoneMultiFactorGenerator.assertion(cred); |
| 48 | +await multiFactorUser.enroll(multiFactorAssertion, 'Optional display name for the user); |
| 49 | +``` |
| 50 | +
|
| 51 | +You can inspect [`User#multiFactor`](/reference/auth/user#multiFactor) for |
| 52 | +information about the user's enrolled factors. |
| 53 | + |
| 54 | +# Sign-in flow using multi-factor |
| 55 | + |
| 56 | +Ensure the account has already enrolled a second factor. Begin by calling the |
| 57 | +default sign-in methods, for example email and password. If the account requires |
| 58 | +a second factor to complete login, an exception will be raised: |
| 59 | + |
| 60 | +```js |
| 61 | +import auth from '@react-native-firebase/auth'; |
| 62 | +
|
| 63 | +auth() |
| 64 | + .signInWithEmailAndPassword(email, password) |
| 65 | + .then(() => { |
| 66 | + // User has not enrolled a second factor |
| 67 | + }) |
| 68 | + .catch(error => { |
| 69 | + const { code } = error; |
| 70 | + // Make sure to check if multi factor authentication is required |
| 71 | + if (code === 'auth/multi-factor-auth-required') { |
| 72 | + return; |
| 73 | + } |
| 74 | +
|
| 75 | + // Other error |
| 76 | + }); |
| 77 | +``` |
| 78 | + |
| 79 | +Using the error object you can obtain a |
| 80 | +[`MultiFactorResolver`](/reference/auth/multifactorresolver) instance and |
| 81 | +continue the flow: |
| 82 | + |
| 83 | +```js |
| 84 | +const resolver = auth.getMultiFactorResolver(auth(), error); |
| 85 | +``` |
| 86 | + |
| 87 | +The resolver object has all the required information to prompt the user for a |
| 88 | +specific factor: |
| 89 | + |
| 90 | +```js |
| 91 | +if (resolver.hints.length > 1) { |
| 92 | + // Use resolver.hints to display a list of second factors to the user |
| 93 | +} |
| 94 | +
|
| 95 | +// Currently only phone based factors are supported |
| 96 | +if (resolver.hints[0].factorId === auth.PhoneMultiFactorGenerator.FACTOR_ID) { |
| 97 | + // Continue with the sign-in flow |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +Using a multi-factor hint and the session information you can send a |
| 102 | +verification code to the user: |
| 103 | + |
| 104 | +```js |
| 105 | +const hint = resolver.hints[0]; |
| 106 | +const sessionId = resolver.session; |
| 107 | +
|
| 108 | +auth() |
| 109 | + .verifyPhoneNumberWithMultiFactorInfo(hint, sessionId) // triggers the message to the user |
| 110 | + .then(verificationId => setVerificationId(verificationId)); |
| 111 | +``` |
| 112 | + |
| 113 | +Once the user has entered the verification code you can create a multi-factor |
| 114 | +assertion and finish the flow: |
| 115 | + |
| 116 | +```js |
| 117 | +const credential = auth.PhoneAuthProvider.credential(verificationId, verificationCode); |
| 118 | +
|
| 119 | +const multiFactorAssertion = auth.PhoneMultiFactorGenerator.assertion(credential); |
| 120 | +
|
| 121 | +resolver.resolveSignIn(multiFactorAssertion).then(userCredential => { |
| 122 | + // additionally onAuthStateChanged will be triggered as well |
| 123 | +}); |
| 124 | +``` |
| 125 | + |
| 126 | +Upon successful sign-in, any |
| 127 | +[`onAuthStateChanged`](/auth/usage#listening-to-authentication-state) listeners |
| 128 | +will trigger with the new authentication state of the user. |
| 129 | + |
| 130 | +To put the example together: |
| 131 | + |
| 132 | +```js |
| 133 | +import auth from '@react-native-firebase/auth'; |
| 134 | +
|
| 135 | +const authInstance = auth(); |
| 136 | +
|
| 137 | +authInstance |
| 138 | + .signInWithEmailAndPassword(email, password) |
| 139 | + .then(() => { |
| 140 | + // User has not enrolled a second factor |
| 141 | + }) |
| 142 | + .catch(error => { |
| 143 | + const { code } = error; |
| 144 | + // Make sure to check if multi factor authentication is required |
| 145 | + if (code !== 'auth/multi-factor-auth-required') { |
| 146 | + const resolver = auth.getMultiFactorResolver(authInstance, error); |
| 147 | +
|
| 148 | + if (resolver.hints.length > 1) { |
| 149 | + // Use resolver.hints to display a list of second factors to the user |
| 150 | + } |
| 151 | +
|
| 152 | + // Currently only phone based factors are supported |
| 153 | + if (resolver.hints[0].factorId === auth.PhoneMultiFactorGenerator.FACTOR_ID) { |
| 154 | + const hint = resolver.hints[0]; |
| 155 | + const sessionId = resolver.session; |
| 156 | +
|
| 157 | + authInstance |
| 158 | + .verifyPhoneNumberWithMultiFactorInfo(hint, sessionId) // triggers the message to the user |
| 159 | + .then(verificationId => setVerificationId(verificationId)); |
| 160 | +
|
| 161 | + // Request verificationCode from user |
| 162 | +
|
| 163 | + const credential = auth.PhoneAuthProvider.credential(verificationId, verificationCode); |
| 164 | +
|
| 165 | + const multiFactorAssertion = auth.PhoneMultiFactorGenerator.assertion(credential); |
| 166 | +
|
| 167 | + resolver.resolveSignIn(multiFactorAssertion).then(userCredential => { |
| 168 | + // additionally onAuthStateChanged will be triggered as well |
| 169 | + }); |
| 170 | + } |
| 171 | + } |
| 172 | + }); |
| 173 | +``` |
| 174 | + |
| 175 | +# Testing |
| 176 | + |
| 177 | +You can define test phone numbers and corresponding verification codes. The |
| 178 | +official[official |
| 179 | +guide](https://cloud.google.com/identity-platform/docs/ios/mfa#enabling_multi-factor_authentication) |
| 180 | +contains more information on setting this up. |
0 commit comments