"use client" import { useState, useEffect } from "react" import { Button } from "./ui/button" import { Dialog, DialogContent } from "./ui/dialog" import { Input } from "./ui/input" import { Label } from "./ui/label" import { Shield, Lock, User, AlertCircle } from "lucide-react" import { getApiUrl } from "../lib/api-config" interface AuthSetupProps { onComplete: () => void } export function AuthSetup({ onComplete }: AuthSetupProps) { const [open, setOpen] = useState(false) const [step, setStep] = useState<"choice" | "setup">("choice") const [username, setUsername] = useState("") const [password, setPassword] = useState("") const [confirmPassword, setConfirmPassword] = useState("") const [error, setError] = useState("") const [loading, setLoading] = useState(false) useEffect(() => { const checkOnboardingStatus = async () => { try { const response = await fetch(getApiUrl("/api/auth/status")) const data = await response.json() console.log("[v0] Auth status for modal check:", data) // Show modal if auth is not configured and not declined if (!data.auth_configured) { setTimeout(() => setOpen(true), 500) } } catch (error) { console.error("[v0] Failed to check auth status:", error) // Fail-safe: show modal if we can't check status setTimeout(() => setOpen(true), 500) } } checkOnboardingStatus() }, []) const handleSkipAuth = async () => { setLoading(true) setError("") try { console.log("[v0] Skipping authentication setup...") const response = await fetch(getApiUrl("/api/auth/skip"), { method: "POST", headers: { "Content-Type": "application/json" }, }) const data = await response.json() console.log("[v0] Auth skip response:", data) if (!response.ok) { throw new Error(data.error || "Failed to skip authentication") } if (data.auth_declined) { console.log("[v0] Authentication skipped successfully - APIs should be accessible without token") } console.log("[v0] Authentication skipped successfully") localStorage.setItem("proxmenux-auth-declined", "true") localStorage.removeItem("proxmenux-auth-token") // Remove any old token setOpen(false) onComplete() } catch (err) { console.error("[v0] Auth skip error:", err) setError(err instanceof Error ? err.message : "Failed to save preference") } finally { setLoading(false) } } const handleSetupAuth = async () => { setError("") if (!username || !password) { setError("Please fill in all fields") return } if (password !== confirmPassword) { setError("Passwords do not match") return } if (password.length < 6) { setError("Password must be at least 6 characters") return } setLoading(true) try { console.log("[v0] Setting up authentication...") const response = await fetch(getApiUrl("/api/auth/setup"), { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ username, password, }), }) const data = await response.json() console.log("[v0] Auth setup response:", data) if (!response.ok) { throw new Error(data.error || "Failed to setup authentication") } if (data.token) { localStorage.setItem("proxmenux-auth-token", data.token) localStorage.removeItem("proxmenux-auth-declined") console.log("[v0] Authentication setup successful") } setOpen(false) onComplete() } catch (err) { console.error("[v0] Auth setup error:", err) setError(err instanceof Error ? err.message : "Failed to setup authentication") } finally { setLoading(false) } } return ( {step === "choice" ? (

Protect Your Dashboard?

Add an extra layer of security to protect your Proxmox data when accessing from non-private networks.

You can always enable this later in Settings

) : (

Setup Authentication

Create a username and password to protect your dashboard

{error && (

{error}

)}
setUsername(e.target.value)} className="pl-10 text-base" disabled={loading} autoComplete="username" />
setPassword(e.target.value)} className="pl-10 text-base" disabled={loading} autoComplete="new-password" />
setConfirmPassword(e.target.value)} className="pl-10 text-base" disabled={loading} autoComplete="new-password" />
)}
) }