Skip to content

Commit

Permalink
fix: retaining local data on log out
Browse files Browse the repository at this point in the history
  • Loading branch information
Billie He committed Oct 28, 2024
1 parent e8d4584 commit 04ee404
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/features/auth/model/cleanupData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ import { clearEntityRecordStorages } from '@features/auth/lib/clearEntityRecordS
import { clearUploadQueueStorage } from '@features/auth/lib/clearUploadQueueStorage';
import { getDefaultLogger } from '@shared/lib/services/loggerInstance';

// Clear entity record and progression data.
// This function should only be called upon successful login, and that the
// just logged-in user's ID is not the same as the previously logged-in user's
// ID.
// This function should not be called upon log out. This way, previously stored
// entity record and progression data can be preserved for the same user over
// multiple sessions. This is an intended behaviour.
export async function cleanupData() {
getDefaultLogger().info(
'[cleanupData] Processing cleanup upon another user login',
Expand Down
5 changes: 5 additions & 0 deletions src/features/login/ui/LoginForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ export const LoginForm: FC<Props> = props => {
password: variables.password,
};

// If the previously logged-in user's ID is not the same as the just
// logged-in user's ID, then clear previously stored data.
// We are able to make this determination because previously stored data
// is not cleared on log out (only session info is cleared on log out),
// and this is an intended behaviour.
if (userParams.userId !== userId) {
await cleanupData();
dispatch(cleanUpAction());
Expand Down
3 changes: 0 additions & 3 deletions src/features/logout/model/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { getDefaultLogger } from '@app/shared/lib/services/loggerInstance';
import { useTCPSocket } from '@app/shared/lib/tcp/useTCPSocket';
import { isAppOnline } from '@app/shared/lib/utils/networkHelpers';
import { hasPendingMutations } from '@app/shared/lib/utils/reactQueryHelpers';
import { cleanupData } from '@features/auth/model/cleanupData';

export function useLogout() {
const queryClient = useQueryClient();
Expand Down Expand Up @@ -58,8 +57,6 @@ export function useLogout() {

queryClient.clear();

await cleanupData();

getDefaultSessionService().clearSession();

closeActiveTCPConnection();
Expand Down

0 comments on commit 04ee404

Please sign in to comment.