@@ -21,8 +21,14 @@ instance for the current user. This is the entry point for most multi-factor
2121operations:
2222
2323``` js
24- import auth from ' @react-native-firebase/auth' ;
25- const multiFactorUser = await auth ().multiFactor (auth ().currentUser );
24+ import {
25+ PhoneAuthProvider ,
26+ PhoneMultiFactorGenerator ,
27+ getAuth ,
28+ multiFactor ,
29+ } from ' @react-native-firebase/auth' ;
30+
31+ const multiFactorUser = await multiFactor (getAuth ().currentUser );
2632```
2733
2834Request the session identifier and use the phone number obtained from the user
@@ -36,15 +42,15 @@ const phoneOptions = {
3642};
3743
3844// Sends a text message to the user
39- const verificationId = await auth (). verifyPhoneNumberForMultiFactor (phoneOptions);
45+ const verificationId = await new PhoneAuthProvider ( getAuth ()). verifyPhoneNumber (phoneOptions);
4046```
4147
4248Once the user has provided the verification code received by text message, you
4349can complete the process:
4450
4551``` js
46- const cred = auth . PhoneAuthProvider .credential (verificationId, verificationCode);
47- const multiFactorAssertion = auth . PhoneMultiFactorGenerator .assertion (cred);
52+ const cred = PhoneAuthProvider .credential (verificationId, verificationCode);
53+ const multiFactorAssertion = PhoneMultiFactorGenerator .assertion (cred);
4854await multiFactorUser .enroll (multiFactorAssertion, ' Optional display name for the user' );
4955```
5056
@@ -58,10 +64,15 @@ default sign-in methods, for example email and password. If the account requires
5864a second factor to complete login, an exception will be raised:
5965
6066``` js
61- import auth from ' @react-native-firebase/auth' ;
62-
63- auth ()
64- .signInWithEmailAndPassword (email, password)
67+ import {
68+ PhoneAuthProvider ,
69+ PhoneMultiFactorGenerator ,
70+ getAuth ,
71+ signInWithEmailAndPassword ,
72+ getMultiFactorResolver ,
73+ } from ' @react-native-firebase/auth' ;
74+
75+ signInWithEmailAndPassword (getAuth (), email, password)
6576 .then (() => {
6677 // User has not enrolled a second factor
6778 })
@@ -81,7 +92,7 @@ Using the error object you can obtain a
8192continue the flow:
8293
8394``` js
84- const resolver = auth (). getMultiFactorResolver (error);
95+ const resolver = getMultiFactorResolver (getAuth (), error);
8596```
8697
8798The resolver object has all the required information to prompt the user for a
@@ -93,7 +104,7 @@ if (resolver.hints.length > 1) {
93104}
94105
95106// Currently only phone based factors are supported
96- if (resolver .hints [0 ].factorId === auth . PhoneMultiFactorGenerator .FACTOR_ID ) {
107+ if (resolver .hints [0 ].factorId === PhoneMultiFactorGenerator .FACTOR_ID ) {
97108 // Continue with the sign-in flow
98109}
99110```
@@ -105,18 +116,18 @@ verification code to the user:
105116const hint = resolver .hints [0 ];
106117const sessionId = resolver .session ;
107118
108- auth ( )
109- .verifyPhoneNumberWithMultiFactorInfo (hint, sessionId) // triggers the message to the user
119+ new PhoneAuthProvider ( getAuth () )
120+ .verifyPhoneNumber (hint, sessionId) // triggers the message to the user
110121 .then (verificationId => setVerificationId (verificationId));
111122```
112123
113124Once the user has entered the verification code you can create a multi-factor
114125assertion and finish the flow:
115126
116127``` js
117- const credential = auth . PhoneAuthProvider .credential (verificationId, verificationCode);
128+ const credential = PhoneAuthProvider .credential (verificationId, verificationCode);
118129
119- const multiFactorAssertion = auth . PhoneMultiFactorGenerator .assertion (credential);
130+ const multiFactorAssertion = PhoneMultiFactorGenerator .assertion (credential);
120131
121132resolver .resolveSignIn (multiFactorAssertion).then (userCredential => {
122133 // additionally onAuthStateChanged will be triggered as well
@@ -130,39 +141,42 @@ will trigger with the new authentication state of the user.
130141To put the example together:
131142
132143``` js
133- import auth from ' @react-native-firebase/auth' ;
134-
135- const authInstance = auth ();
136-
137- authInstance
138- .signInWithEmailAndPassword (email, password)
144+ import {
145+ PhoneAuthProvider ,
146+ PhoneMultiFactorGenerator ,
147+ getAuth ,
148+ signInWithEmailAndPassword ,
149+ getMultiFactorResolver ,
150+ } from ' @react-native-firebase/auth' ;
151+
152+ signInWithEmailAndPassword (getAuth (), email, password)
139153 .then (() => {
140154 // User has not enrolled a second factor
141155 })
142156 .catch (error => {
143157 const { code } = error;
144158 // Make sure to check if multi factor authentication is required
145159 if (code === ' auth/multi-factor-auth-required' ) {
146- const resolver = auth . getMultiFactorResolver (error);
160+ const resolver = getMultiFactorResolver (error);
147161
148162 if (resolver .hints .length > 1 ) {
149163 // Use resolver.hints to display a list of second factors to the user
150164 }
151165
152166 // Currently only phone based factors are supported
153- if (resolver .hints [0 ].factorId === auth . PhoneMultiFactorGenerator .FACTOR_ID ) {
167+ if (resolver .hints [0 ].factorId === PhoneMultiFactorGenerator .FACTOR_ID ) {
154168 const hint = resolver .hints [0 ];
155169 const sessionId = resolver .session ;
156170
157- authInstance
158- .verifyPhoneNumberWithMultiFactorInfo (hint, sessionId) // triggers the message to the user
171+ new PhoneAuthProvider ( getAuth ())
172+ .verifyPhoneNumber (hint, sessionId) // triggers the message to the user
159173 .then (verificationId => setVerificationId (verificationId));
160174
161175 // Request verificationCode from user
162176
163- const credential = auth . PhoneAuthProvider .credential (verificationId, verificationCode);
177+ const credential = PhoneAuthProvider .credential (verificationId, verificationCode);
164178
165- const multiFactorAssertion = auth . PhoneMultiFactorGenerator .assertion (credential);
179+ const multiFactorAssertion = PhoneMultiFactorGenerator .assertion (credential);
166180
167181 resolver .resolveSignIn (multiFactorAssertion).then (userCredential => {
168182 // additionally onAuthStateChanged will be triggered as well
0 commit comments