2022-09-13 09:43:58 +00:00
|
|
|
import { Box } from "@chakra-ui/react";
|
2022-09-15 15:59:54 +00:00
|
|
|
import AuthClient, { generateNonce } from "@walletconnect/auth-client";
|
2022-09-07 09:26:32 +00:00
|
|
|
import { version } from "@walletconnect/auth-client/package.json";
|
2022-09-02 09:34:03 +00:00
|
|
|
import type { NextPage } from "next";
|
2022-09-13 09:43:58 +00:00
|
|
|
import { useCallback, useEffect, useState } from "react";
|
|
|
|
import DefaultView from "../views/DefaultView";
|
|
|
|
import QrView from "../views/QrView";
|
|
|
|
import SignedInView from "../views/SignedInView";
|
2022-09-02 09:34:03 +00:00
|
|
|
|
2022-09-07 09:26:32 +00:00
|
|
|
console.log(`AuthClient@${version}`);
|
|
|
|
|
2022-09-02 09:34:03 +00:00
|
|
|
const Home: NextPage = () => {
|
|
|
|
const [client, setClient] = useState<AuthClient | null>();
|
2022-09-17 09:35:03 +00:00
|
|
|
const [hasInitialized, setHasInitialized] = useState(false);
|
2022-09-02 09:34:03 +00:00
|
|
|
const [uri, setUri] = useState<string>("");
|
2022-09-13 09:43:58 +00:00
|
|
|
const [address, setAddress] = useState<string>("");
|
2022-09-02 09:34:03 +00:00
|
|
|
|
|
|
|
const onSignIn = useCallback(() => {
|
|
|
|
if (!client) return;
|
|
|
|
client
|
|
|
|
.request({
|
2022-09-15 16:24:26 +00:00
|
|
|
aud: window.location.href,
|
|
|
|
domain: window.location.hostname.split(".").slice(-2).join("."),
|
2022-09-09 12:23:36 +00:00
|
|
|
chainId: "eip155:1",
|
2022-09-02 09:34:03 +00:00
|
|
|
type: "eip4361",
|
2022-09-15 15:59:54 +00:00
|
|
|
nonce: generateNonce(),
|
2022-09-02 09:34:03 +00:00
|
|
|
statement: "Sign in with wallet.",
|
|
|
|
})
|
|
|
|
.then(({ uri }) => setUri(uri));
|
|
|
|
}, [client, setUri]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
AuthClient.init({
|
|
|
|
relayUrl:
|
|
|
|
process.env.NEXT_PUBLIC_RELAY_URL || "wss://relay.walletconnect.com",
|
2022-10-14 20:26:47 +00:00
|
|
|
projectId: process.env.NEXT_PUBLIC_PROJECT_ID!,
|
2022-09-06 09:54:45 +00:00
|
|
|
metadata: {
|
|
|
|
name: "react-dapp-auth",
|
|
|
|
description: "React Example Dapp for Auth",
|
|
|
|
url: window.location.host,
|
|
|
|
icons: [],
|
|
|
|
},
|
2022-09-02 09:34:03 +00:00
|
|
|
})
|
2022-09-07 09:26:32 +00:00
|
|
|
.then((authClient) => {
|
|
|
|
setClient(authClient);
|
2022-09-17 09:35:03 +00:00
|
|
|
setHasInitialized(true);
|
2022-09-02 09:34:03 +00:00
|
|
|
})
|
2022-09-17 09:35:03 +00:00
|
|
|
.catch(console.error);
|
|
|
|
}, []);
|
2022-09-02 09:34:03 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!client) return;
|
2022-09-17 09:34:03 +00:00
|
|
|
client.on("auth_response", ({ params }) => {
|
|
|
|
if ("code" in params) {
|
|
|
|
console.error(params);
|
|
|
|
return;
|
2022-09-02 09:34:03 +00:00
|
|
|
}
|
2022-09-17 09:34:03 +00:00
|
|
|
if ("error" in params) {
|
|
|
|
console.error(params.error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setAddress(params.result.p.iss.split(":")[4]);
|
2022-09-02 09:34:03 +00:00
|
|
|
});
|
|
|
|
}, [client]);
|
|
|
|
|
2022-09-13 09:43:58 +00:00
|
|
|
const [view, changeView] = useState<"default" | "qr" | "signedIn">("default");
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (uri) changeView("qr");
|
|
|
|
}, [uri, changeView]);
|
|
|
|
|
2022-09-02 09:34:03 +00:00
|
|
|
useEffect(() => {
|
2022-09-13 09:43:58 +00:00
|
|
|
if (address) changeView("signedIn");
|
|
|
|
}, [address, changeView]);
|
|
|
|
|
2022-09-02 09:34:03 +00:00
|
|
|
return (
|
2022-09-13 09:43:58 +00:00
|
|
|
<Box width="100%" height="100%">
|
2022-09-17 09:35:03 +00:00
|
|
|
{view === "default" && (
|
|
|
|
<DefaultView onClick={onSignIn} hasInitialized={hasInitialized} />
|
|
|
|
)}
|
2022-09-13 09:43:58 +00:00
|
|
|
{view === "qr" && <QrView uri={uri} />}
|
|
|
|
{view === "signedIn" && <SignedInView address={address} />}
|
|
|
|
</Box>
|
2022-09-02 09:34:03 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Home;
|