Skip to content

Commit

Permalink
Add logout listener
Browse files Browse the repository at this point in the history
  • Loading branch information
arhtudormorar committed Jul 16, 2024
1 parent ccb95cd commit a15e833
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 14 deletions.
12 changes: 12 additions & 0 deletions src/store/actions/loginInfo/loginInfoActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,15 @@ export const setIsWalletConnectV2Initialized = (isInitialized: boolean) =>
store.setState(({ loginInfo: state }) => {
state.isWalletConnectV2Initialized = isInitialized;
});

// reset by passing null
export const updateLoginExpiresAt = (data?: null) =>
store.setState(({ loginInfo: state }) => {
if (data === null) {
state.loginExpiresAt = null;
return;
}

const loginExpiresAt = new Date().setHours(new Date().getHours() + 24);
state.loginExpiresAt = loginExpiresAt;
});
27 changes: 25 additions & 2 deletions src/store/actions/sharedActions/sharedActions.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
import { Address } from '@multiversx/sdk-core/out';
import { initialState as initialAccountState } from 'store/slices/account/accountSlice';
import { initialState as initialLoginInfoState } from 'store/slices/loginInfo/loginInfoSlice';
import { store } from '../../store';
import { getState, store } from '../../store';
import { LoginMethodsEnum } from 'types/enums.types';
import { updateLoginExpiresAt } from '../loginInfo/loginInfoActions';

store.subscribe(
(state) => ({ account: state.account, network: state.network }), // only listen to changes in account and network
({ account }) => {
const isLoggedIn = Boolean(account.address);
const loginTimestamp = getState().loginInfo.loginExpiresAt;

if (!isLoggedIn || loginTimestamp == null) {
return;
}

const isExpired = loginTimestamp - Date.now() < 0;

if (isExpired) {
logoutAction();
return;
}

updateLoginExpiresAt();
}
);

export const logoutAction = () =>
store.setState((store) => {
store.account = initialAccountState;
store.loginInfo = initialLoginInfoState;
store.loginInfo.loginExpiresAt = null;
});

export interface LoginActionPayloadType {
Expand All @@ -20,5 +43,5 @@ export const loginAction = ({ address, loginMethod }: LoginActionPayloadType) =>
account.address = address;
account.publicKey = new Address(address).hex();
loginInfo.loginMethod = loginMethod;
// setLoginExpiresAt(getNewLoginExpiresTimestamp());
updateLoginExpiresAt();
});
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 @@ -22,6 +22,7 @@ export interface LoginInfoSliceType {
walletConnectLogin: WalletConnectLoginType | null;
ledgerLogin: LedgerLoginType | null;
tokenLogin: TokenLoginType | null;
loginExpiresAt: number | null;
walletLogin: LoginInfoType | null;
extensionLogin: LoginInfoType | null;
operaLogin: LoginInfoType | null;
Expand Down
1 change: 1 addition & 0 deletions src/store/slices/loginInfo/loginInfoSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const initialState: LoginInfoSliceType = {
walletConnectLogin: null,
ledgerLogin: null,
tokenLogin: null,
loginExpiresAt: null,
walletLogin: null,
extensionLogin: null,
operaLogin: null,
Expand Down
33 changes: 21 additions & 12 deletions src/store/store.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { createStore } from 'zustand/vanilla';
import { createJSONStorage, devtools, persist } from 'zustand/middleware';
import {
createJSONStorage,
devtools,
persist,
subscribeWithSelector
} from 'zustand/middleware';
import { immer } from 'zustand/middleware/immer';
import { networkSlice } from './slices/network/networkSlice';
import { accountSlice } from './slices/account/accountSlice';
Expand All @@ -8,29 +13,33 @@ import { loginInfoSlice } from './slices/loginInfo';
import { StoreType } from './store.types';

export type MutatorsIn = [
['zustand/subscribeWithSelector', never],
['zustand/devtools', never],
['zustand/persist', unknown],
['zustand/immer', never]
];

export type MutatorsOut = [
['zustand/subscribeWithSelector', never],
['zustand/devtools', never],
['zustand/persist', StoreType],
['zustand/immer', never]
];

export const store = createStore<StoreType, MutatorsOut>(
devtools(
persist(
immer((...args) => ({
network: networkSlice(...args),
account: accountSlice(...args),
loginInfo: loginInfoSlice(...args)
})),
{
name: 'sdk-dapp-store',
storage: createJSONStorage(() => localStorage)
}
subscribeWithSelector(
devtools(
persist(
immer((...args) => ({
network: networkSlice(...args),
account: accountSlice(...args),
loginInfo: loginInfoSlice(...args)
})),
{
name: 'sdk-dapp-store',
storage: createJSONStorage(() => localStorage)
}
)
)
)
);
Expand Down

0 comments on commit a15e833

Please sign in to comment.