import { useState } from "react"; import { Link, useNavigate } from "react-router-dom"; import { Mail, Lock, User, ArrowRight, ArrowLeft } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { PasswordStrengthMeter, isPasswordValid } from "@/components/auth/PasswordStrengthMeter"; import { BannerAlert } from "@/components/auth/BannerAlert"; import { api, ApiError, tokenManager } from "@/lib/api"; type RegistrationState = "form" | "disabled"; export default function RegisterPage() { const navigate = useNavigate(); const [name, setName] = useState(""); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const [state, setState] = useState("form"); const passwordsMatch = password === confirmPassword; const canSubmit = name.trim() && email.trim() && isPasswordValid(password) && passwordsMatch; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(null); if (!passwordsMatch) { setError("Passwords do not match"); return; } if (!isPasswordValid(password)) { setError("Password does not meet requirements"); return; } setIsLoading(true); try { const response = await api.auth.register(email, password, name.trim() || undefined); // Store the session token so ProtectedLayout lets the user through if (response.token) { tokenManager.setToken(response.token, response.expires_at ?? null); } // Navigate to org-setup so the user can name their org or accept an invite navigate("/org-setup", { state: { pendingInvites: response.pending_invites ?? [], isFirstUser: response.is_first_user ?? false, }, }); } catch (err) { if (err instanceof ApiError) { if (err.code === 409) { setError("An account with this email already exists."); } else if (err.code === 403 || (err.message && err.message.toLowerCase().includes("disabled"))) { setState("disabled"); } else { setError(err.message || "An error occurred. Please try again."); } } else { setError("An error occurred. Please try again."); } } finally { setIsLoading(false); } }; // Registration disabled state if (state === "disabled") { return (

Registration unavailable

New account registration is currently invite-only. Please contact your administrator for an invitation.

); } // Registration form return (

Create your account

Get started with Secuird in seconds

{error && ( )}
setName(e.target.value)} className="pl-10" required autoComplete="name" />
setEmail(e.target.value)} className="pl-10" required autoComplete="email" />
setPassword(e.target.value)} className="pl-10" required autoComplete="new-password" />
setConfirmPassword(e.target.value)} className="pl-10" required autoComplete="new-password" />
{confirmPassword && !passwordsMatch && (

Passwords do not match

)}

Already have an account?{" "} Sign in

By creating an account, you agree to our{" "} Terms of Service {" "} and{" "} Privacy Policy

); }