admin / Synapse-Sonar
publicAttack Surface Simulation
Synapse-Sonar / synapse-sonar / components / InviteAccept.tsx
7045 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 | "use client"; import { useEffect, useState } from "react"; import { useRouter } from "next/navigation"; import Logo from "@/components/Logo"; interface InviteInfo { username: string; name: string; roleLabel: string; tenantName: string; } export default function InviteAccept({ token }: { token: string }) { const router = useRouter(); const [loading, setLoading] = useState(true); const [info, setInfo] = useState<InviteInfo | null>(null); const [invalid, setInvalid] = useState<string | null>(null); const [name, setName] = useState(""); const [password, setPassword] = useState(""); const [confirm, setConfirm] = useState(""); const [submitting, setSubmitting] = useState(false); const [error, setError] = useState<string | null>(null); const [done, setDone] = useState(false); useEffect(() => { fetch(`/api/invite/${token}`) .then(async (r) => { const data = await r.json(); if (!r.ok) { setInvalid(data.error ?? "Invitation not found"); return; } setInfo(data); setName(data.name ?? ""); }) .catch(() => setInvalid("Could not load invitation")) .finally(() => setLoading(false)); }, [token]); const valid = password.length >= 10 && password === confirm && name.trim().length >= 1; const accept = async () => { setSubmitting(true); setError(null); try { const res = await fetch(`/api/invite/${token}`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ name, password }), }); const data = await res.json(); if (!res.ok) { setError(data.error ?? "Could not accept invitation"); return; } setDone(true); setTimeout( () => router.push(`/login?u=${encodeURIComponent(data.username)}`), 1400 ); } finally { setSubmitting(false); } }; return ( <Centered> <Card> <div className="flex justify-center"> <Logo variant="full" className="h-24 drop-shadow-[0_0_24px_rgba(59,130,246,0.35)]" /> </div> {loading ? ( <p className="mt-6 text-center text-sm text-slate-400">Loading invitation…</p> ) : invalid ? ( <div className="mt-6 text-center"> <div className="mx-auto flex h-12 w-12 items-center justify-center rounded-full bg-rose-500/15 text-2xl text-rose-300"> ✕ </div> <p className="mt-3 text-sm text-slate-300">{invalid}</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> </div> ) : 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"> Account activated. Redirecting you to sign in… </p> </div> ) : ( info && ( <div className="mt-6"> <h1 className="text-center text-xl font-semibold text-slate-100"> Join {info.tenantName} </h1> <p className="mt-1 text-center text-sm text-slate-400"> You’ve been invited as{" "} <span className="text-slate-200">{info.roleLabel}</span>. Set a password to activate your account. </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"> <input value={info.username} disabled className={`${inputCls} cursor-not-allowed font-mono text-slate-400`} /> </Field> <Field label="Your name"> <input autoFocus value={name} onChange={(e) => setName(e.target.value)} placeholder="Jane Doe" className={inputCls} /> </Field> <Field label="Password" hint="Minimum 10 characters"> <input type="password" value={password} onChange={(e) => setPassword(e.target.value)} placeholder="••••••••••" className={inputCls} /> </Field> <Field label="Confirm password"> <input type="password" value={confirm} onChange={(e) => setConfirm(e.target.value)} placeholder="••••••••••" className={inputCls} /> {confirm.length > 0 && password !== confirm && ( <span className="mt-1 block text-[11px] text-rose-400"> Passwords don’t match </span> )} </Field> <button disabled={!valid || submitting} onClick={accept} 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 ? "Activating…" : "Activate account"} </button> </div> </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 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"> {children} </div> ); } |