"use client" import * as React from "react" import { Eye, EyeOff, CheckCircle2 } from "lucide-react" import { Logo } from "./logo" import { Button } from "@/components/ui/button" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Alert, AlertDescription } from "@/components/ui/alert" import { ApiClient } from "@/lib/api" import { useAppState } from "@/lib/app-state" import { useToast } from "@/hooks/use-toast" interface SetupFormProps { token: string onSetupComplete: () => void } export function SetupForm({ token, onSetupComplete }: SetupFormProps) { const [email, setEmail] = React.useState("") const [password, setPassword] = React.useState("") const [confirmPassword, setConfirmPassword] = React.useState("") const [showPassword, setShowPassword] = React.useState(false) const [showConfirmPassword, setShowConfirmPassword] = React.useState(false) const [isLoading, setIsLoading] = React.useState(false) const [error, setError] = React.useState("") const [success, setSuccess] = React.useState(false) const { mode } = useAppState() const { toast } = useToast() const apiClient = React.useMemo(() => { return new ApiClient( () => {}, (error) => setError(error), token, ) }, [token]) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setIsLoading(true) setError("") if (password !== confirmPassword) { setError("Passwords do not match") setIsLoading(false) return } if (password.length < 8) { setError("Password must be at least 8 characters long") setIsLoading(false) return } try { // Create admin account using the setup token const response: any = await apiClient.call("/api/v1/sys/admins/setup", { method: "POST", body: JSON.stringify({ email, password, }), }) // Check if response has an error field (ApiClient may return error in catch block) if (response?.error && response.error.length > 0) { throw new Error(response.error) } // If we get here, the account was created successfully setSuccess(true) toast({ title: "Account Created", description: "Your admin account has been created successfully. You can now sign in.", }) // Wait a moment then redirect to login setTimeout(() => { onSetupComplete() }, 2000) } catch (err: any) { // Handle API errors - ApiClient throws errors for failed requests const errorMessage = err.message || "Failed to create admin account" setError(errorMessage) console.error("Setup error:", err) } finally { setIsLoading(false) } } return (
Account created successfully!
Redirecting to login...