@@ -21,8 +21,14 @@ instance for the current user. This is the entry point for most multi-factor
21
21
operations:
22
22
23
23
``` 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 );
26
32
```
27
33
28
34
Request the session identifier and use the phone number obtained from the user
@@ -36,15 +42,15 @@ const phoneOptions = {
36
42
};
37
43
38
44
// Sends a text message to the user
39
- const verificationId = await auth (). verifyPhoneNumberForMultiFactor (phoneOptions);
45
+ const verificationId = await new PhoneAuthProvider ( getAuth ()). verifyPhoneNumber (phoneOptions);
40
46
```
41
47
42
48
Once the user has provided the verification code received by text message, you
43
49
can complete the process:
44
50
45
51
``` 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);
48
54
await multiFactorUser .enroll (multiFactorAssertion, ' Optional display name for the user' );
49
55
```
50
56
@@ -58,10 +64,15 @@ default sign-in methods, for example email and password. If the account requires
58
64
a second factor to complete login, an exception will be raised:
59
65
60
66
``` 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)
65
76
.then (() => {
66
77
// User has not enrolled a second factor
67
78
})
@@ -81,7 +92,7 @@ Using the error object you can obtain a
81
92
continue the flow:
82
93
83
94
``` js
84
- const resolver = auth (). getMultiFactorResolver (error);
95
+ const resolver = getMultiFactorResolver (getAuth (), error);
85
96
```
86
97
87
98
The resolver object has all the required information to prompt the user for a
@@ -93,7 +104,7 @@ if (resolver.hints.length > 1) {
93
104
}
94
105
95
106
// 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 ) {
97
108
// Continue with the sign-in flow
98
109
}
99
110
```
@@ -105,18 +116,18 @@ verification code to the user:
105
116
const hint = resolver .hints [0 ];
106
117
const sessionId = resolver .session ;
107
118
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
110
121
.then (verificationId => setVerificationId (verificationId));
111
122
```
112
123
113
124
Once the user has entered the verification code you can create a multi-factor
114
125
assertion and finish the flow:
115
126
116
127
``` js
117
- const credential = auth . PhoneAuthProvider .credential (verificationId, verificationCode);
128
+ const credential = PhoneAuthProvider .credential (verificationId, verificationCode);
118
129
119
- const multiFactorAssertion = auth . PhoneMultiFactorGenerator .assertion (credential);
130
+ const multiFactorAssertion = PhoneMultiFactorGenerator .assertion (credential);
120
131
121
132
resolver .resolveSignIn (multiFactorAssertion).then (userCredential => {
122
133
// additionally onAuthStateChanged will be triggered as well
@@ -130,39 +141,42 @@ will trigger with the new authentication state of the user.
130
141
To put the example together:
131
142
132
143
``` 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)
139
153
.then (() => {
140
154
// User has not enrolled a second factor
141
155
})
142
156
.catch (error => {
143
157
const { code } = error;
144
158
// Make sure to check if multi factor authentication is required
145
159
if (code === ' auth/multi-factor-auth-required' ) {
146
- const resolver = auth . getMultiFactorResolver (error);
160
+ const resolver = getMultiFactorResolver (error);
147
161
148
162
if (resolver .hints .length > 1 ) {
149
163
// Use resolver.hints to display a list of second factors to the user
150
164
}
151
165
152
166
// 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 ) {
154
168
const hint = resolver .hints [0 ];
155
169
const sessionId = resolver .session ;
156
170
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
159
173
.then (verificationId => setVerificationId (verificationId));
160
174
161
175
// Request verificationCode from user
162
176
163
- const credential = auth . PhoneAuthProvider .credential (verificationId, verificationCode);
177
+ const credential = PhoneAuthProvider .credential (verificationId, verificationCode);
164
178
165
- const multiFactorAssertion = auth . PhoneMultiFactorGenerator .assertion (credential);
179
+ const multiFactorAssertion = PhoneMultiFactorGenerator .assertion (credential);
166
180
167
181
resolver .resolveSignIn (multiFactorAssertion).then (userCredential => {
168
182
// additionally onAuthStateChanged will be triggered as well
0 commit comments