"use client"; import { useSession } from "next-auth/react"; import { useRouter, useSearchParams } from "next/navigation"; import React, { useEffect, Suspense } from "react"; import AuthScreen from "./AuthScreen"; function AuthFlowInner({ mode }: { mode: "signin" | "signup" }) { const { data: session, status } = useSession(); const router = useRouter(); const searchParams = useSearchParams(); const [ssoProcessing, setSsoProcessing] = React.useState(false); const [ssoToken, setSsoToken] = React.useState(null); const [routing, setRouting] = React.useState(false); useEffect(() => { if (status === "authenticated" && session?.user?.email) { setRouting(true); const isVibnCodeSSO = searchParams?.get("vibncode") === "true"; if (isVibnCodeSSO) { setSsoProcessing(true); // Call our secure token endpoint fetch("/api/auth/token") .then((r) => r.json()) .then((data) => { if (data.token) { setSsoToken(data.token); // Deep-link redirect back to the VibnCode desktop app window.location.href = `vibncode://auth/callback?token=${data.token}`; } else { console.error("SSO Token missing from response", data); setSsoProcessing(false); } }) .catch((err) => { console.error("Desktop SSO failed:", err); setSsoProcessing(false); }); return; } // Resolve the user's ACTUAL workspace slug (it can differ from their // email — e.g. after they renamed it in onboarding) and decide where to // land. Onboarding shows only once: gate on the onboardingComplete flag, // with project count as a fallback for users who onboarded before the // flag existed. Promise.all([ fetch("/api/workspaces") .then((r) => r.json()) .catch(() => ({})), fetch("/api/projects") .then((r) => r.json()) .catch(() => ({})), ]).then(([ws, proj]) => { const slug: string | undefined = ws?.workspaces?.[0]?.slug; const onboarded = ws?.onboarded === true; const hasProjects = Array.isArray(proj?.projects) && proj.projects.length > 0; if (slug && (onboarded || hasProjects)) { router.push(`/${slug}/projects`); } else { router.push("/onboarding"); } }); } }, [status, session, router, searchParams]); if (status === "loading" || ssoProcessing || routing) { return (
{ssoToken ? (

Authentication Successful

Signed in. Redirecting to VibnCode...

If the app doesn't open automatically, copy your Workspace API Key below and paste it into the connection card.

{ssoToken}
) : (
Checking session
)}
); } return ; } export default function AuthFlow({ mode }: { mode: "signin" | "signup" }) { return ( ); }