129 lines
3.5 KiB
JavaScript
Raw Normal View History

2025-02-17 15:21:20 +07:00
import { useRouter } from "next/router";
import { PrivyClient } from "@privy-io/server-auth";
import { useDelegatedActions } from "@privy-io/react-auth";
import pool from "@/server/db";
export const getServerSideProps = async ({ req }) => {
const cookieAuthToken = req.cookies["privy-id-token"];
if (!cookieAuthToken) {
return {
redirect: {
destination: "/onboarding/login",
permanent: false,
},
};
}
const PRIVY_APP_ID = process.env.NEXT_PUBLIC_PRIVY_APP_ID;
const PRIVY_APP_SECRET = process.env.PRIVY_APP_SECRET;
const client = new PrivyClient(PRIVY_APP_ID, PRIVY_APP_SECRET);
try {
const claims = await client.verifyAuthToken(cookieAuthToken);
const userId = claims.userId;
const dbRes = await pool.query("SELECT * FROM users WHERE userid = $1", [userId]);
if (dbRes.rows.length === 0) {
const user = await client.getUser(userId);
const privyWallets = user.linkedAccounts.filter(
(account) => account.type === "wallet" && account.walletClientType === "privy" && account.chainType === "solana"
);
const embeddedWallet = privyWallets[0]?.address || null;
const delegated = privyWallets[0]?.delegated || false;
await pool.query(
"INSERT INTO users (userid, publickey, last_signedin_at, created_at, delegated) VALUES ($1, $2, NOW(), NOW(), $3)",
[userId, embeddedWallet, delegated]
);
if (delegated) {
return {
redirect: {
destination: "/chat",
permanent: false,
},
};
} else {
return {
props: { user: { delegated: false, publickey: embeddedWallet } },
};
}
}
const { delegated } = dbRes.rows[0];
if (delegated) {
return {
redirect: {
destination: "/chat",
permanent: false,
},
};
} else {
const user = await client.getUser(userId);
const privyWallets = user.linkedAccounts.filter(
(account) => account.type === "wallet" && account.walletClientType === "privy" && account.chainType === "solana"
);
const updatedDelegated = privyWallets[0]?.delegated || false;
if (updatedDelegated) {
await pool.query("UPDATE users SET delegated = TRUE WHERE userid = $1", [userId]);
return {
redirect: {
destination: "/chat",
permanent: false,
},
};
} else {
return {
props: { user: { delegated: false, publickey: privyWallets[0]?.address || null } },
};
}
}
} catch (error) {
console.error("Auth verification failed", error);
return {
redirect: {
destination: "/onboarding/login",
permanent: false,
},
};
}
};
export default function DelegatePage({ user }) {
const router = useRouter();
const { delegateWallet } = useDelegatedActions();
const handleDelegate = async () => {
try {
if (user.publickey) {
console.log(`Delegating wallet ${user.publickey}...`);
await delegateWallet({
address: user.publickey,
chainType: "solana",
});
router.push("/onboarding");
}
} catch (error) {
console.error("Delegation failed:", error);
}
};
return (
<div>
<h1>Delegate your Wallet</h1>
<p>Wallet: {user.publickey}</p>
<button onClick={handleDelegate} className="bg-violet-600 hover:bg-violet-700 py-3 px-6 text-white rounded-lg">
Delegate
</button>
</div>
);
}