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