@@ -6,7 +6,6 @@ import type {OnyxEntry, OnyxUpdate} from 'react-native-onyx';
6
6
import Onyx from 'react-native-onyx' ;
7
7
import type { ValueOf } from 'type-fest' ;
8
8
import * as PersistedRequests from '@libs/actions/PersistedRequests' ;
9
- import { resolveDuplicationConflictAction } from '@libs/actions/RequestConflictUtils' ;
10
9
import * as API from '@libs/API' ;
11
10
import type {
12
11
AuthenticatePusherParams ,
@@ -25,6 +24,7 @@ import type {
25
24
} from '@libs/API/parameters' ;
26
25
import type SignInUserParams from '@libs/API/parameters/SignInUserParams' ;
27
26
import { READ_COMMANDS , SIDE_EFFECT_REQUEST_COMMANDS , WRITE_COMMANDS } from '@libs/API/types' ;
27
+ import asyncOpenURL from '@libs/asyncOpenURL' ;
28
28
import * as Authentication from '@libs/Authentication' ;
29
29
import * as ErrorUtils from '@libs/ErrorUtils' ;
30
30
import Fullstory from '@libs/Fullstory' ;
@@ -56,6 +56,7 @@ import type {HybridAppRoute, Route} from '@src/ROUTES';
56
56
import ROUTES from '@src/ROUTES' ;
57
57
import SCREENS from '@src/SCREENS' ;
58
58
import type Credentials from '@src/types/onyx/Credentials' ;
59
+ import type Response from '@src/types/onyx/Response' ;
59
60
import type Session from '@src/types/onyx/Session' ;
60
61
import type { AutoAuthState } from '@src/types/onyx/Session' ;
61
62
import clearCache from './clearCache' ;
@@ -178,7 +179,7 @@ function signInWithSupportAuthToken(authToken: string) {
178
179
/**
179
180
* Clears the Onyx store and redirects user to the sign in page
180
181
*/
181
- function signOut ( ) {
182
+ function signOut ( ) : Promise < void | Response > {
182
183
Log . info ( 'Flushing logs before signing out' , true , { } , true ) ;
183
184
const params = {
184
185
// Send current authToken because we will immediately clear it once triggering this command
@@ -189,14 +190,8 @@ function signOut() {
189
190
shouldRetry : false ,
190
191
} ;
191
192
192
- API . write (
193
- WRITE_COMMANDS . LOG_OUT ,
194
- params ,
195
- { } ,
196
- {
197
- checkAndFixConflictingRequest : ( persistedRequests ) => resolveDuplicationConflictAction ( persistedRequests , ( request ) => request . command === WRITE_COMMANDS . LOG_OUT ) ,
198
- } ,
199
- ) ;
193
+ // eslint-disable-next-line rulesdir/no-api-side-effects-method
194
+ return API . makeRequestWithSideEffects ( SIDE_EFFECT_REQUEST_COMMANDS . LOG_OUT , params , { } ) ;
200
195
}
201
196
202
197
/**
@@ -228,71 +223,81 @@ function isExpiredSession(sessionCreationDate: number): boolean {
228
223
function signOutAndRedirectToSignIn ( shouldResetToHome ?: boolean , shouldStashSession ?: boolean , killHybridApp = true ) {
229
224
Log . info ( 'Redirecting to Sign In because signOut() was called' ) ;
230
225
hideContextMenu ( false ) ;
231
- if ( ! isAnonymousUser ( ) ) {
232
- // In the HybridApp, we want the Old Dot to handle the sign out process
233
- if ( NativeModules . HybridAppModule && killHybridApp ) {
234
- NativeModules . HybridAppModule . closeReactNativeApp ( true , false ) ;
235
- return ;
236
- }
237
- // We'll only call signOut if we're not stashing the session and this is not a supportal session,
238
- // otherwise we'll call the API to invalidate the autogenerated credentials used for infinite
239
- // session.
240
- const isSupportal = isSupportAuthToken ( ) ;
241
- if ( ! isSupportal && ! shouldStashSession ) {
242
- signOut ( ) ;
243
- }
244
226
245
- // The function redirectToSignIn will clear the whole storage, so let's create our onyx params
246
- // updates for the credentials before we call it
247
- let onyxSetParams = { } ;
248
-
249
- // If we are not currently using a support token, and we received stashSession as true, we need to
250
- // store the credentials so the user doesn't need to login again after they finish their supportal
251
- // action. This needs to computed before we call `redirectToSignIn`
252
- if ( ! isSupportal && shouldStashSession ) {
253
- onyxSetParams = {
254
- [ ONYXKEYS . STASHED_CREDENTIALS ] : credentials ,
255
- [ ONYXKEYS . STASHED_SESSION ] : session ,
256
- } ;
257
- }
258
- // If this is a supportal token, and we've received the parameters to stashSession as true, and
259
- // we already have a stashedSession, that means we are supportaled, currently supportaling
260
- // into another account and we want to keep the stashed data from the original account.
261
- if ( isSupportal && shouldStashSession && hasStashedSession ( ) ) {
262
- onyxSetParams = {
263
- [ ONYXKEYS . STASHED_CREDENTIALS ] : stashedCredentials ,
264
- [ ONYXKEYS . STASHED_SESSION ] : stashedSession ,
265
- } ;
266
- }
267
- // Now if this is a supportal access, we do not want to stash the current session and we have a
268
- // stashed session, then we need to restore the stashed session instead of completely logging out
269
- if ( isSupportal && ! shouldStashSession && hasStashedSession ( ) ) {
270
- onyxSetParams = {
271
- [ ONYXKEYS . CREDENTIALS ] : stashedCredentials ,
272
- [ ONYXKEYS . SESSION ] : stashedSession ,
273
- } ;
274
- }
275
- if ( isSupportal && ! shouldStashSession && ! hasStashedSession ( ) ) {
276
- Log . info ( 'No stashed session found for supportal access, clearing the session' ) ;
227
+ if ( isAnonymousUser ( ) ) {
228
+ if ( ! Navigation . isActiveRoute ( ROUTES . SIGN_IN_MODAL ) ) {
229
+ if ( shouldResetToHome ) {
230
+ Navigation . resetToHome ( ) ;
231
+ }
232
+ Navigation . navigate ( ROUTES . SIGN_IN_MODAL ) ;
233
+ Linking . getInitialURL ( ) . then ( ( url ) => {
234
+ const reportID = getReportIDFromLink ( url ) ;
235
+ if ( reportID ) {
236
+ Onyx . merge ( ONYXKEYS . LAST_OPENED_PUBLIC_ROOM_ID , reportID ) ;
237
+ }
238
+ } ) ;
277
239
}
278
- redirectToSignIn ( ) . then ( ( ) => {
240
+ return ;
241
+ }
242
+
243
+ // In the HybridApp, we want the Old Dot to handle the sign out process
244
+ if ( NativeModules . HybridAppModule && killHybridApp ) {
245
+ NativeModules . HybridAppModule . closeReactNativeApp ( true , false ) ;
246
+ return ;
247
+ }
248
+ // We'll only call signOut if we're not stashing the session and this is not a supportal session,
249
+ // otherwise we'll call the API to invalidate the autogenerated credentials used for infinite
250
+ // session.
251
+ const isSupportal = isSupportAuthToken ( ) ;
252
+ const signOutPromise : Promise < void | Response > = ! isSupportal && ! shouldStashSession ? signOut ( ) : Promise . resolve ( ) ;
253
+
254
+ // The function redirectToSignIn will clear the whole storage, so let's create our onyx params
255
+ // updates for the credentials before we call it
256
+ let onyxSetParams = { } ;
257
+
258
+ // If we are not currently using a support token, and we received stashSession as true, we need to
259
+ // store the credentials so the user doesn't need to login again after they finish their supportal
260
+ // action. This needs to computed before we call `redirectToSignIn`
261
+ if ( ! isSupportal && shouldStashSession ) {
262
+ onyxSetParams = {
263
+ [ ONYXKEYS . STASHED_CREDENTIALS ] : credentials ,
264
+ [ ONYXKEYS . STASHED_SESSION ] : session ,
265
+ } ;
266
+ }
267
+ // If this is a supportal token, and we've received the parameters to stashSession as true, and
268
+ // we already have a stashedSession, that means we are supportaled, currently supportaling
269
+ // into another account and we want to keep the stashed data from the original account.
270
+ if ( isSupportal && shouldStashSession && hasStashedSession ( ) ) {
271
+ onyxSetParams = {
272
+ [ ONYXKEYS . STASHED_CREDENTIALS ] : stashedCredentials ,
273
+ [ ONYXKEYS . STASHED_SESSION ] : stashedSession ,
274
+ } ;
275
+ }
276
+ // Now if this is a supportal access, we do not want to stash the current session and we have a
277
+ // stashed session, then we need to restore the stashed session instead of completely logging out
278
+ if ( isSupportal && ! shouldStashSession && hasStashedSession ( ) ) {
279
+ onyxSetParams = {
280
+ [ ONYXKEYS . CREDENTIALS ] : stashedCredentials ,
281
+ [ ONYXKEYS . SESSION ] : stashedSession ,
282
+ } ;
283
+ }
284
+ if ( isSupportal && ! shouldStashSession && ! hasStashedSession ( ) ) {
285
+ Log . info ( 'No stashed session found for supportal access, clearing the session' ) ;
286
+ }
287
+
288
+ // Wait for signOut (if called), then redirect and update Onyx.
289
+ signOutPromise
290
+ . then ( ( response ) => {
279
291
Onyx . multiSet ( onyxSetParams ) ;
280
- } ) ;
281
- } else {
282
- if ( Navigation . isActiveRoute ( ROUTES . SIGN_IN_MODAL ) ) {
283
- return ;
284
- }
285
- if ( shouldResetToHome ) {
286
- Navigation . resetToHome ( ) ;
287
- }
288
- Navigation . navigate ( ROUTES . SIGN_IN_MODAL ) ;
289
- Linking . getInitialURL ( ) . then ( ( url ) => {
290
- const reportID = getReportIDFromLink ( url ) ;
291
- if ( reportID ) {
292
- Onyx . merge ( ONYXKEYS . LAST_OPENED_PUBLIC_ROOM_ID , reportID ) ;
292
+
293
+ if ( response ?. hasOldDotAuthCookies ) {
294
+ Log . info ( 'Redirecting to OldDot sign out' ) ;
295
+ asyncOpenURL ( redirectToSignIn ( ) , `${ CONFIG . EXPENSIFY . EXPENSIFY_URL } ${ CONST . OLDDOT_URLS . SIGN_OUT } ` , true , true ) ;
296
+ } else {
297
+ redirectToSignIn ( ) ;
293
298
}
294
- } ) ;
295
- }
299
+ } )
300
+ . catch ( ( error : string ) => Log . warn ( 'Error during sign out process:' , error ) ) ;
296
301
}
297
302
298
303
/**
0 commit comments