"use client"; import { useEffect, useState } from "react"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { confirmAttachAuth } from "@/lib/api"; import { docsUrl } from "@/lib/docs"; const ATTACH_AUTH_STORAGE_KEY = "happyview_attach_auth"; const ATTACH_AUTH_MAX_AGE_MS = 10 * 60 * 1000; interface AttachAuthPayload { attachedDid: string; originalDid: string; timestamp?: number; } interface SetupAttachAuthProps { attachedDid: string; attachedHandle: string | null; onComplete: () => void; onBack?: () => void; } export function SetupAttachAuth({ attachedDid, attachedHandle, onComplete, onBack, }: SetupAttachAuthProps) { const [confirming, setConfirming] = useState(false); const [error, setError] = useState(null); useEffect(() => { const stored = localStorage.getItem(ATTACH_AUTH_STORAGE_KEY); if (!stored) return; let payload: AttachAuthPayload; try { payload = JSON.parse(stored) as AttachAuthPayload; } catch { localStorage.removeItem(ATTACH_AUTH_STORAGE_KEY); return; } if (payload.attachedDid !== attachedDid) { localStorage.removeItem(ATTACH_AUTH_STORAGE_KEY); return; } if ( payload.timestamp && Date.now() - payload.timestamp > ATTACH_AUTH_MAX_AGE_MS ) { localStorage.removeItem(ATTACH_AUTH_STORAGE_KEY); setError("Your sign-in session expired. Please authenticate again."); return; } localStorage.removeItem(ATTACH_AUTH_STORAGE_KEY); setConfirming(true); confirmAttachAuth({ original_did: payload.originalDid }) .then(() => onComplete()) .catch((e) => { setError( e instanceof Error ? e.message : "Failed to restore admin session. Try authenticating again.", ); setConfirming(false); }); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); function handleAuthenticate() { setConfirming(true); setError(null); fetch("/auth/me", { credentials: "same-origin" }) .then((res) => { if (!res.ok) throw new Error("Failed to fetch current user"); return res.json() as Promise<{ did: string }>; }) .then(({ did: originalDid }) => { const payload: AttachAuthPayload = { attachedDid, originalDid, timestamp: Date.now(), }; localStorage.setItem(ATTACH_AUTH_STORAGE_KEY, JSON.stringify(payload)); const handle = attachedHandle ?? attachedDid; return fetch(`/auth/login?handle=${encodeURIComponent(handle)}&scope=${encodeURIComponent("atproto identity:*")}&redirect_uri=${encodeURIComponent("/setup")}`, { credentials: "same-origin", }); }) .then((resp) => { if (!resp.ok) throw new Error("Login request failed"); return resp.json() as Promise<{ url: string }>; }) .then(({ url }) => { window.location.href = url; }) .catch((e) => { setError( e instanceof Error ? e.message : "Failed to start authentication. Check your connection and try again.", ); setConfirming(false); }); } const displayName = attachedHandle ? `@${attachedHandle}` : attachedDid; return ( Sign in to verify ownership {/* You'll be redirected to sign in as{" "} {displayName}, then returned here automatically.
*/}

You'll leave this page briefly to authenticate through the account's data server. Once verified, your admin session will be restored and you'll continue from where you left off.
Learn more

{error && (

{error}

)} {confirming ? (

Restoring admin session…

) : (
{onBack ? ( ) : (
)}
)} ); }