import { getCsrfToken, signIn, useSession } from "next-auth/react"
import { SiwaMessage } from "@avmkit/siwa"
import { useEffect, useState } from "react"
import Layout from "../components/layout"
function Siwa() {
const [address, setAddress] = useState("")
const { data: session, status } = useSession()
const handleLogin = async () => {
try {
const callbackUrl = "/protected"
const message = new SiwaMessage({
domain: window.location.host,
address: address,
statement: "Sign in with Algorand to the app.",
uri: window.location.origin,
version: "1",
chainId: "416001", // Algorand TestNet
nonce: await getCsrfToken(),
})
// Here, you would use your Algorand wallet library to sign the message
// For example, with Pera:
// const signMessageWithPera = async (message, address) => {
// const encodedMessage = prepareMessage(message);
// const signatureArray = await peraWallet.signData(
// [{ data: encodedMessage, message: "" }],
// address );
// return signatureArray[0]; // Returns the signature
// };
// For this example, we'll use a placeholder signature
const signature = "placeholder_signature"
signIn("credentials", {
message: JSON.stringify(message),
redirect: false,
signature,
callbackUrl,
})
} catch (error) {
console.error(error)
alert("An error occurred during sign-in")
}
}
return (
<Layout>
<h1>Sign-In with Algorand</h1>
<input
type="text"
placeholder="Enter Algorand address"
value={address}
onChange={(e) => setAddress(e.target.value)}
/>
<button onClick={handleLogin}>Sign-in with Algorand</button>
</Layout>
)
}
export default Siwa