diff --git a/src/components/TopBar/TopBar.tsx b/src/components/TopBar/TopBar.tsx index 88f57052..b6a6f752 100644 --- a/src/components/TopBar/TopBar.tsx +++ b/src/components/TopBar/TopBar.tsx @@ -46,7 +46,7 @@ const useStyles = createStyles((theme) => ({ function TopBar() { const { classes, cx } = useStyles() - const { adexAccount, disconnectWallet, updateAdexAccount } = useAccount() + const { adexAccount, disconnectWallet, resetAdexAccount } = useAccount() const { showInfoNotification, showDangerNotification } = useCustomNotifications() const location = useLocation() const splitPath = useMemo(() => location.pathname.split('/'), [location.pathname]) @@ -77,7 +77,7 @@ function TopBar() { }) .then((res) => { if (res) { - updateAdexAccount(null) + resetAdexAccount() showInfoNotification('Successfully logged out', 'Logging out') navigate('/login', { replace: true }) } @@ -92,7 +92,7 @@ function TopBar() { disconnectWallet, fetchAuthRequest, navigate, - updateAdexAccount, + resetAdexAccount, showInfoNotification, showDangerNotification ]) diff --git a/src/contexts/AccountContext/AccountContext.tsx b/src/contexts/AccountContext/AccountContext.tsx index f90170d9..bc19423b 100644 --- a/src/contexts/AccountContext/AccountContext.tsx +++ b/src/contexts/AccountContext/AccountContext.tsx @@ -19,6 +19,7 @@ interface IAccountContext { disconnectWallet: () => void updateAdexAccount: (value: any) => void updateAccessToken: () => Promise + resetAdexAccount: () => void } const AccountContext = createContext(null) @@ -43,6 +44,7 @@ const AccountProvider: FC = ({ children }) => { setAdexAccount((prevState) => (newValue === null ? newValue : { ...prevState, ...newValue })), [setAdexAccount] ) + const resetAdexAccount = useCallback(() => updateAdexAccount(null), [updateAdexAccount]) const connectWallet = useCallback( () => ambireSDK.openLogin({ chainId: DEFAULT_CHAIN_ID }), @@ -53,7 +55,6 @@ const AccountProvider: FC = ({ children }) => { (type: string, message: string) => ambireSDK.openSignMessage(type, message), [ambireSDK] ) - const hideAmbireSDKIframe = useCallback(() => ambireSDK.hideIframe(), [ambireSDK]) const updateAccessToken = useCallback(async () => { if (!adexAccount?.accessToken || !adexAccount?.refreshToken) return @@ -81,7 +82,7 @@ const AccountProvider: FC = ({ children }) => { showDangerNotification ]) - const handleLoginSuccess = useCallback( + const handleRegistrationOrLoginSuccess = useCallback( ({ address, chainId }: any) => { if ( !address || @@ -99,7 +100,6 @@ const AccountProvider: FC = ({ children }) => { }) .catch((error) => { console.error('Get message to sign failed', error) - disconnectWallet() showDangerNotification(error.message, 'Get message to sign failed') }) }, @@ -108,8 +108,7 @@ const AccountProvider: FC = ({ children }) => { adexAccount?.chainId, adexAccount?.authMsgResp, updateAdexAccount, - showDangerNotification, - disconnectWallet + showDangerNotification ] ) @@ -133,7 +132,6 @@ const AccountProvider: FC = ({ children }) => { }) .catch((error) => { console.error('Error verify login:', error) - disconnectWallet() showDangerNotification(error.message, 'Verify login failed') }) }, @@ -141,8 +139,7 @@ const AccountProvider: FC = ({ children }) => { adexAccount?.authMsgResp, adexAccount?.authenticated, updateAdexAccount, - showDangerNotification, - disconnectWallet + showDangerNotification ] ) @@ -151,22 +148,24 @@ const AccountProvider: FC = ({ children }) => { }, [disconnectWallet]) const handleLogoutSuccess = useCallback(() => { - hideAmbireSDKIframe() - }, [hideAmbireSDKIframe]) + resetAdexAccount() + }, [resetAdexAccount]) const handleActionRejected = useCallback(() => { disconnectWallet() }, [disconnectWallet]) useEffect(() => { - ambireSDK.onLoginSuccess(handleLoginSuccess) + ambireSDK.onRegistrationSuccess(handleRegistrationOrLoginSuccess) + ambireSDK.onLoginSuccess(handleRegistrationOrLoginSuccess) + ambireSDK.onAlreadyLoggedIn(handleRegistrationOrLoginSuccess) ambireSDK.onMsgSigned(handleMsgSigned) ambireSDK.onMsgRejected(handleMsgRejected) ambireSDK.onLogoutSuccess(handleLogoutSuccess) ambireSDK.onActionRejected(handleActionRejected) }, [ ambireSDK, - handleLoginSuccess, + handleRegistrationOrLoginSuccess, handleMsgSigned, handleMsgRejected, handleLogoutSuccess, @@ -188,12 +187,14 @@ const AccountProvider: FC = ({ children }) => { () => ({ adexAccount, authenticated, + // authenticated: true, connectWallet, disconnectWallet, signMessage, ambireSDK, updateAdexAccount, - updateAccessToken + updateAccessToken, + resetAdexAccount }), [ adexAccount, @@ -203,7 +204,8 @@ const AccountProvider: FC = ({ children }) => { signMessage, ambireSDK, updateAdexAccount, - updateAccessToken + updateAccessToken, + resetAdexAccount ] )