admin / Synapse-Sonar
publicAttack Surface Simulation
Synapse-Sonar / synapse-sonar / components / SetupWizard.tsx
7156 B · main
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | "use client"; import { useEffect, useState } from "react"; import { useRouter } from "next/navigation"; import Logo from "@/components/Logo"; export default function SetupWizard() { const router = useRouter(); const [checking, setChecking] = useState(true); const [alreadyDone, setAlreadyDone] = useState(false); const [submitting, setSubmitting] = useState(false); const [error, setError] = useState<string | null>(null); const [done, setDone] = useState(false); const [form, setForm] = useState({ username: "", name: "", password: "", confirm: "", }); // Gate: if the system is already initialized, bounce to login. useEffect(() => { fetch("/api/setup") .then((r) => r.json()) .then((d) => { if (!d.needsSetup) setAlreadyDone(true); }) .finally(() => setChecking(false)); }, []); const set = (k: keyof typeof form, v: string) => setForm((f) => ({ ...f, [k]: v })); const valid = /^[a-zA-Z0-9._-]{3,40}$/.test(form.username) && form.password.length >= 10 && form.password === form.confirm; const submit = async () => { setSubmitting(true); setError(null); try { const res = await fetch("/api/setup", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ username: form.username, name: form.name || undefined, password: form.password, }), }); const data = await res.json(); if (!res.ok) { setError(data.error ?? "Setup failed"); return; } setDone(true); } catch { setError("Network error"); } finally { setSubmitting(false); } }; if (checking) return <Centered>Initializing…</Centered>; if (alreadyDone) { return ( <Centered> <Card> <Brand /> <h1 className="mt-4 text-center text-xl font-semibold text-slate-100"> Already initialized </h1> <p className="mt-2 text-center text-sm text-slate-400"> Synapse Sonar has already been set up on this instance. </p> <button onClick={() => router.push("/login")} className="mt-6 w-full rounded-lg bg-synapse-cyan px-4 py-2.5 text-sm font-semibold text-[#05080f] transition hover:brightness-110" > Go to sign in </button> </Card> </Centered> ); } return ( <Centered> <Card> <Brand /> {done ? ( <div className="mt-6 text-center"> <div className="mx-auto flex h-12 w-12 items-center justify-center rounded-full bg-emerald-500/15 text-2xl text-emerald-300"> ✓ </div> <p className="mt-3 text-sm text-slate-300"> Admin account{" "} <span className="font-mono text-synapse-cyan">{form.username}</span>{" "} created. Sign in to continue. </p> <button onClick={() => router.push("/login")} className="mt-6 w-full rounded-lg bg-synapse-cyan px-4 py-2.5 text-sm font-semibold text-[#05080f] transition hover:brightness-110" > Continue to sign in </button> </div> ) : ( <> <h1 className="mt-6 text-center text-xl font-semibold text-slate-100"> Create your admin account </h1> <p className="mt-1 text-center text-sm text-slate-400"> Set a username and password to get started. </p> {error && ( <div className="mt-4 rounded-lg border border-rose-500/40 bg-rose-500/10 px-3 py-2 text-xs text-rose-300"> {error} </div> )} <div className="mt-5 space-y-4"> <Field label="Username" hint="3–40 chars: letters, numbers, . _ -"> <input autoFocus value={form.username} onChange={(e) => set("username", e.target.value)} placeholder="admin" className={`${inputCls} font-mono`} /> </Field> <Field label="Display name (optional)"> <input value={form.name} onChange={(e) => set("name", e.target.value)} placeholder="Jane Doe" className={inputCls} /> </Field> <Field label="Password" hint="Minimum 10 characters"> <input type="password" value={form.password} onChange={(e) => set("password", e.target.value)} placeholder="••••••••••" className={inputCls} /> </Field> <Field label="Confirm password"> <input type="password" value={form.confirm} onChange={(e) => set("confirm", e.target.value)} placeholder="••••••••••" className={inputCls} /> {form.confirm.length > 0 && form.password !== form.confirm && ( <span className="mt-1 block text-[11px] text-rose-400"> Passwords don’t match </span> )} </Field> <button disabled={!valid || submitting} onClick={submit} className="w-full rounded-lg bg-synapse-cyan px-4 py-2.5 text-sm font-semibold text-[#05080f] transition hover:brightness-110 disabled:opacity-40" > {submitting ? "Creating…" : "Create admin account"} </button> </div> </> )} </Card> </Centered> ); } const inputCls = "w-full rounded-lg border border-slate-600/60 bg-[#05080f] px-3 py-2 text-sm text-slate-100 outline-none transition placeholder:text-slate-600 focus:border-synapse-cyan"; function Field({ label, hint, children }: { label: string; hint?: string; children: React.ReactNode }) { return ( <div> <label className="mb-1.5 block text-xs font-medium text-slate-300">{label}</label> {children} {hint && <span className="mt-1 block text-[11px] text-slate-500">{hint}</span>} </div> ); } function Brand() { return ( <div className="flex justify-center"> <Logo variant="full" className="h-24 drop-shadow-[0_0_24px_rgba(59,130,246,0.35)]" /> </div> ); } function Card({ children }: { children: React.ReactNode }) { return ( <div className="w-full max-w-md rounded-2xl border border-slate-700/60 bg-gradient-to-b from-[#1e293b] to-[#0f172a] p-8 shadow-2xl shadow-cyan-500/5"> {children} </div> ); } function Centered({ children }: { children: React.ReactNode }) { return ( <div className="flex min-h-screen items-center justify-center bg-[#05080f] px-4 text-slate-300"> {children} </div> ); } |