2022-09-02 09:34:03 +00:00
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
Container,
|
|
|
|
Text,
|
|
|
|
Divider,
|
|
|
|
Flex,
|
|
|
|
Heading,
|
|
|
|
Image,
|
|
|
|
SimpleGrid,
|
|
|
|
useToast,
|
|
|
|
} from "@chakra-ui/react";
|
|
|
|
import AuthClient 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";
|
|
|
|
import Link from "next/link";
|
|
|
|
import Qrcode from "qrcode";
|
2022-09-05 07:50:21 +00:00
|
|
|
import { Fragment, useCallback, useEffect, useRef, useState } from "react";
|
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>();
|
|
|
|
const [uri, setUri] = useState<string>("");
|
|
|
|
const [accepted, setAccepted] = useState<boolean>(false);
|
|
|
|
const toast = useToast();
|
|
|
|
const canvasRef = useRef(null);
|
|
|
|
|
|
|
|
const onSignIn = useCallback(() => {
|
|
|
|
if (!client) return;
|
|
|
|
client
|
|
|
|
.request({
|
|
|
|
aud: "http://localhost:3000/",
|
|
|
|
domain: "localhost:3000",
|
2022-09-06 13:24:29 +00:00
|
|
|
chainId: "1",
|
2022-09-02 09:34:03 +00:00
|
|
|
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",
|
2022-09-05 12:17:24 +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-02 09:34:03 +00:00
|
|
|
})
|
|
|
|
.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 (
|
|
|
|
<Container>
|
|
|
|
<Flex gap={20} height={"100%"} direction="column">
|
|
|
|
<Flex
|
|
|
|
boxShadow={"inner"}
|
|
|
|
p={5}
|
|
|
|
backgroundColor={"gray.50"}
|
|
|
|
direction="column"
|
|
|
|
justifyContent="space-evenly"
|
|
|
|
borderRadius={10}
|
|
|
|
border={"solid 2px black"}
|
|
|
|
>
|
|
|
|
<Heading color={"black"} textAlign="center" mb={5}>
|
|
|
|
Sign In
|
|
|
|
</Heading>
|
|
|
|
<Divider mb={5}></Divider>
|
|
|
|
<Text color={"black"} textAlign="center" padding={5}>
|
|
|
|
Initiate the auth cycle by sending an auth request
|
|
|
|
</Text>
|
|
|
|
<form>
|
|
|
|
<Container></Container>
|
|
|
|
<Flex alignItems={"center"} gap={4} direction="column">
|
|
|
|
<Button
|
|
|
|
p={2}
|
|
|
|
color={"black"}
|
|
|
|
onClick={onSignIn}
|
|
|
|
leftIcon={<Image width={10} src="/walletconnect.png" />}
|
|
|
|
>
|
|
|
|
Sign in with WalletConnect
|
|
|
|
</Button>
|
|
|
|
{uri && (
|
2022-09-05 07:50:21 +00:00
|
|
|
<Fragment>
|
|
|
|
<canvas
|
|
|
|
onClick={() => {
|
|
|
|
navigator.clipboard.writeText(uri).then(() => {
|
|
|
|
toast({
|
|
|
|
title: "URI copied to clipboard",
|
|
|
|
status: "success",
|
|
|
|
duration: 1000,
|
|
|
|
});
|
2022-09-02 09:34:03 +00:00
|
|
|
});
|
2022-09-05 07:50:21 +00:00
|
|
|
}}
|
|
|
|
ref={canvasRef}
|
|
|
|
/>
|
|
|
|
<Text
|
|
|
|
color="gray.400"
|
|
|
|
style={{ fontStyle: "italic", fontSize: "0.75em" }}
|
|
|
|
>
|
|
|
|
(You can copy the URI by clicking the QR code above)
|
|
|
|
</Text>
|
|
|
|
</Fragment>
|
2022-09-02 09:34:03 +00:00
|
|
|
)}
|
|
|
|
</Flex>
|
|
|
|
</form>
|
|
|
|
{accepted && (
|
|
|
|
<Button
|
|
|
|
_hover={{ cursor: "pointer" }}
|
|
|
|
p={10}
|
|
|
|
colorScheme="green"
|
|
|
|
isActive={false}
|
|
|
|
>
|
|
|
|
Authenticated
|
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
</Flex>
|
|
|
|
</Flex>
|
|
|
|
</Container>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Home;
|