wallet-connect-web-examples/dapps/react-dapp-auth/pages/index.tsx
Celine Sarafa b45f4369c0
Use standalone modal (#79)
* Use standalone modal

* Set project id

* chore(deps): update deps

* chore(w3m-standalone): use web3modal standalone with auth-client

* fix(deps): update web3modal to v2.2.2

* fix(typescript): revert to 4.9.5

* chore(remove-deps): remove unused dependencies and global declaration

---------

Co-authored-by: Cali93 <armut.cumaali@gmail.com>
2023-03-28 09:58:18 +03:00

111 lines
2.9 KiB
TypeScript

import { Box } from "@chakra-ui/react";
import AuthClient, { generateNonce } from "@walletconnect/auth-client";
import { Web3Modal } from "@web3modal/standalone";
import type { NextPage } from "next";
import { useCallback, useEffect, useState } from "react";
import DefaultView from "../views/DefaultView";
import SignedInView from "../views/SignedInView";
// 1. Get projectID at https://cloud.walletconnect.com
const projectId = process.env.NEXT_PUBLIC_PROJECT_ID;
if (!projectId) {
throw new Error("You need to provide NEXT_PUBLIC_PROJECT_ID env variable");
}
// 2. Configure web3Modal
const web3Modal = new Web3Modal({
projectId,
walletConnectVersion: 2,
});
const Home: NextPage = () => {
const [client, setClient] = useState<AuthClient | null>();
const [hasInitialized, setHasInitialized] = useState(false);
const [uri, setUri] = useState<string>("");
const [address, setAddress] = useState<string>("");
const onSignIn = useCallback(() => {
if (!client) return;
client
.request({
aud: window.location.href,
domain: window.location.hostname.split(".").slice(-2).join("."),
chainId: "eip155:1",
type: "eip4361",
nonce: generateNonce(),
statement: "Sign in with wallet.",
})
.then(({ uri }) => {
if (uri) {
setUri(uri);
}
});
}, [client, setUri]);
useEffect(() => {
AuthClient.init({
relayUrl:
process.env.NEXT_PUBLIC_RELAY_URL || "wss://relay.walletconnect.com",
projectId: process.env.NEXT_PUBLIC_PROJECT_ID!,
metadata: {
name: "react-dapp-auth",
description: "React Example Dapp for Auth",
url: window.location.host,
icons: [],
},
})
.then((authClient) => {
setClient(authClient);
setHasInitialized(true);
})
.catch(console.error);
}, []);
useEffect(() => {
if (!client) return;
client.on("auth_response", ({ params }) => {
if ("code" in params) {
console.error(params);
return;
}
if ("error" in params) {
console.error(params.error);
return;
}
setAddress(params.result.p.iss.split(":")[4]);
});
}, [client]);
const [view, changeView] = useState<"default" | "qr" | "signedIn">("default");
useEffect(() => {
async function handleOpenModal() {
if (uri) {
await web3Modal.openModal({
uri,
standaloneChains: ["eip155:1"],
});
}
}
handleOpenModal();
}, [uri]);
useEffect(() => {
if (address) {
web3Modal.closeModal();
changeView("signedIn");
}
}, [address, changeView]);
return (
<Box width="100%" height="100%">
{view === "default" && (
<DefaultView onClick={onSignIn} hasInitialized={hasInitialized} />
)}
{view === "signedIn" && <SignedInView address={address} />}
</Box>
);
};
export default Home;