import pool from "@/server/db"; import { PrivyClient } from "@privy-io/server-auth"; import { ulid } from "ulid"; export const getServerSideProps = async ({ req }) => { const cookieAuthToken = req.cookies["privy-id-token"]; if (!cookieAuthToken) { return { redirect: { destination: "/onboarding", 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 userid, publickey, tg_userid, delegated FROM users WHERE userid = $1", [userId]); if (dbRes.rows.length === 0) { return { redirect: { destination: "/onboarding", permanent: false, }, }; } const { delegated } = dbRes.rows[0]; if (!delegated) { return { redirect: { destination: "/onboarding", permanent: false, }, }; } const chatid = ulid() return { redirect: { destination: `/chat/${chatid}`, permanent: false, }, }; } catch (error) { console.error("Auth verification failed", error); return { redirect: { destination: "/onboarding", permanent: false, }, }; } }; export default function Page() { return (
); }