Skip to content

Commit

Permalink
fix(dcellar-web-ui): auto logout if wallet is locked
Browse files Browse the repository at this point in the history
  • Loading branch information
wenty22 committed Aug 7, 2023
1 parent bb13d56 commit 635e1bc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
23 changes: 19 additions & 4 deletions apps/dcellar-web-ui/src/context/LoginContext/provider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PropsWithChildren, useCallback, useMemo } from 'react';
import { PropsWithChildren, useCallback, useEffect, useMemo } from 'react';

import { LoginContext } from '@/context/LoginContext/index';

Expand Down Expand Up @@ -42,21 +42,36 @@ export function LoginContextProvider(props: PropsWithChildren<LoginContextProvid
});

const { pathname } = useRouter();
const { address: walletAddress } = useAccount();
const { address: walletAddress, connector } = useAccount();

useAsyncEffect(async () => {
useEffect(() => {
if (pathname === '/' || inline) return;

if (!walletAddress || loginAccount !== walletAddress) {
logout();
}

// Once the wallet is connected, we can get the address
// but if wallet is locked, we can't get the connector from wagmi
// to avoid errors when using the connector, we treat this situation as not log in.
const timer = setTimeout(() => {
if (!connector) {
logout()
}
}, 1000)

return () => {
clearTimeout(timer)
}
}, [walletAddress, connector, pathname, inline, loginAccount, logout])

useAsyncEffect(async () => {
if (loginAccount === walletAddress) {
// expire date less than 24h,remove sp auth & logout
const spMayExpired = await dispatch(checkSpOffChainMayExpired(walletAddress));
if (spMayExpired) logout(true);
}
}, [walletAddress, pathname]);
}, [walletAddress]);

const { pass } = useLoginGuard(inline);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,21 @@ const trustWalletConnector = new InjectedConnector({
options: {
name: 'Trust Wallet',
shimDisconnect: true,
getProvider: () => (typeof window !== 'undefined' ? window.trustwallet : undefined),
getProvider: () => {
try {
if (typeof window !== 'undefined' && typeof window?.trustWallet !== 'undefined') {
Object.defineProperty(window.trustWallet, 'removeListener', {
value: window.trustWallet.off,
});
return window?.trustWallet;
} else {
return null;
}
} catch (e) {
// eslint-disable-next-line no-console
console.log('Trust Wallet Provider Error:', e);
}
},
},
});

Expand Down
2 changes: 1 addition & 1 deletion apps/dcellar-web-ui/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ declare global {
ethereum: any;
ga: any;
clipboardData: any;
trustwallet: any;
trustWallet: any;
// zk.wasm export
eddsaSign: any;
}
Expand Down

0 comments on commit 635e1bc

Please sign in to comment.