-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetamaskAuthButton.tsx
More file actions
67 lines (59 loc) · 2.2 KB
/
MetamaskAuthButton.tsx
File metadata and controls
67 lines (59 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//@ts-nocheck
import React, { useEffect, useState } from "react";
import wagmi from "./wagmi";
export function MetamaskAuthButton({ thirdStorageClient }) {
const [isInitializing, setIsInitializing] = useState(false);
const { useAccount, useSignMessage, useNetwork, useConnect } = wagmi;
const { connectAsync, connectors, isLoading } = useConnect();
const { address, isConnected } = useAccount();
const { signMessageAsync } = useSignMessage();
const { chain: activeChain } = useNetwork();
const signIn = async (a = null, chainId = null) => {
try {
let res = {};
if (!isConnected) {
res = await connectAsync({
connector: connectors[1],
});
a = res.account;
chainId = res.chain?.id;
} else {
a = address;
chainId = activeChain?.id;
if (!address || !chainId) return;
}
setIsInitializing(false);
if (await thirdStorageClient.signIn(a, chainId, signMessageAsync)) {
alert("Logged in!");
window.location.href = window.location.href;
}
} catch (error) {
setIsInitializing(false);
console.log(error.message);
}
};
return (
<button
onClick={async () => {
setIsInitializing(true);
await signIn();
}}
disabled={
!connectors[1].ready || isConnected || thirdStorageClient.isConnected
}
className="p-3 border rounded-xl border-gray-400 text-[#ffffff9d] title"
>
{isInitializing
? "Connecting"
: isConnected
? `Connected to ${thirdStorageClient.connectedAddress.substring(
0,
3
)}...${thirdStorageClient.connectedAddress.substring(
thirdStorageClient.connectedAddress.length - 3,
thirdStorageClient.connectedAddress.length
)} `
: "Connect Wallet with Metamask"}
</button>
);
}