@@ -21,8 +21,10 @@ 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 authModule , { PhoneAuthProvider , getAuth , multiFactor } from ' @react-native-firebase/auth' ;
25+
26+ const auth = getAuth ();
27+ const multiFactorUser = await multiFactor (auth .currentUser );
2628```
2729
2830Request the session identifier and use the phone number obtained from the user
@@ -36,15 +38,15 @@ const phoneOptions = {
3638};
3739
3840// Sends a text message to the user
39- const verificationId = await auth () .verifyPhoneNumberForMultiFactor (phoneOptions);
41+ const verificationId = await auth .verifyPhoneNumberForMultiFactor (phoneOptions);
4042```
4143
4244Once the user has provided the verification code received by text message, you
4345can complete the process:
4446
4547``` js
46- const cred = auth . PhoneAuthProvider .credential (verificationId, verificationCode);
47- const multiFactorAssertion = auth .PhoneMultiFactorGenerator .assertion (cred);
48+ const cred = PhoneAuthProvider .credential (verificationId, verificationCode);
49+ const multiFactorAssertion = authModule .PhoneMultiFactorGenerator .assertion (cred);
4850await multiFactorUser .enroll (multiFactorAssertion, ' Optional display name for the user' );
4951```
5052
@@ -58,10 +60,16 @@ default sign-in methods, for example email and password. If the account requires
5860a second factor to complete login, an exception will be raised:
5961
6062``` js
61- import auth from ' @react-native-firebase/auth' ;
63+ import authModule, {
64+ PhoneAuthProvider ,
65+ getAuth ,
66+ signInWithEmailAndPassword ,
67+ getMultiFactorResolver ,
68+ } from ' @react-native-firebase/auth' ;
69+
70+ const auth = getAuth ();
6271
63- auth ()
64- .signInWithEmailAndPassword (email, password)
72+ signInWithEmailAndPassword (auth, email, password)
6573 .then (() => {
6674 // User has not enrolled a second factor
6775 })
@@ -81,7 +89,7 @@ Using the error object you can obtain a
8189continue the flow:
8290
8391``` js
84- const resolver = auth (). getMultiFactorResolver (error);
92+ const resolver = getMultiFactorResolver (auth, error);
8593```
8694
8795The resolver object has all the required information to prompt the user for a
@@ -93,7 +101,7 @@ if (resolver.hints.length > 1) {
93101}
94102
95103// Currently only phone based factors are supported
96- if (resolver .hints [0 ].factorId === auth .PhoneMultiFactorGenerator .FACTOR_ID ) {
104+ if (resolver .hints [0 ].factorId === authModule .PhoneMultiFactorGenerator .FACTOR_ID ) {
97105 // Continue with the sign-in flow
98106}
99107```
@@ -105,7 +113,7 @@ verification code to the user:
105113const hint = resolver .hints [0 ];
106114const sessionId = resolver .session ;
107115
108- auth ()
116+ auth
109117 .verifyPhoneNumberWithMultiFactorInfo (hint, sessionId) // triggers the message to the user
110118 .then (verificationId => setVerificationId (verificationId));
111119```
@@ -114,9 +122,9 @@ Once the user has entered the verification code you can create a multi-factor
114122assertion and finish the flow:
115123
116124``` js
117- const credential = auth . PhoneAuthProvider .credential (verificationId, verificationCode);
125+ const credential = PhoneAuthProvider .credential (verificationId, verificationCode);
118126
119- const multiFactorAssertion = auth .PhoneMultiFactorGenerator .assertion (credential);
127+ const multiFactorAssertion = authModule .PhoneMultiFactorGenerator .assertion (credential);
120128
121129resolver .resolveSignIn (multiFactorAssertion).then (userCredential => {
122130 // additionally onAuthStateChanged will be triggered as well
@@ -130,39 +138,43 @@ will trigger with the new authentication state of the user.
130138To put the example together:
131139
132140``` js
133- import auth from ' @react-native-firebase/auth' ;
141+ import authModule, {
142+ PhoneAuthProvider ,
143+ getAuth ,
144+ signInWithEmailAndPassword ,
145+ getMultiFactorResolver ,
146+ } from ' @react-native-firebase/auth' ;
134147
135- const authInstance = auth ();
148+ const auth = getAuth ();
136149
137- authInstance
138- .signInWithEmailAndPassword (email, password)
150+ signInWithEmailAndPassword (auth, email, password)
139151 .then (() => {
140152 // User has not enrolled a second factor
141153 })
142154 .catch (error => {
143155 const { code } = error;
144156 // Make sure to check if multi factor authentication is required
145157 if (code === ' auth/multi-factor-auth-required' ) {
146- const resolver = auth . getMultiFactorResolver (error);
158+ const resolver = getMultiFactorResolver (error);
147159
148160 if (resolver .hints .length > 1 ) {
149161 // Use resolver.hints to display a list of second factors to the user
150162 }
151163
152164 // Currently only phone based factors are supported
153- if (resolver .hints [0 ].factorId === auth .PhoneMultiFactorGenerator .FACTOR_ID ) {
165+ if (resolver .hints [0 ].factorId === authModule .PhoneMultiFactorGenerator .FACTOR_ID ) {
154166 const hint = resolver .hints [0 ];
155167 const sessionId = resolver .session ;
156168
157- authInstance
169+ auth
158170 .verifyPhoneNumberWithMultiFactorInfo (hint, sessionId) // triggers the message to the user
159171 .then (verificationId => setVerificationId (verificationId));
160172
161173 // Request verificationCode from user
162174
163- const credential = auth . PhoneAuthProvider .credential (verificationId, verificationCode);
175+ const credential = PhoneAuthProvider .credential (verificationId, verificationCode);
164176
165- const multiFactorAssertion = auth .PhoneMultiFactorGenerator .assertion (credential);
177+ const multiFactorAssertion = authModule .PhoneMultiFactorGenerator .assertion (credential);
166178
167179 resolver .resolveSignIn (multiFactorAssertion).then (userCredential => {
168180 // additionally onAuthStateChanged will be triggered as well
0 commit comments