Skip to content

Commit

Permalink
Merge pull request #4259 from tloncorp/lb/clear-kv-on-logout
Browse files Browse the repository at this point in the history
key value: add option to keep or clear storage items after logout
  • Loading branch information
latter-bolden authored Dec 6, 2024
2 parents 1ea0367 + a3e7727 commit 685f89d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/app/hooks/useHandleLogout.native.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createDevLogger } from '@tloncorp/shared';
import * as api from '@tloncorp/shared/api';
import { finishingSelfHostedLogin } from '@tloncorp/shared/db';
import { clearNonPersistentStorageItems } from '@tloncorp/shared/db';
import * as store from '@tloncorp/shared/store';
import { useCallback } from 'react';

Expand Down Expand Up @@ -28,7 +28,7 @@ export function useHandleLogout({ resetDb }: { resetDb: () => void }) {
clearDeepLink();
clearSplashDismissed();
clearTelemetry();
finishingSelfHostedLogin.resetValue();
clearNonPersistentStorageItems();
if (!resetDb) {
logger.trackError('could not reset db on logout');
return;
Expand Down
3 changes: 2 additions & 1 deletion packages/app/hooks/useHandleLogout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// which isn't made for web.
import { createDevLogger } from '@tloncorp/shared';
import * as api from '@tloncorp/shared/api';
import { clearNonPersistentStorageItems } from '@tloncorp/shared/db';
import * as store from '@tloncorp/shared/store';
import { useCallback } from 'react';

Expand Down Expand Up @@ -33,7 +34,7 @@ export function useHandleLogout({ resetDb }: { resetDb?: () => void }) {
removeHostingUserId();
removeHostingAuthTracking();
clearSplashDismissed();
// signupContext.clear();
clearNonPersistentStorageItems();
if (!resetDb) {
logger.trackError('could not reset db on logout');
return;
Expand Down
31 changes: 29 additions & 2 deletions packages/shared/src/db/keyValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,16 @@ type StorageItem<T> = {
key: string;
defaultValue: T;
isSecure?: boolean;
persistAfterLogout?: boolean;
serialize?: (value: T) => string;
deserialize?: (value: string) => T;
};

const createStorageItem = <T>(config: StorageItem<T>) => {
const createStorageItemImpl = <T>(config: StorageItem<T>) => {
const {
key,
defaultValue,
persistAfterLogout = false,
serialize = JSON.stringify,
deserialize = JSON.parse,
} = config;
Expand Down Expand Up @@ -248,7 +250,30 @@ const createStorageItem = <T>(config: StorageItem<T>) => {
};
}

return { getValue, setValue, resetValue, useValue, useStorageItem };
return {
getValue,
setValue,
resetValue,
useValue,
useStorageItem,
__persistAfterLogout: persistAfterLogout,
};
};

const storageItems: Array<ReturnType<typeof createStorageItemImpl<any>>> = [];
export const clearNonPersistentStorageItems = async (): Promise<void> => {
const clearPromises = storageItems
.filter((item) => !item.__persistAfterLogout)
.map((item) => item.resetValue());

await Promise.all(clearPromises);
logger.log('Cleared all non-persistent storage items');
};

const createStorageItem = <T>(config: StorageItem<T>) => {
const storageItem = createStorageItemImpl(config);
storageItems.push(storageItem);
return storageItem;
};

export const signupData = createStorageItem<SignupParams>({
Expand All @@ -263,11 +288,13 @@ export const signupData = createStorageItem<SignupParams>({
export const lastAppVersion = createStorageItem<string | null>({
key: 'lastAppVersion',
defaultValue: null,
persistAfterLogout: true,
});

export const didSignUp = createStorageItem<boolean>({
key: 'didSignUp',
defaultValue: false,
persistAfterLogout: true,
});

export const didInitializeTelemetry = createStorageItem<boolean>({
Expand Down

0 comments on commit 685f89d

Please sign in to comment.