From 3b23b2d2d0c3f11d8fc57536b77a55353ce51a2f Mon Sep 17 00:00:00 2001 From: daniecon Date: Sun, 18 Sep 2022 18:07:42 +0200 Subject: [PATCH] Auto connect when signed in --- dapp/components/DropdownAccount.tsx | 6 ++--- dapp/components/connectWalletButton.tsx | 34 +++++++++++++------------ dapp/components/logInBox.tsx | 5 ++-- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/dapp/components/DropdownAccount.tsx b/dapp/components/DropdownAccount.tsx index dd66818..d3ac967 100644 --- a/dapp/components/DropdownAccount.tsx +++ b/dapp/components/DropdownAccount.tsx @@ -24,7 +24,7 @@ const DropdownAccount = (props: DropdownProps) => {
{ens ?? shortenAddress(props.account)} @@ -66,7 +66,7 @@ const DropdownAccount = (props: DropdownProps) => { )} - + {/* {({ active }) => ( { Logout )} - + */}
diff --git a/dapp/components/connectWalletButton.tsx b/dapp/components/connectWalletButton.tsx index 73bed61..ce6e4a9 100644 --- a/dapp/components/connectWalletButton.tsx +++ b/dapp/components/connectWalletButton.tsx @@ -3,6 +3,7 @@ import { useEthers } from "@usedapp/core" import Web3Modal from "web3modal" import WalletConnectProvider from "@walletconnect/web3-provider" import { useTheme } from "next-themes" +import { ethers } from "ethers" const ConnectWalletButton = () => { const { theme } = useTheme() @@ -28,6 +29,7 @@ const ConnectWalletButton = () => { const [loaded, setLoaded] = useState(false) useEffect(() => setLoaded(true), []) + // Set up Web3modal const [web3Modal, setWeb3Modal] = useState(undefined) useEffect(() => { const providerOptions = { @@ -58,16 +60,6 @@ const ConnectWalletButton = () => { } }, [loaded, theme]) - // const connect = async () => { - // try { - // const provider = await web3Modal?.connect() - // await activate(provider) - // setActivateError("") - // } catch (error: any) { - // setActivateError(error.message) - // } - // } - const connect = useCallback(async () => { try { const provider = await web3Modal?.connect() @@ -78,12 +70,22 @@ const ConnectWalletButton = () => { } }, [web3Modal, activate]) - // useEffect(() => { - // if (web3Modal && web3Modal.cachedProvider) { - // console.log(web3Modal.cachedProvider) - // connect() - // } - // }, [connect, web3Modal]) + // Set up provider if already connected + useEffect(() => { + const { ethereum } = window + const checkMetaMaskConnected = async () => { + var provider = new ethers.providers.Web3Provider(ethereum) + const accounts = await provider.listAccounts() + const connected = accounts.length > 0 + console.log("CONNECTED", connected) + if (connected) { + activate(provider) + } + } + if (ethereum) { + checkMetaMaskConnected() + } + }) if (!loaded) return null diff --git a/dapp/components/logInBox.tsx b/dapp/components/logInBox.tsx index cdb9458..63f874a 100644 --- a/dapp/components/logInBox.tsx +++ b/dapp/components/logInBox.tsx @@ -1,12 +1,13 @@ import { useEthers } from "@usedapp/core" import { useState } from "react" -import { motion, AnimatePresence } from "framer-motion" +import { motion } from "framer-motion" import { ConnectWalletButton, TotpSetup, ZkPasswordSetup, CardChoice } from "./" const LogInBox = () => { - const { account } = useEthers() + const { account, library: provider } = useEthers() const [authType, setAuthType] = useState("") + return ( <> {!account ? (