Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove storage helpers #72

Merged
merged 5 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- [Remove storage helpers](https://github.com/multiversx/mx-sdk-dapp-core/pull/72)

## [[0.0.0-alpha.12](https://github.com/multiversx/mx-sdk-dapp-core/pull/56)] - 2025-01-20

- [Added transaction and message cancel signing handle](https://github.com/multiversx/mx-sdk-dapp-core/pull/71)
Expand Down
37 changes: 15 additions & 22 deletions src/core/providers/DappProvider/helpers/logout/logout.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,12 @@
import { safeWindow } from 'constants/window.constants';
import { getAddress } from 'core/methods/account/getAddress';
import {
IProvider,
ProviderTypeEnum
} from 'core/providers/types/providerFactory.types';
import { CrossWindowProvider } from 'lib/sdkWebWalletCrossWindowProvider';
import { storage } from 'storage';
import { localStorageKeys } from 'storage/local';
import { logoutAction } from 'store/actions/sharedActions/sharedActions';

const broadcastLogoutAcrossTabs = (address: string) => {
const storedData = storage.local?.getItem(localStorageKeys.logoutEvent);
const { data } = storedData ? JSON.parse(storedData) : { data: address };

if (address !== data) {
return;
}

storage.local.setItem({
arhtudormorar marked this conversation as resolved.
Show resolved Hide resolved
key: localStorageKeys.logoutEvent,
data: address,
expires: 0
});

storage.local.removeItem(localStorageKeys.logoutEvent);
};

export type LogoutPropsType = {
shouldAttemptReLogin?: boolean;
shouldBroadcastLogoutAcrossTabs?: boolean;
Expand All @@ -39,6 +21,18 @@ interface IProviderLogout {
options?: LogoutPropsType;
}

const broadcastLogoutAcrossTabs = (address: string, localStorage: Storage) => {
const logoutEventKey = `sdk-dapp-core-logout-event-${address}`;
const storedAddress = localStorage.getItem(logoutEventKey);

if (storedAddress && address !== storedAddress) {
return;
}

localStorage.setItem(logoutEventKey, address);
localStorage.removeItem(logoutEventKey);
};

export async function logout({
provider,
options = {
Expand All @@ -48,10 +42,9 @@ export async function logout({
}: IProviderLogout) {
let address = getAddress();

if (options.shouldBroadcastLogoutAcrossTabs) {
broadcastLogoutAcrossTabs(address);
if (options.shouldBroadcastLogoutAcrossTabs && safeWindow.localStorage) {
broadcastLogoutAcrossTabs(address, safeWindow.localStorage);
}

try {
logoutAction();

Expand Down
4 changes: 0 additions & 4 deletions src/storage/index.ts

This file was deleted.

80 changes: 0 additions & 80 deletions src/storage/local.ts

This file was deleted.

57 changes: 0 additions & 57 deletions src/storage/session.ts

This file was deleted.

10 changes: 10 additions & 0 deletions src/store/actions/loginInfo/loginInfoActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,13 @@ export const setIsWalletConnectV2Initialized = (isInitialized: boolean) =>
getStore().setState(({ loginInfo: state }) => {
state.isWalletConnectV2Initialized = isInitialized;
});

export const removeLoginExpiresAt = () =>
getStore().setState(({ loginInfo: state }) => {
state.loginExpiresAt = null;
});

export const addLoginExpiresAt = (expiresAt: number) =>
getStore().setState(({ loginInfo: state }) => {
state.loginExpiresAt = expiresAt;
});
25 changes: 14 additions & 11 deletions src/store/middleware/logoutMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { Address } from '@multiversx/sdk-core/out';
import { WritableDraft } from 'immer';
import { storage } from 'storage';
import { localStorageKeys } from 'storage/local';
import { accountInfoSelector, isLoggedInSelector } from 'store/selectors';
import {
addLoginExpiresAt,
removeLoginExpiresAt
} from 'store/actions/loginInfo/loginInfoActions';
import {
accountInfoSelector,
isLoggedInSelector,
loginExpiresAtSelector
} from 'store/selectors';
import { initialState as initialAccountState } from 'store/slices/account/accountSlice';
import { initialState as initialLoginInfoState } from 'store/slices/loginInfo/loginInfoSlice';
import { initialState as initialToastState } from 'store/slices/toast/toastSlice';
import { initialState as initialTrackedTransactionsState } from 'store/slices/trackedTransactions/trackedTransactionsSlice';
import { getStore } from 'store/store';
import { StoreType } from '../store.types';

export const resetStore = (store: WritableDraft<StoreType>) => {
Expand All @@ -22,20 +29,16 @@ export function getNewLoginExpiresTimestamp() {

export function setLoginExpiresAt(expiresAt: number | null) {
if (expiresAt == null) {
storage.local.removeItem(localStorageKeys.loginExpiresAt);
removeLoginExpiresAt();
return;
}
storage.local.setItem({
key: localStorageKeys.loginExpiresAt,
data: expiresAt,
expires: expiresAt
});
addLoginExpiresAt(expiresAt);
}

export const logoutMiddleware = (state: StoreType) => {
const isLoggedIn = isLoggedInSelector(state);
const loginTimestamp = loginExpiresAtSelector(state);
arhtudormorar marked this conversation as resolved.
Show resolved Hide resolved
const { address, publicKey } = accountInfoSelector(state);
const loginTimestamp = storage.local.getItem(localStorageKeys.loginExpiresAt);

if (address && publicKey !== new Address(address).hex()) {
resetStore(state);
Expand All @@ -56,6 +59,6 @@ export const logoutMiddleware = (state: StoreType) => {
if (isExpired) {
// logout
setLoginExpiresAt(null);
resetStore(state);
getStore().setState(resetStore);
}
};
3 changes: 3 additions & 0 deletions src/store/selectors/loginInfoSelectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ export const providerTypeSelector = ({ loginInfo }: StoreType) =>

export const ledgerLoginSelector = ({ loginInfo }: StoreType) =>
loginInfo.ledgerLogin;

export const loginExpiresAtSelector = ({ loginInfo }: StoreType) =>
loginInfo.loginExpiresAt;
1 change: 1 addition & 0 deletions src/store/slices/loginInfo/loginInfo.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ export interface LoginInfoSliceType<
crossWindowLogin: LoginInfoType | null;
logoutRoute?: string;
isWalletConnectV2Initialized?: boolean;
loginExpiresAt: number | null;
}
3 changes: 2 additions & 1 deletion src/store/slices/loginInfo/loginInfoSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export const initialState: LoginInfoSliceType = {
walletLogin: null,
extensionLogin: null,
operaLogin: null,
crossWindowLogin: null
crossWindowLogin: null,
loginExpiresAt: null
};

function getTokenInfoSlice(): StateCreator<
Expand Down
Loading