import { Button, Container, Text, Divider, Flex, Heading, Image, SimpleGrid, useToast, } from "@chakra-ui/react"; import AuthClient from "@walletconnect/auth-client"; import type { NextPage } from "next"; import Link from "next/link"; import Qrcode from "qrcode"; import { Fragment, useCallback, useEffect, useRef, useState } from "react"; const Home: NextPage = () => { const [client, setClient] = useState(); const [uri, setUri] = useState(""); const [accepted, setAccepted] = useState(false); const toast = useToast(); const canvasRef = useRef(null); const onSignIn = useCallback(() => { if (!client) return; client .request({ aud: "http://localhost:3000/", domain: "localhost:3000", chainId: "eip191:1", type: "eip4361", nonce: "nonce", 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", projectId: process.env.NEXT_PUBLIC_PROJECT_ID, metadata: { name: "react-dapp-auth", description: "React Example Dapp for Auth", url: window.location.host, icons: [], }, }) .then((v) => { setClient(v); }) .catch(console.log); }, [setClient]); useEffect(() => { if (!client) return; client.on("auth_response", (res) => { if (res.params.code !== -1) { setAccepted(true); } }); }, [client]); useEffect(() => { if (uri && canvasRef.current) Qrcode.toCanvas(canvasRef.current, uri); }, [uri, canvasRef]); return ( Sign In Initiate the auth cycle by sending an auth request
{uri && ( { navigator.clipboard.writeText(uri).then(() => { toast({ title: "URI copied to clipboard", status: "success", duration: 1000, }); }); }} ref={canvasRef} /> (You can copy the URI by clicking the QR code above) )}
{accepted && ( )}
); }; export default Home;