Skip to content

Commit

Permalink
Merge pull request #57 from AmbireTech/fix-user-loging
Browse files Browse the repository at this point in the history
Fix user logging
  • Loading branch information
ivopaunov authored Mar 25, 2024
2 parents 333124a + d1a80fc commit 350f272
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
6 changes: 3 additions & 3 deletions src/components/TopBar/TopBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -77,7 +77,7 @@ function TopBar() {
})
.then((res) => {
if (res) {
updateAdexAccount(null)
resetAdexAccount()
showInfoNotification('Successfully logged out', 'Logging out')
navigate('/login', { replace: true })
}
Expand All @@ -92,7 +92,7 @@ function TopBar() {
disconnectWallet,
fetchAuthRequest,
navigate,
updateAdexAccount,
resetAdexAccount,
showInfoNotification,
showDangerNotification
])
Expand Down
30 changes: 16 additions & 14 deletions src/contexts/AccountContext/AccountContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface IAccountContext {
disconnectWallet: () => void
updateAdexAccount: (value: any) => void
updateAccessToken: () => Promise<any>
resetAdexAccount: () => void
}

const AccountContext = createContext<IAccountContext | null>(null)
Expand All @@ -43,6 +44,7 @@ const AccountProvider: FC<PropsWithChildren> = ({ children }) => {
setAdexAccount((prevState) => (newValue === null ? newValue : { ...prevState, ...newValue })),
[setAdexAccount]
)
const resetAdexAccount = useCallback(() => updateAdexAccount(null), [updateAdexAccount])

const connectWallet = useCallback(
() => ambireSDK.openLogin({ chainId: DEFAULT_CHAIN_ID }),
Expand All @@ -53,7 +55,6 @@ const AccountProvider: FC<PropsWithChildren> = ({ 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
Expand Down Expand Up @@ -81,7 +82,7 @@ const AccountProvider: FC<PropsWithChildren> = ({ children }) => {
showDangerNotification
])

const handleLoginSuccess = useCallback(
const handleRegistrationOrLoginSuccess = useCallback(
({ address, chainId }: any) => {
if (
!address ||
Expand All @@ -99,7 +100,6 @@ const AccountProvider: FC<PropsWithChildren> = ({ children }) => {
})
.catch((error) => {
console.error('Get message to sign failed', error)
disconnectWallet()
showDangerNotification(error.message, 'Get message to sign failed')
})
},
Expand All @@ -108,8 +108,7 @@ const AccountProvider: FC<PropsWithChildren> = ({ children }) => {
adexAccount?.chainId,
adexAccount?.authMsgResp,
updateAdexAccount,
showDangerNotification,
disconnectWallet
showDangerNotification
]
)

Expand All @@ -133,16 +132,14 @@ const AccountProvider: FC<PropsWithChildren> = ({ children }) => {
})
.catch((error) => {
console.error('Error verify login:', error)
disconnectWallet()
showDangerNotification(error.message, 'Verify login failed')
})
},
[
adexAccount?.authMsgResp,
adexAccount?.authenticated,
updateAdexAccount,
showDangerNotification,
disconnectWallet
showDangerNotification
]
)

Expand All @@ -151,22 +148,24 @@ const AccountProvider: FC<PropsWithChildren> = ({ 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,
Expand All @@ -188,12 +187,14 @@ const AccountProvider: FC<PropsWithChildren> = ({ children }) => {
() => ({
adexAccount,
authenticated,
// authenticated: true,
connectWallet,
disconnectWallet,
signMessage,
ambireSDK,
updateAdexAccount,
updateAccessToken
updateAccessToken,
resetAdexAccount
}),
[
adexAccount,
Expand All @@ -203,7 +204,8 @@ const AccountProvider: FC<PropsWithChildren> = ({ children }) => {
signMessage,
ambireSDK,
updateAdexAccount,
updateAccessToken
updateAccessToken,
resetAdexAccount
]
)

Expand Down

0 comments on commit 350f272

Please sign in to comment.