Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dcellar-web-ui): auto logout if wallet is locked #162

Closed
wants to merge 43 commits into from
Closed
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
f424a7c
feat(dcellar-web-ui): add select object
aiden-cao Jul 26, 2023
3d8c012
feat(dcellar-web-ui): add select object
aiden-cao Jul 26, 2023
55d394b
feat(dcellar-web-ui): add temp account
devinxl Jul 25, 2023
511c5d2
fix(dcellar-web-ui): add batch delete
Miyaww Jul 28, 2023
b59889a
fix(dcellar-web-ui): merge upload
Miyaww Jul 28, 2023
0aca0e3
fix(dcellar-web-ui): batch delete commit
Miyaww Jul 28, 2023
fe00195
fix: batch delete
Miyaww Aug 1, 2023
8e43e6d
fix: batch delete
Miyaww Aug 1, 2023
bb0d041
Merge branch 'main' into feat/batch-operations
aiden-cao Aug 4, 2023
f4c1831
feat(dcellar-web-ui): add multi download
aiden-cao Aug 4, 2023
635e1bc
fix(dcellar-web-ui): auto logout if wallet is locked
wenty22 Aug 7, 2023
f156044
test(dcellar-web-ui): add wallet log
wenty22 Aug 7, 2023
6776ab0
feat(dcellar-web-ui): add wallet log
wenty22 Aug 7, 2023
0b0d892
test(dcellar-web-ui): add log
wenty22 Aug 7, 2023
631d817
test(dcellar-web-ui): add log
wenty22 Aug 7, 2023
9650643
test(dcellar-web-ui): add log
wenty22 Aug 7, 2023
c2e42cc
test: add log
wenty22 Aug 7, 2023
9ee856c
test: add log
wenty22 Aug 7, 2023
8d66dcc
fix(dcellar-web-ui): fix refresh page will logout if using trust wallet
wenty22 Aug 7, 2023
5d9884a
test: add log
wenty22 Aug 7, 2023
3e1201b
test: test wallet
wenty22 Aug 7, 2023
3913a3a
test: add log
wenty22 Aug 7, 2023
bcda7ea
test: add log
wenty22 Aug 7, 2023
a89593f
test: remove MetaMaskConnector
wenty22 Aug 7, 2023
d346d54
fix: customize MetaMaskConnector & TrustWalletConnector
wenty22 Aug 7, 2023
88de960
test: add TrustWalletConnector connect method
wenty22 Aug 7, 2023
237bb81
refactor: fix wallet issue
wenty22 Aug 7, 2023
22174e4
fix(dcellar-web-ui): fix wallet issue
wenty22 Aug 7, 2023
91a972d
fix: remove wallet log
wenty22 Aug 7, 2023
e5f5ea4
test: test
wenty22 Aug 7, 2023
b5342d9
test: add log
wenty22 Aug 7, 2023
be9d9e5
fix: fix wallet issues
wenty22 Aug 8, 2023
4f42db8
fix: remove logs
wenty22 Aug 8, 2023
08fd237
feat(dcellar-web-ui): update multi download ui
aiden-cao Aug 8, 2023
c7f9ee1
feat(dcellar-web-ui): add share permission grant
aiden-cao Aug 8, 2023
170d88f
feat(dcellar-web-ui): add share permission grant
aiden-cao Aug 8, 2023
c191b54
fix(dcellar-web-ui): merge main branch
Miyaww Aug 9, 2023
0686097
feat(dcellar-web-ui): add fault sps & fix download pop tip ui issue
aiden-cao Aug 9, 2023
d7d4030
Merge branch 'feat/batch-delete' into feat/batch-operations
aiden-cao Aug 9, 2023
5e592d9
feat(dcellar-web-ui): fix batch delete issues
aiden-cao Aug 9, 2023
b824531
feat(dcellar-web-ui): fix createTmpAccount params
aiden-cao Aug 9, 2023
f1f8d6c
Merge branch 'release/0809' into feat/wenty/wallet
wenty22 Aug 9, 2023
db7a4bf
fix(dcellar-web-ui): remove unused dependencies
wenty22 Aug 9, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion apps/dcellar-web-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@
"@reduxjs/toolkit": "^1.9.5",
"react-redux": "^8.1.1",
"next-redux-wrapper": "^8.1.0",
"redux-persist": "^6.0.0"
"redux-persist": "^6.0.0",
"@wagmi/core": "^0.10.13",
"@ethersproject/address": "^5.0.0"
},
"devDependencies": {
"@babel/plugin-syntax-flow": "^7.14.5",
Expand Down
26 changes: 21 additions & 5 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 All @@ -19,11 +19,12 @@ export function LoginContextProvider(props: PropsWithChildren<LoginContextProvid
const dispatch = useAppDispatch();
const { children, inline = false } = props;
const { loginAccount } = useAppSelector((root) => root.persist);


const { disconnect } = useDisconnect();

const logout = useCallback(
(removeSpAuth = false) => {
(removeSpAuth = true) => {
dispatch(resetUploadQueue({loginAccount}))
dispatch(setLogout(removeSpAuth));
disconnect();
Expand All @@ -42,21 +43,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 logout.
const timer = setTimeout(() => {
if (!connector) {
logout()
}
}, 1000)

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

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
@@ -1,6 +1,6 @@
import { WagmiConfig, createClient } from 'wagmi';
import { provider, webSocketProvider } from '@/context/WalletConnectContext/config/chains';
import { connectors } from '@/context/WalletConnectContext/config/connectors';
import { provider, webSocketProvider } from '@/context/WalletConnectContext/chains';
import { connectors } from '@/context/WalletConnectContext/connectors';

const client = createClient({
autoConnect: true,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { MetaMaskConnector as WagmiMetaMaskConnector } from 'wagmi/connectors/metaMask';

export class MetaMaskConnector extends WagmiMetaMaskConnector {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { Chain } from 'wagmi'
import { MetaMaskConnector as WagmiMetaMaskConnector } from 'wagmi/connectors/metaMask';
import {
getClient,
} from '@wagmi/core'

export type TrustWalletConnectorOptions = {
shimDisconnect?: boolean
}

export class TrustWalletConnector extends WagmiMetaMaskConnector {
readonly id: any = 'trustWallet';

constructor({
chains,
options: _options,
}: {
chains?: Chain[]
options?: TrustWalletConnectorOptions
} = {}) {

const options = {
name: 'Trust Wallet',
shimDisconnect: true,
UNSTABLE_shimOnConnectSelectAccount: true,
getProvider: getTrustWalletProvider,
..._options,
}

super({
chains,
options,
})
}

async disconnect() {
super.disconnect()

const provider: any = await this.getProvider()
if (!provider?.off) return

provider.off('accountsChanged', this.onAccountsChanged)
provider.off('chainChanged', this.onChainChanged)
provider.off('disconnect', this.onDisconnect)

if (this.options.shimDisconnect) {
getClient().storage?.removeItem(this.shimDisconnectKey)
}
}
}

export function getTrustWalletProvider() {
const isTrustWallet = (ethereum: any) => {
return !!ethereum.isTrust
}

const injectedProviderExist = typeof window !== 'undefined' && typeof window.ethereum !== 'undefined'

if (!injectedProviderExist) {
return
}

if (isTrustWallet(window.ethereum)) {
return window.ethereum
}

if (window.ethereum?.providers) {
return window.ethereum.providers.find(isTrustWallet)
}

return window.trustWallet
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { chains } from '@/context/WalletConnectContext/chains';
import { MetaMaskConnector } from '@/context/WalletConnectContext/connectors/MetaMaskConnector';
import { TrustWalletConnector } from '@/context/WalletConnectContext/connectors/TrustWalletConnector';

const trustWalletConnector = new TrustWalletConnector({ chains })
const metaMaskConnector = new MetaMaskConnector({ chains })

export const connectors = [trustWalletConnector, metaMaskConnector];
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ import { ConnectorData, useAccount } from 'wagmi';
export type WalletSwitchAccountHandler = (data: ConnectorData) => void;

export function useWalletSwitchAccount(handler: WalletSwitchAccountHandler) {
const { connector } = useAccount();
const {address, connector } = useAccount();

const handlerRef = useSaveFuncRef(handler);

useEffect(() => {
const handler = (data: ConnectorData) => {
if (data.account) {
const onChange = (data: ConnectorData) => {
if (data.account && data.account !== address) {
handlerRef.current?.(data);
}
};

connector?.on('change', handler);
connector?.on('change', onChange);
return () => {
connector?.off('change', handler);
connector?.off('change', onChange);
};
}, [connector, handlerRef]);
}, [address, connector, handlerRef]);
}
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
Loading