Skip to content

Commit

Permalink
Merge pull request #32936 from bernhardoj/fix/32733-close-account-sig…
Browse files Browse the repository at this point in the history
…n-out-even-if-fail

Fix fail close account log the user out instead of showing the error message
  • Loading branch information
Gonals authored Dec 14, 2023
2 parents 78ad3f9 + 86497f3 commit 82b8d8c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 37 deletions.
33 changes: 33 additions & 0 deletions src/libs/actions/Session/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@ import {ChannelAuthorizationCallback} from 'pusher-js/with-encryption';
import {Linking} from 'react-native';
import Onyx, {OnyxUpdate} from 'react-native-onyx';
import {ValueOf} from 'type-fest';
import * as PersistedRequests from '@libs/actions/PersistedRequests';
import * as API from '@libs/API';
import * as Authentication from '@libs/Authentication';
import * as ErrorUtils from '@libs/ErrorUtils';
import HttpUtils from '@libs/HttpUtils';
import Log from '@libs/Log';
import Navigation from '@libs/Navigation/Navigation';
import navigationRef from '@libs/Navigation/navigationRef';
import * as MainQueue from '@libs/Network/MainQueue';
import * as NetworkStore from '@libs/Network/NetworkStore';
import NetworkConnection from '@libs/NetworkConnection';
import * as Pusher from '@libs/Pusher/pusher';
import * as ReportUtils from '@libs/ReportUtils';
import * as SessionUtils from '@libs/SessionUtils';
import Timers from '@libs/Timers';
import {hideContextMenu} from '@pages/home/report/ContextMenu/ReportActionContextMenu';
import * as Device from '@userActions/Device';
Expand All @@ -23,6 +29,7 @@ import CONFIG from '@src/CONFIG';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import SCREENS from '@src/SCREENS';
import Credentials from '@src/types/onyx/Credentials';
import {AutoAuthState} from '@src/types/onyx/Session';
import clearCache from './clearCache';
Expand Down Expand Up @@ -583,14 +590,40 @@ function clearSignInData() {
});
}

/**
* Reset all current params of the Home route
*/
function resetHomeRouteParams() {
Navigation.isNavigationReady().then(() => {
const routes = navigationRef.current?.getState().routes;
const homeRoute = routes?.find((route) => route.name === SCREENS.HOME);

const emptyParams: Record<string, undefined> = {};
Object.keys(homeRoute?.params ?? {}).forEach((paramKey) => {
emptyParams[paramKey] = undefined;
});

Navigation.setParams(emptyParams, homeRoute?.key ?? '');
Onyx.set(ONYXKEYS.IS_CHECKING_PUBLIC_ROOM, false);
});
}

/**
* 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.
* - Cancels pending network calls - any lingering requests are discarded to prevent unwanted storage writes
* - Clears all current params of the Home route - the login page URL should not contain any parameter
*/
function cleanupSession() {
Pusher.disconnect();
Timers.clearAll();
Welcome.resetReadyCheck();
PriorityMode.resetHasReadRequiredDataFromStorage();
MainQueue.clear();
HttpUtils.cancelPendingRequests();
PersistedRequests.clear();
NetworkConnection.clearReconnectionCallbacks();
SessionUtils.resetDidUserLogInDuringSession();
resetHomeRouteParams();
}

function clearAccountMessages() {
Expand Down
34 changes: 0 additions & 34 deletions src/libs/actions/SignInRedirect.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import Onyx from 'react-native-onyx';
import * as ErrorUtils from '@libs/ErrorUtils';
import HttpUtils from '@libs/HttpUtils';
import Navigation from '@libs/Navigation/Navigation';
import navigationRef from '@libs/Navigation/navigationRef';
import * as MainQueue from '@libs/Network/MainQueue';
import NetworkConnection from '@libs/NetworkConnection';
import * as SessionUtils from '@libs/SessionUtils';
import ONYXKEYS, {OnyxKey} from '@src/ONYXKEYS';
import SCREENS from '@src/SCREENS';
import * as PersistedRequests from './PersistedRequests';

let currentIsOffline: boolean | undefined;
let currentShouldForceOffline: boolean | undefined;
Expand Down Expand Up @@ -45,42 +37,16 @@ function clearStorageAndRedirect(errorMessage?: string) {
});
}

/**
* Reset all current params of the Home route
*/
function resetHomeRouteParams() {
Navigation.isNavigationReady().then(() => {
const routes = navigationRef.current?.getState().routes;
const homeRoute = routes?.find((route) => route.name === SCREENS.HOME);

const emptyParams: Record<string, undefined> = {};
Object.keys(homeRoute?.params ?? {}).forEach((paramKey) => {
emptyParams[paramKey] = undefined;
});

Navigation.setParams(emptyParams, homeRoute?.key ?? '');
Onyx.set(ONYXKEYS.IS_CHECKING_PUBLIC_ROOM, false);
});
}

/**
* Cleanup actions resulting in the user being redirected to the Sign-in page
* - Clears the Onyx store - removing the authToken redirects the user to the Sign-in page
* - Cancels pending network calls - any lingering requests are discarded to prevent unwanted storage writes
* - Clears all current params of the Home route - the login page URL should not contain any parameter
*
* Normally this method would live in Session.js, but that would cause a circular dependency with Network.js.
*
* @param [errorMessage] error message to be displayed on the sign in page
*/
function redirectToSignIn(errorMessage?: string) {
MainQueue.clear();
HttpUtils.cancelPendingRequests();
PersistedRequests.clear();
NetworkConnection.clearReconnectionCallbacks();
clearStorageAndRedirect(errorMessage);
resetHomeRouteParams();
SessionUtils.resetDidUserLogInDuringSession();
}

export default redirectToSignIn;
3 changes: 0 additions & 3 deletions src/libs/actions/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import * as OnyxUpdates from './OnyxUpdates';
import * as PersonalDetails from './PersonalDetails';
import * as Report from './Report';
import * as Session from './Session';
import redirectToSignIn from './SignInRedirect';

let currentUserAccountID = '';
let currentEmail = '';
Expand Down Expand Up @@ -69,8 +68,6 @@ function closeAccount(message) {
],
},
);
// Run cleanup actions to prevent reconnection callbacks from blocking logging in again
redirectToSignIn();
}

/**
Expand Down

0 comments on commit 82b8d8c

Please sign in to comment.