-
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.
Showing
24 changed files
with
607 additions
and
101 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { getWindowLocation } from 'utils/window/getWindowLocation'; | ||
|
||
export const getWebviewToken = () => { | ||
const { search } = getWindowLocation(); | ||
const urlSearchParams = new URLSearchParams(search) as any; | ||
const searchParams = Object.fromEntries(urlSearchParams); | ||
|
||
return searchParams?.accessToken; | ||
}; |
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,72 @@ | ||
import { storage } from 'storage'; | ||
import { localStorageKeys } from 'storage/local'; | ||
import { LoginMethodsEnum } from 'types'; | ||
import { getAddress } from '../account/getAddress'; | ||
import { CrossWindowProvider } from 'lib/sdkWebWalletCrossWindowProvider'; | ||
import { logoutAction } from 'store/actions/sharedActions/sharedActions'; | ||
import { getWebviewToken } from '../account/getWebviewToken'; | ||
import { getAccountProvider } from 'core/providers/accountProvider'; | ||
import { getProviderType } from 'core/providers/helpers/utils'; | ||
|
||
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({ | ||
key: localStorageKeys.logoutEvent, | ||
data: address, | ||
expires: 0 | ||
}); | ||
|
||
storage.local.removeItem(localStorageKeys.logoutEvent); | ||
}; | ||
|
||
export type LogoutPropsType = { | ||
shouldAttemptReLogin?: boolean; | ||
shouldBroadcastLogoutAcrossTabs?: boolean; | ||
/* | ||
* Only used for web-wallet crossWindow login | ||
*/ | ||
hasConsentPopup?: boolean; | ||
}; | ||
|
||
export async function logout( | ||
shouldAttemptReLogin = Boolean(getWebviewToken()), | ||
options = { | ||
shouldBroadcastLogoutAcrossTabs: true, | ||
hasConsentPopup: false | ||
} | ||
) { | ||
let address = getAddress(); | ||
const provider = getAccountProvider(); | ||
const providerType = getProviderType(provider); | ||
|
||
if (shouldAttemptReLogin && provider?.relogin != null) { | ||
return provider.relogin(); | ||
} | ||
|
||
if (options.shouldBroadcastLogoutAcrossTabs) { | ||
broadcastLogoutAcrossTabs(address); | ||
} | ||
|
||
try { | ||
logoutAction(); | ||
|
||
if ( | ||
options.hasConsentPopup && | ||
providerType === LoginMethodsEnum.crossWindow | ||
) { | ||
(provider as unknown as CrossWindowProvider).setShouldShowConsentPopup( | ||
true | ||
); | ||
} | ||
|
||
await provider.logout(); | ||
} catch (err) { | ||
console.error('Logging out error:', err); | ||
} | ||
} |
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 @@ | ||
export { CrossWindowProvider } from '@multiversx/sdk-web-wallet-cross-window-provider/out/CrossWindowProvider/CrossWindowProvider'; |
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,4 @@ | ||
import * as local from './local'; | ||
import * as session from './session'; | ||
|
||
export const storage = { session, local }; |
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,80 @@ | ||
import { getUnixTimestamp } from 'utils/dateTime'; | ||
|
||
export const localStorageKeys = { | ||
loginExpiresAt: 'sdk-dapp-login-expires-at', | ||
logoutEvent: 'sdk-dapp-logout-event' | ||
} as const; | ||
|
||
type LocalValueType = keyof typeof localStorageKeys; | ||
type LocalKeyType = typeof localStorageKeys[LocalValueType]; | ||
|
||
type ExpiresType = number | false; | ||
|
||
const hasLocalStorage = typeof localStorage !== 'undefined'; | ||
|
||
export const setItem = ({ | ||
key, | ||
data, | ||
expires | ||
}: { | ||
key: LocalKeyType; | ||
data: any; | ||
expires: ExpiresType; | ||
}) => { | ||
if (!hasLocalStorage) { | ||
return; | ||
} | ||
localStorage.setItem( | ||
String(key), | ||
JSON.stringify({ | ||
expires, | ||
data | ||
}) | ||
); | ||
}; | ||
|
||
export const getItem = (key: LocalKeyType): any => { | ||
if (!hasLocalStorage) { | ||
return; | ||
} | ||
const item = localStorage.getItem(String(key)); | ||
if (!item) { | ||
return null; | ||
} | ||
|
||
const deserializedItem = JSON.parse(item); | ||
if (!deserializedItem) { | ||
return null; | ||
} | ||
|
||
if ( | ||
!deserializedItem.hasOwnProperty('expires') || | ||
!deserializedItem.hasOwnProperty('data') | ||
) { | ||
return null; | ||
} | ||
|
||
const expired = getUnixTimestamp() >= deserializedItem.expires; | ||
if (expired) { | ||
localStorage.removeItem(String(key)); | ||
return null; | ||
} | ||
|
||
return deserializedItem.data; | ||
}; | ||
|
||
export const removeItem = (key: LocalKeyType) => { | ||
if (!hasLocalStorage) { | ||
return; | ||
} | ||
|
||
localStorage.removeItem(String(key)); | ||
}; | ||
|
||
export const clear = () => { | ||
if (!hasLocalStorage) { | ||
return; | ||
} | ||
|
||
localStorage.clear(); | ||
}; |
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,57 @@ | ||
export type SessionKeyType = 'address' | 'shard' | 'toasts' | 'toastProgress'; | ||
type ExpiresType = number | false; | ||
|
||
export interface SetItemType { | ||
key: SessionKeyType; | ||
data: any; | ||
expires: ExpiresType; | ||
} | ||
|
||
export const setItem = ({ key, data, expires }: SetItemType) => { | ||
sessionStorage.setItem( | ||
String(key), | ||
JSON.stringify({ | ||
expires, | ||
data | ||
}) | ||
); | ||
}; | ||
|
||
export const getItem = (key: SessionKeyType): any => { | ||
const item = sessionStorage.getItem(String(key)); | ||
if (!item) { | ||
return null; | ||
} | ||
|
||
const deserializedItem = JSON.parse(item); | ||
if (!deserializedItem) { | ||
return null; | ||
} | ||
|
||
if ( | ||
!deserializedItem.hasOwnProperty('expires') || | ||
!deserializedItem.hasOwnProperty('data') | ||
) { | ||
return null; | ||
} | ||
|
||
const expired = Date.now() >= deserializedItem.expires; | ||
if (expired) { | ||
sessionStorage.removeItem(String(key)); | ||
return null; | ||
} | ||
|
||
return deserializedItem.data; | ||
}; | ||
|
||
export const removeItem = (key: SessionKeyType) => | ||
sessionStorage.removeItem(String(key)); | ||
|
||
export const clear = () => sessionStorage.clear(); | ||
|
||
export const storage = { | ||
setItem, | ||
getItem, | ||
removeItem, | ||
clear | ||
}; |
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
Oops, something went wrong.