-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
45df82b
commit 24377dd
Showing
10 changed files
with
81 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import { isLoggedInSelector } from 'store/selectors/accountSelectors'; | ||
import { getAddress } from './getAddress'; | ||
import { getState } from 'store/store'; | ||
|
||
export function getIsLoggedIn() { | ||
return Boolean(getAddress()); | ||
return isLoggedInSelector(getState()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { StoreType } from '../store.types'; | ||
import { StoreApi } from 'zustand/vanilla'; | ||
import { logoutMiddleware } from './logoutMiddleware'; | ||
|
||
export const applyMiddleware = (store: StoreApi<StoreType>) => { | ||
store.subscribe(logoutMiddleware); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './applyMiddleware'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { storage } from 'storage'; | ||
import { WritableDraft } from 'immer'; | ||
import { initialState as initialAccountState } from 'store/slices/account/accountSlice'; | ||
import { initialState as initialLoginInfoState } from 'store/slices/loginInfo/loginInfoSlice'; | ||
import { localStorageKeys } from 'storage/local'; | ||
import { isLoggedInSelector } from 'store/selectors'; | ||
import { StoreType } from '../store.types'; | ||
|
||
export const resetStore = (store: WritableDraft<StoreType>) => { | ||
store.account = initialAccountState; | ||
store.loginInfo = initialLoginInfoState; | ||
}; | ||
|
||
export function getNewLoginExpiresTimestamp() { | ||
return new Date().setHours(new Date().getHours() + 24); | ||
} | ||
|
||
export function setLoginExpiresAt(expiresAt: number) { | ||
storage.local.setItem({ | ||
key: localStorageKeys.loginExpiresAt, | ||
data: expiresAt, | ||
expires: expiresAt | ||
}); | ||
} | ||
|
||
export const logoutMiddleware = (newStore: StoreType) => { | ||
const isLoggedIn = isLoggedInSelector(newStore); | ||
const loginTimestamp = storage.local.getItem(localStorageKeys.loginExpiresAt); | ||
|
||
if (!isLoggedIn) { | ||
return; | ||
} | ||
|
||
if (loginTimestamp == null) { | ||
setLoginExpiresAt(getNewLoginExpiresTimestamp()); | ||
return; | ||
} | ||
|
||
const now = Date.now(); | ||
const isExpired = loginTimestamp - now < 0; | ||
|
||
if (isExpired) { | ||
// logout | ||
resetStore(newStore); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,43 @@ | ||
import { createStore } from 'zustand/vanilla'; | ||
import { | ||
createJSONStorage, | ||
devtools, | ||
persist, | ||
subscribeWithSelector | ||
} from 'zustand/middleware'; | ||
import { createJSONStorage, devtools, persist } from 'zustand/middleware'; | ||
import { immer } from 'zustand/middleware/immer'; | ||
import { networkSlice } from './slices/network/networkSlice'; | ||
import { accountSlice } from './slices/account/accountSlice'; | ||
import { createBoundedUseStore } from './createBoundedStore'; | ||
import { loginInfoSlice } from './slices/loginInfo'; | ||
import { StoreType } from './store.types'; | ||
import { applyMiddleware } from './middleware/applyMiddleware'; | ||
|
||
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>( | ||
subscribeWithSelector( | ||
devtools( | ||
persist( | ||
immer((...args) => ({ | ||
network: networkSlice(...args), | ||
account: accountSlice(...args), | ||
loginInfo: loginInfoSlice(...args) | ||
})), | ||
{ | ||
name: 'sdk-dapp-store', | ||
storage: createJSONStorage(() => localStorage) | ||
} | ||
) | ||
devtools( | ||
persist( | ||
immer((...args) => ({ | ||
network: networkSlice(...args), | ||
account: accountSlice(...args), | ||
loginInfo: loginInfoSlice(...args) | ||
})), | ||
{ | ||
name: 'sdk-dapp-store', | ||
storage: createJSONStorage(() => localStorage) | ||
} | ||
) | ||
) | ||
); | ||
|
||
applyMiddleware(store); | ||
|
||
export const getState = () => store.getState(); | ||
|
||
export const useStore = createBoundedUseStore(store); |