import { useRouter } from "next/router"; import { PrivyClient } from "@privy-io/server-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 { redirect: { destination: "/onboarding/delegate", permanent: false, }, }; } } 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 { redirect: { destination: "/onboarding/delegate", permanent: false, }, }; } } } catch (error) { console.error("Auth verification failed", error); return { redirect: { destination: "/onboarding/login", permanent: false, }, }; } }; export default function Onboarding() { const router = useRouter(); return
Redirecting...
; }