-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
index.js
813 lines (755 loc) · 23.8 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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
import Onyx from 'react-native-onyx';
import _ from 'underscore';
import lodashGet from 'lodash/get';
import {Linking} from 'react-native';
import clearCache from './clearCache';
import ONYXKEYS from '../../../ONYXKEYS';
import redirectToSignIn from '../SignInRedirect';
import CONFIG from '../../../CONFIG';
import Log from '../../Log';
import Timing from '../Timing';
import CONST from '../../../CONST';
import Timers from '../../Timers';
import * as Pusher from '../../Pusher/pusher';
import * as Authentication from '../../Authentication';
import * as Welcome from '../Welcome';
import * as API from '../../API';
import * as NetworkStore from '../../Network/NetworkStore';
import Navigation from '../../Navigation/Navigation';
import * as Device from '../Device';
import ROUTES from '../../../ROUTES';
import * as ErrorUtils from '../../ErrorUtils';
import * as ReportUtils from '../../ReportUtils';
import {hideContextMenu} from '../../../pages/home/report/ContextMenu/ReportActionContextMenu';
let sessionAuthTokenType = '';
let sessionAuthToken = null;
let authPromiseResolver = null;
Onyx.connect({
key: ONYXKEYS.SESSION,
callback: (session) => {
sessionAuthTokenType = lodashGet(session, 'authTokenType');
sessionAuthToken = lodashGet(session, 'authToken');
if (sessionAuthToken && authPromiseResolver) {
authPromiseResolver(true);
authPromiseResolver = null;
}
},
});
let credentials = {};
Onyx.connect({
key: ONYXKEYS.CREDENTIALS,
callback: (val) => (credentials = val || {}),
});
/**
* Clears the Onyx store and redirects user to the sign in page
*/
function signOut() {
Log.info('Flushing logs before signing out', true, {}, true);
API.write('LogOut', {
// Send current authToken because we will immediately clear it once triggering this command
authToken: NetworkStore.getAuthToken(),
partnerUserID: lodashGet(credentials, 'autoGeneratedLogin', ''),
partnerName: CONFIG.EXPENSIFY.PARTNER_NAME,
partnerPassword: CONFIG.EXPENSIFY.PARTNER_PASSWORD,
shouldRetry: false,
});
clearCache().then(() => {
Log.info('Cleared all chache data', true, {}, true);
});
Timing.clearData();
}
/**
* Checks if the account is an anonymous account.
*
* @return {boolean}
*/
function isAnonymousUser() {
return sessionAuthTokenType === 'anonymousAccount';
}
function signOutAndRedirectToSignIn() {
Log.info('Redirecting to Sign In because signOut() was called');
hideContextMenu(false);
if (!isAnonymousUser()) {
signOut();
redirectToSignIn();
} else {
Navigation.navigate(ROUTES.SIGN_IN_MODAL);
Linking.getInitialURL().then((url) => {
const reportID = ReportUtils.getReportIDFromLink(url);
if (reportID) {
Onyx.merge(ONYXKEYS.LAST_OPENED_PUBLIC_ROOM_ID, reportID);
}
});
}
}
/**
* @param {Function} callback The callback to execute if the action is allowed
* @param {Boolean} isAnonymousAction The action is allowed for anonymous or not
* @returns {Function} same callback if the action is allowed, otherwise a function that signs out and redirects to sign in
*/
function checkIfActionIsAllowed(callback, isAnonymousAction = false) {
if (isAnonymousUser() && !isAnonymousAction) {
return () => signOutAndRedirectToSignIn();
}
return callback;
}
/**
* Resend the validation link to the user that is validating their account
*
* @param {String} [login]
*/
function resendValidationLink(login = credentials.login) {
const optimisticData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
isLoading: true,
errors: null,
message: null,
loadingForm: CONST.FORMS.RESEND_VALIDATION_FORM,
},
},
];
const successData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
isLoading: false,
message: 'resendValidationForm.linkHasBeenResent',
loadingForm: null,
},
},
];
const failureData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
isLoading: false,
message: null,
loadingForm: null,
},
},
];
API.write('RequestAccountValidationLink', {email: login}, {optimisticData, successData, failureData});
}
/**
* Request a new validate / magic code for user to sign in via passwordless flow
*
* @param {String} [login]
*/
function resendValidateCode(login = credentials.login) {
const optimisticData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
errors: null,
loadingForm: CONST.FORMS.RESEND_VALIDATE_CODE_FORM,
},
},
];
const successData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
loadingForm: null,
},
},
];
const failureData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
loadingForm: null,
},
},
];
API.write('RequestNewValidateCode', {email: login}, {optimisticData, successData, failureData});
}
/**
/**
* Constructs the state object for the BeginSignIn && BeginAppleSignIn API calls.
* @returns {Object}
*/
function signInAttemptState() {
return {
optimisticData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
...CONST.DEFAULT_ACCOUNT_DATA,
isLoading: true,
message: null,
loadingForm: CONST.FORMS.LOGIN_FORM,
},
},
],
successData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
isLoading: false,
loadingForm: null,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.CREDENTIALS,
value: {
validateCode: null,
},
},
],
failureData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
isLoading: false,
loadingForm: null,
// eslint-disable-next-line rulesdir/prefer-localization
errors: ErrorUtils.getMicroSecondOnyxError('loginForm.cannotGetAccountDetails'),
},
},
],
};
}
/**
* Checks the API to see if an account exists for the given login.
*
* @param {String} login
*/
function beginSignIn(login) {
const {optimisticData, successData, failureData} = signInAttemptState();
API.read('BeginSignIn', {email: login}, {optimisticData, successData, failureData});
}
/**
* Given an idToken from Sign in with Apple, checks the API to see if an account
* exists for that email address and signs the user in if so.
*
* @param {String} idToken
*/
function beginAppleSignIn(idToken) {
const {optimisticData, successData, failureData} = signInAttemptState();
API.write('SignInWithApple', {idToken}, {optimisticData, successData, failureData});
}
/**
* Shows Google sign-in process, and if an auth token is successfully obtained,
* passes the token on to the Expensify API to sign in with
*
* @param {String} token
*/
function beginGoogleSignIn(token) {
const {optimisticData, successData, failureData} = signInAttemptState();
API.write('SignInWithGoogle', {token}, {optimisticData, successData, failureData});
}
/**
* Will create a temporary login for the user in the passed authenticate response which is used when
* re-authenticating after an authToken expires.
*
* @param {String} email
* @param {String} authToken
*/
function signInWithShortLivedAuthToken(email, authToken) {
const optimisticData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
...CONST.DEFAULT_ACCOUNT_DATA,
isLoading: true,
},
},
];
const successData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
isLoading: false,
},
},
];
const failureData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
isLoading: false,
},
},
];
// If the user is signing in with a different account from the current app, should not pass the auto-generated login as it may be tied to the old account.
// scene 1: the user is transitioning to newDot from a different account on oldDot.
// scene 2: the user is transitioning to desktop app from a different account on web app.
const oldPartnerUserID = credentials.login === email ? credentials.autoGeneratedLogin : '';
API.read('SignInWithShortLivedAuthToken', {authToken, oldPartnerUserID, skipReauthentication: true}, {optimisticData, successData, failureData});
}
/**
* Sign the user into the application. This will first authenticate their account
* then it will create a temporary login for them which is used when re-authenticating
* after an authToken expires.
*
* @param {String} validateCode 6 digit code required for login
* @param {String} [twoFactorAuthCode]
* @param {String} [preferredLocale] Indicates which language to use when the user lands in the app
*/
function signIn(validateCode, twoFactorAuthCode, preferredLocale = CONST.LOCALES.DEFAULT) {
const optimisticData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
...CONST.DEFAULT_ACCOUNT_DATA,
isLoading: true,
loadingForm: twoFactorAuthCode ? CONST.FORMS.VALIDATE_TFA_CODE_FORM : CONST.FORMS.VALIDATE_CODE_FORM,
},
},
];
const successData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
isLoading: false,
loadingForm: null,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.CREDENTIALS,
value: {
validateCode,
},
},
];
const failureData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
isLoading: false,
loadingForm: null,
},
},
];
const params = {
twoFactorAuthCode,
email: credentials.login,
preferredLocale,
};
// Conditionally pass a password or validateCode to command since we temporarily allow both flows
if (validateCode || twoFactorAuthCode) {
params.validateCode = validateCode || credentials.validateCode;
}
Device.getDeviceInfoWithID().then((deviceInfo) => {
API.write('SigninUser', {...params, deviceInfo}, {optimisticData, successData, failureData});
});
}
function signInWithValidateCode(accountID, code, preferredLocale = CONST.LOCALES.DEFAULT, twoFactorAuthCode = '') {
// If this is called from the 2fa step, get the validateCode directly from onyx
// instead of the one passed from the component state because the state is changing when this method is called.
const validateCode = twoFactorAuthCode ? credentials.validateCode : code;
const optimisticData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
...CONST.DEFAULT_ACCOUNT_DATA,
isLoading: true,
loadingForm: twoFactorAuthCode ? CONST.FORMS.VALIDATE_TFA_CODE_FORM : CONST.FORMS.VALIDATE_CODE_FORM,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.SESSION,
value: {autoAuthState: CONST.AUTO_AUTH_STATE.SIGNING_IN},
},
];
const successData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
isLoading: false,
loadingForm: null,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.CREDENTIALS,
value: {
accountID,
validateCode,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.SESSION,
value: {autoAuthState: CONST.AUTO_AUTH_STATE.JUST_SIGNED_IN},
},
];
const failureData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
isLoading: false,
loadingForm: null,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.SESSION,
value: {autoAuthState: CONST.AUTO_AUTH_STATE.FAILED},
},
];
Device.getDeviceInfoWithID().then((deviceInfo) => {
API.write(
'SigninUserWithLink',
{
accountID,
validateCode,
twoFactorAuthCode,
preferredLocale,
deviceInfo,
},
{optimisticData, successData, failureData},
);
});
}
function signInWithValidateCodeAndNavigate(accountID, validateCode, preferredLocale = CONST.LOCALES.DEFAULT, twoFactorAuthCode = '') {
signInWithValidateCode(accountID, validateCode, preferredLocale, twoFactorAuthCode);
Navigation.navigate(ROUTES.HOME);
}
/**
* Initializes the state of the automatic authentication when the user clicks on a magic link.
*
* This method is called in componentDidMount event of the lifecycle.
* When the user gets authenticated, the component is unmounted and then remounted
* when AppNavigator switches from PublicScreens to AuthScreens.
* That's the reason why autoAuthState initialization is skipped while the last state is SIGNING_IN.
*
* @param {string} cachedAutoAuthState
*/
function initAutoAuthState(cachedAutoAuthState) {
Onyx.merge(ONYXKEYS.SESSION, {
autoAuthState: _.contains([CONST.AUTO_AUTH_STATE.SIGNING_IN, CONST.AUTO_AUTH_STATE.JUST_SIGNED_IN], cachedAutoAuthState)
? CONST.AUTO_AUTH_STATE.JUST_SIGNED_IN
: CONST.AUTO_AUTH_STATE.NOT_STARTED,
});
}
function invalidateCredentials() {
Onyx.merge(ONYXKEYS.CREDENTIALS, {autoGeneratedLogin: '', autoGeneratedPassword: ''});
}
function invalidateAuthToken() {
NetworkStore.setAuthToken('pizza');
Onyx.merge(ONYXKEYS.SESSION, {authToken: 'pizza'});
}
/**
* Sets the SupportToken
* @param {String} supportToken
* @param {String} email
* @param {Number} accountID
*/
function setSupportAuthToken(supportToken, email, accountID) {
if (supportToken) {
Onyx.merge(ONYXKEYS.SESSION, {
authToken: '1',
supportAuthToken: supportToken,
email,
accountID,
});
} else {
Onyx.set(ONYXKEYS.SESSION, {});
}
NetworkStore.setSupportAuthToken(supportToken);
}
/**
* Clear the credentials and partial sign in session so the user can taken back to first Login step
*/
function clearSignInData() {
Onyx.multiSet({
[ONYXKEYS.ACCOUNT]: null,
[ONYXKEYS.CREDENTIALS]: {},
});
}
/**
* Put any logic that needs to run when we are signed out here. This can be triggered when the current tab or another tab signs out.
*/
function cleanupSession() {
Pusher.disconnect();
Timers.clearAll();
Welcome.resetReadyCheck();
}
function clearAccountMessages() {
Onyx.merge(ONYXKEYS.ACCOUNT, {
success: '',
errors: null,
message: null,
isLoading: false,
});
}
// It's necessary to throttle requests to reauthenticate since calling this multiple times will cause Pusher to
// reconnect each time when we only need to reconnect once. This way, if an authToken is expired and we try to
// subscribe to a bunch of channels at once we will only reauthenticate and force reconnect Pusher once.
const reauthenticatePusher = _.throttle(
() => {
Log.info('[Pusher] Re-authenticating and then reconnecting');
Authentication.reauthenticate('AuthenticatePusher')
.then(Pusher.reconnect)
.catch(() => {
console.debug('[PusherConnectionManager]', 'Unable to re-authenticate Pusher because we are offline.');
});
},
5000,
{trailing: false},
);
/**
* @param {String} socketID
* @param {String} channelName
* @param {Function} callback
*/
function authenticatePusher(socketID, channelName, callback) {
Log.info('[PusherAuthorizer] Attempting to authorize Pusher', false, {channelName});
// We use makeRequestWithSideEffects here because we need to authorize to Pusher (an external service) each time a user connects to any channel.
// eslint-disable-next-line rulesdir/no-api-side-effects-method
API.makeRequestWithSideEffects('AuthenticatePusher', {
socket_id: socketID,
channel_name: channelName,
shouldRetry: false,
forceNetworkRequest: true,
})
.then((response) => {
if (response.jsonCode === CONST.JSON_CODE.NOT_AUTHENTICATED) {
Log.hmmm('[PusherAuthorizer] Unable to authenticate Pusher because authToken is expired');
callback(new Error('Pusher failed to authenticate because authToken is expired'), {auth: ''});
// Attempt to refresh the authToken then reconnect to Pusher
reauthenticatePusher();
return;
}
if (response.jsonCode !== CONST.JSON_CODE.SUCCESS) {
Log.hmmm('[PusherAuthorizer] Unable to authenticate Pusher for reason other than expired session');
callback(new Error(`Pusher failed to authenticate because code: ${response.jsonCode} message: ${response.message}`), {auth: ''});
return;
}
Log.info('[PusherAuthorizer] Pusher authenticated successfully', false, {channelName});
callback(null, response);
})
.catch((error) => {
Log.hmmm('[PusherAuthorizer] Unhandled error: ', {channelName, error});
callback(new Error('AuthenticatePusher request failed'), {auth: ''});
});
}
/**
* Request a new validation link / magic code to unlink an unvalidated secondary login from a primary login
*/
function requestUnlinkValidationLink() {
const optimisticData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
isLoading: true,
errors: null,
message: null,
loadingForm: CONST.FORMS.UNLINK_LOGIN_FORM,
},
},
];
const successData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
isLoading: false,
message: 'unlinkLoginForm.linkSent',
loadingForm: null,
},
},
];
const failureData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
isLoading: false,
loadingForm: null,
},
},
];
API.write('RequestUnlinkValidationLink', {email: credentials.login}, {optimisticData, successData, failureData});
}
function unlinkLogin(accountID, validateCode) {
const optimisticData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
...CONST.DEFAULT_ACCOUNT_DATA,
isLoading: true,
},
},
];
const successData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
isLoading: false,
message: 'unlinkLoginForm.succesfullyUnlinkedLogin',
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.CREDENTIALS,
value: {
login: '',
},
},
];
const failureData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
isLoading: false,
},
},
];
API.write(
'UnlinkLogin',
{
accountID,
validateCode,
},
{
optimisticData,
successData,
failureData,
},
);
}
/**
* Toggles two-factor authentication based on the `enable` parameter
*
* @param {Boolean} enable
*/
function toggleTwoFactorAuth(enable) {
const optimisticData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
isLoading: true,
},
},
];
const successData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
isLoading: false,
},
},
];
const failureData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
isLoading: false,
},
},
];
API.write(enable ? 'EnableTwoFactorAuth' : 'DisableTwoFactorAuth', {}, {optimisticData, successData, failureData});
}
function validateTwoFactorAuth(twoFactorAuthCode) {
const optimisticData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
isLoading: true,
},
},
];
const successData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
isLoading: false,
},
},
];
const failureData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.ACCOUNT,
value: {
isLoading: false,
},
},
];
API.write('TwoFactorAuth_Validate', {twoFactorAuthCode}, {optimisticData, successData, failureData});
}
/**
* Waits for a user to sign in.
*
* If the user is already signed in (`authToken` is truthy), the promise resolves immediately.
* Otherwise, the promise will resolve when the `authToken` in `ONYXKEYS.SESSION` becomes truthy via the Onyx callback.
* The promise will not reject on failed login attempt.
*
* @returns {Promise<boolean>} A promise that resolves to `true` once the user is signed in.
* @example
* waitForUserSignIn().then(() => {
* console.log('User is signed in!');
* });
*/
function waitForUserSignIn() {
return new Promise((resolve) => {
if (sessionAuthToken) {
resolve(true);
} else {
authPromiseResolver = resolve;
}
});
}
export {
beginSignIn,
beginAppleSignIn,
beginGoogleSignIn,
setSupportAuthToken,
checkIfActionIsAllowed,
signIn,
signInWithValidateCode,
signInWithValidateCodeAndNavigate,
initAutoAuthState,
signInWithShortLivedAuthToken,
cleanupSession,
signOut,
signOutAndRedirectToSignIn,
resendValidationLink,
resendValidateCode,
requestUnlinkValidationLink,
unlinkLogin,
clearSignInData,
clearAccountMessages,
authenticatePusher,
reauthenticatePusher,
invalidateCredentials,
invalidateAuthToken,
isAnonymousUser,
toggleTwoFactorAuth,
validateTwoFactorAuth,
waitForUserSignIn,
};