"use client"; import { handleServerActionResponse } from "@/lib/client-utils"; import { registerUser } from "@/lib/server/register"; import { LegalAndSupportSettings } from "@zitadel/proto/zitadel/settings/v2/legal_settings_pb"; import { LoginSettings, PasskeysType } from "@zitadel/proto/zitadel/settings/v2/login_settings_pb"; import { useTranslations } from "next-intl"; import { useRouter } from "next/navigation"; import { useState } from "react"; import { FieldValues, useForm } from "react-hook-form"; import { Alert, AlertType } from "./alert"; import { AuthenticationMethod, AuthenticationMethodRadio, methods } from "./authentication-method-radio"; import { AutoSubmitForm } from "./auto-submit-form"; import { BackButton } from "./back-button"; import { Button, ButtonVariants } from "./button"; import { TextInput } from "./input"; import { PrivacyPolicyCheckboxes } from "./privacy-policy-checkboxes"; import { Spinner } from "./spinner"; import { Translated } from "./translated"; type Inputs = | { firstname: string; lastname: string; email: string; } | FieldValues; type Props = { legal: LegalAndSupportSettings; firstname?: string; lastname?: string; email?: string; organization: string; requestId?: string; loginSettings?: LoginSettings; idpCount: number; }; export function RegisterForm({ legal, email, firstname, lastname, organization, requestId, loginSettings, idpCount = 0, }: Props) { const { register, handleSubmit, formState } = useForm({ mode: "onChange", defaultValues: { email: email ?? "", firstname: firstname ?? "", lastname: lastname ?? "", }, }); const t = useTranslations("register"); const [loading, setLoading] = useState(false); const [selected, setSelected] = useState(methods[0]); const [error, setError] = useState(""); const [samlData, setSamlData] = useState<{ url: string; fields: Record } | null>(null); const router = useRouter(); async function submitAndRegister(values: Inputs) { setLoading(true); try { const response = await registerUser({ email: values.email, firstName: values.firstname, lastName: values.lastname, organization: organization, requestId: requestId, }); handleServerActionResponse(response, router, setSamlData, setError); return response; } catch { setError(t("errors.couldNotRegisterUser")); } finally { setLoading(false); } } async function submitAndContinue(value: Inputs, withPassword: boolean = false) { const registerParams: any = value; if (organization) { registerParams.organization = organization; } if (requestId) { registerParams.requestId = requestId; } // redirect user to /register/password if password is chosen if (withPassword) { return router.push(`/register/password?` + new URLSearchParams(registerParams)); } else { return submitAndRegister(value); } } const { errors } = formState; const [tosAndPolicyAccepted, setTosAndPolicyAccepted] = useState(false); // Check if legal acceptance is required const isLegalAcceptanceRequired = !!(legal?.tosLink || legal?.privacyPolicyLink); const canSubmit = formState.isValid && (!isLegalAcceptanceRequired || tosAndPolicyAccepted); return ( <> {samlData && }
{(legal?.tosLink || legal?.privacyPolicyLink) && ( )} {/* show chooser if both methods are allowed */} {loginSettings && loginSettings.allowLocalAuthentication && loginSettings.passkeysType == PasskeysType.ALLOWED && ( <>

)} {!loginSettings?.allowLocalAuthentication && loginSettings?.passkeysType !== PasskeysType.ALLOWED && (!loginSettings?.allowExternalIdp || !idpCount) && (
)} {error && (
{error}
)}
); }