-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathindex.js
412 lines (351 loc) · 11.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
* Copyright (c) 2016-present Invertase Limited & Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this library except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import {
isAndroid,
isBoolean,
isString,
isNull,
isValidUrl,
} from '@react-native-firebase/app/lib/common';
import {
createModuleNamespace,
FirebaseModule,
getFirebaseRoot,
} from '@react-native-firebase/app/lib/internal';
import ConfirmationResult from './ConfirmationResult';
import PhoneAuthListener from './PhoneAuthListener';
import EmailAuthProvider from './providers/EmailAuthProvider';
import FacebookAuthProvider from './providers/FacebookAuthProvider';
import GithubAuthProvider from './providers/GithubAuthProvider';
import GoogleAuthProvider from './providers/GoogleAuthProvider';
import OAuthProvider from './providers/OAuthProvider';
import PhoneAuthProvider from './providers/PhoneAuthProvider';
import TwitterAuthProvider from './providers/TwitterAuthProvider';
import AppleAuthProvider from './providers/AppleAuthProvider';
import Settings from './Settings';
import User from './User';
import version from './version';
const statics = {
AppleAuthProvider,
EmailAuthProvider,
PhoneAuthProvider,
GoogleAuthProvider,
GithubAuthProvider,
TwitterAuthProvider,
FacebookAuthProvider,
OAuthProvider,
PhoneAuthState: {
CODE_SENT: 'sent',
AUTO_VERIFY_TIMEOUT: 'timeout',
AUTO_VERIFIED: 'verified',
ERROR: 'error',
},
};
const namespace = 'auth';
const nativeModuleName = 'RNFBAuthModule';
class FirebaseAuthModule extends FirebaseModule {
constructor(...args) {
super(...args);
this._user = null;
this._settings = null;
this._authResult = false;
this._languageCode = this.native.APP_LANGUAGE[this.app._name];
this._tenantId = null;
if (!this.languageCode) {
this._languageCode = this.native.APP_LANGUAGE['[DEFAULT]'];
}
if (this.native.APP_USER[this.app._name]) {
this._setUser(this.native.APP_USER[this.app._name]);
}
this.emitter.addListener(this.eventNameForApp('auth_state_changed'), event => {
this._setUser(event.user);
this.emitter.emit(this.eventNameForApp('onAuthStateChanged'), this._user);
});
this.emitter.addListener(this.eventNameForApp('phone_auth_state_changed'), event => {
const eventKey = `phone:auth:${event.requestKey}:${event.type}`;
this.emitter.emit(eventKey, event.state);
});
this.emitter.addListener(this.eventNameForApp('auth_id_token_changed'), auth => {
this._setUser(auth.user);
this.emitter.emit(this.eventNameForApp('onIdTokenChanged'), this._user);
});
this.native.addAuthStateListener();
this.native.addIdTokenListener();
}
get languageCode() {
return this._languageCode;
}
get tenantId() {
return this._tenantId;
}
get settings() {
if (!this._settings) {
this._settings = new Settings(this);
}
return this._settings;
}
get currentUser() {
return this._user;
}
_setUser(user) {
this._user = user ? new User(this, user) : null;
this._authResult = true;
this.emitter.emit(this.eventNameForApp('onUserChanged'), this._user);
return this._user;
}
_setUserCredential(userCredential) {
const user = new User(this, userCredential.user);
this._user = user;
this._authResult = true;
this.emitter.emit(this.eventNameForApp('onUserChanged'), this._user);
return {
additionalUserInfo: userCredential.additionalUserInfo,
user,
};
}
async setLanguageCode(code) {
if (!isString(code) && !isNull(code)) {
throw new Error(
"firebase.auth().setLanguageCode(*) expected 'languageCode' to be a string or null value",
);
}
await this.native.setLanguageCode(code);
if (code === null) {
this._languageCode = this.native.APP_LANGUAGE[this.app._name];
if (!this.languageCode) {
this._languageCode = this.native.APP_LANGUAGE['[DEFAULT]'];
}
} else {
this._languageCode = code;
}
}
async setTenantId(tenantId) {
if (!isString(tenantId)) {
throw new Error("firebase.auth().setTenantId(*) expected 'tenantId' to be a string");
}
this._tenantId = tenantId;
await this.native.setTenantId(tenantId);
}
_parseListener(listenerOrObserver) {
return typeof listenerOrObserver === 'object'
? listenerOrObserver.next.bind(listenerOrObserver)
: listenerOrObserver;
}
onAuthStateChanged(listenerOrObserver) {
const listener = this._parseListener(listenerOrObserver);
const subscription = this.emitter.addListener(
this.eventNameForApp('onAuthStateChanged'),
listener,
);
if (this._authResult) {
Promise.resolve().then(() => {
listener(this._user || null);
});
}
return () => subscription.remove();
}
onIdTokenChanged(listenerOrObserver) {
const listener = this._parseListener(listenerOrObserver);
const subscription = this.emitter.addListener(
this.eventNameForApp('onIdTokenChanged'),
listener,
);
if (this._authResult) {
Promise.resolve().then(() => {
listener(this._user || null);
});
}
return () => subscription.remove();
}
onUserChanged(listenerOrObserver) {
const listener = this._parseListener(listenerOrObserver);
const subscription = this.emitter.addListener(this.eventNameForApp('onUserChanged'), listener);
if (this._authResult) {
Promise.resolve().then(() => {
listener(this._user || null);
});
}
return () => {
subscription.remove();
};
}
signOut() {
return this.native.signOut().then(() => {
this._setUser();
});
}
signInAnonymously() {
return this.native
.signInAnonymously()
.then(userCredential => this._setUserCredential(userCredential));
}
signInWithPhoneNumber(phoneNumber, forceResend) {
if (isAndroid) {
return this.native
.signInWithPhoneNumber(phoneNumber, forceResend || false)
.then(result => new ConfirmationResult(this, result.verificationId));
}
return this.native
.signInWithPhoneNumber(phoneNumber)
.then(result => new ConfirmationResult(this, result.verificationId));
}
verifyPhoneNumber(phoneNumber, autoVerifyTimeoutOrForceResend, forceResend) {
let _forceResend = forceResend;
let _autoVerifyTimeout = 60;
if (isBoolean(autoVerifyTimeoutOrForceResend)) {
_forceResend = autoVerifyTimeoutOrForceResend;
} else {
_autoVerifyTimeout = autoVerifyTimeoutOrForceResend;
}
return new PhoneAuthListener(this, phoneNumber, _autoVerifyTimeout, _forceResend);
}
createUserWithEmailAndPassword(email, password) {
return this.native
.createUserWithEmailAndPassword(email, password)
.then(userCredential => this._setUserCredential(userCredential));
}
signInWithEmailAndPassword(email, password) {
return this.native
.signInWithEmailAndPassword(email, password)
.then(userCredential => this._setUserCredential(userCredential));
}
signInWithCustomToken(customToken) {
return this.native
.signInWithCustomToken(customToken)
.then(userCredential => this._setUserCredential(userCredential));
}
signInWithCredential(credential) {
return this.native
.signInWithCredential(credential.providerId, credential.token, credential.secret)
.then(userCredential => this._setUserCredential(userCredential));
}
sendPasswordResetEmail(email, actionCodeSettings = null) {
return this.native.sendPasswordResetEmail(email, actionCodeSettings);
}
sendSignInLinkToEmail(email, actionCodeSettings = {}) {
return this.native.sendSignInLinkToEmail(email, actionCodeSettings);
}
isSignInWithEmailLink(emailLink) {
return (
typeof emailLink === 'string' &&
(emailLink.includes('mode=signIn') || emailLink.includes('mode%3DsignIn')) &&
(emailLink.includes('oobCode=') || emailLink.includes('oobCode%3D'))
);
}
signInWithEmailLink(email, emailLink) {
return this.native
.signInWithEmailLink(email, emailLink)
.then(userCredential => this._setUserCredential(userCredential));
}
confirmPasswordReset(code, newPassword) {
return this.native.confirmPasswordReset(code, newPassword);
}
applyActionCode(code) {
return this.native.applyActionCode(code).then(user => {
this._setUser(user);
});
}
checkActionCode(code) {
return this.native.checkActionCode(code);
}
fetchSignInMethodsForEmail(email) {
return this.native.fetchSignInMethodsForEmail(email);
}
verifyPasswordResetCode(code) {
return this.native.verifyPasswordResetCode(code);
}
useUserAccessGroup(userAccessGroup) {
if (isAndroid) {
return Promise.resolve();
}
return this.native.useUserAccessGroup(userAccessGroup);
}
getRedirectResult() {
throw new Error(
'firebase.auth().getRedirectResult() is unsupported by the native Firebase SDKs.',
);
}
setPersistence() {
throw new Error('firebase.auth().setPersistence() is unsupported by the native Firebase SDKs.');
}
signInWithPopup() {
throw new Error(
'firebase.auth().signInWithPopup() is unsupported by the native Firebase SDKs.',
);
}
signInWithRedirect() {
throw new Error(
'firebase.auth().signInWithRedirect() is unsupported by the native Firebase SDKs.',
);
}
// firebase issue - https://github.com/invertase/react-native-firebase/pull/655#issuecomment-349904680
useDeviceLanguage() {
throw new Error(
'firebase.auth().useDeviceLanguage() is unsupported by the native Firebase SDKs.',
);
}
useEmulator(url) {
if (!url || !isString(url) || !isValidUrl(url)) {
throw new Error('firebase.auth().useEmulator() takes a non-empty string URL');
}
let _url = url;
if (isAndroid && _url) {
if (_url.startsWith('http://localhost')) {
_url = _url.replace('http://localhost', 'http://10.0.2.2');
// eslint-disable-next-line no-console
console.log(
'Mapping auth host "localhost" to "10.0.2.2" for android emulators. Use real IP on real devices.',
);
}
if (_url.startsWith('http://127.0.0.1')) {
_url = _url.replace('http://127.0.0.1', 'http://10.0.2.2');
// eslint-disable-next-line no-console
console.log(
'Mapping auth host "127.0.0.1" to "10.0.2.2" for android emulators. Use real IP on real devices.',
);
}
}
// Native calls take the host and port split out
const hostPortRegex = /^http:\/\/([\w\d.]+):(\d+)$/;
const urlMatches = _url.match(hostPortRegex);
if (!urlMatches) {
throw new Error('firebase.auth().useEmulator() unable to parse host and port from URL');
}
const host = urlMatches[1];
const port = parseInt(urlMatches[2], 10);
this.native.useEmulator(host, port);
return [host, port]; // undocumented return, useful for unit testing
}
}
// import { SDK_VERSION } from '@react-native-firebase/auth';
export const SDK_VERSION = version;
// import auth from '@react-native-firebase/auth';
// auth().X(...);
export default createModuleNamespace({
statics,
version,
namespace,
nativeModuleName,
nativeEvents: ['auth_state_changed', 'auth_id_token_changed', 'phone_auth_state_changed'],
hasMultiAppSupport: true,
hasCustomUrlOrRegionSupport: false,
ModuleClass: FirebaseAuthModule,
});
// import auth, { firebase } from '@react-native-firebase/auth';
// auth().X(...);
// firebase.auth().X(...);
export const firebase = getFirebaseRoot();