"use client"; import { LegalAndSupportSettings } from "@zitadel/server"; import { useState } from "react"; import { Button, ButtonVariants } from "./Button"; import { TextInput } from "./Input"; import { PrivacyPolicyCheckboxes } from "./PrivacyPolicyCheckboxes"; import { FieldValues, useForm } from "react-hook-form"; import { useRouter } from "next/navigation"; import { Spinner } from "./Spinner"; import AuthenticationMethodRadio, { methods, } from "./AuthenticationMethodRadio"; import Alert from "./Alert"; type Inputs = | { firstname: string; lastname: string; email: string; } | FieldValues; type Props = { legal: LegalAndSupportSettings; organization?: string; authRequestId?: string; }; export default function RegisterFormWithoutPassword({ legal, organization, authRequestId, }: Props) { const { register, handleSubmit, formState } = useForm({ mode: "onBlur", }); const [loading, setLoading] = useState(false); const [selected, setSelected] = useState(methods[0]); const [error, setError] = useState(""); const router = useRouter(); async function submitAndRegister(values: Inputs) { setLoading(true); const res = await fetch("/api/registeruser", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ email: values.email, firstName: values.firstname, lastName: values.lastname, organization: organization, }), }); setLoading(false); if (!res.ok) { const error = await res.json(); throw new Error(error.details); } return res.json(); } async function createSessionWithLoginName(loginName: string) { setLoading(true); const res = await fetch("/api/session", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ loginName: loginName, organization: organization, authRequestId: authRequestId, }), }); setLoading(false); if (!res.ok) { throw new Error("Failed to set user"); } return res.json(); } async function submitAndContinue( value: Inputs, withPassword: boolean = false ) { const registerParams: any = value; if (organization) { registerParams.organization = organization; } if (authRequestId) { registerParams.authRequestId = authRequestId; } return withPassword ? router.push(`/register?` + new URLSearchParams(registerParams)) : submitAndRegister(value) .then((resp: any) => { createSessionWithLoginName(value.email).then(({ factors }) => { setError(""); const params: any = { loginName: factors.user.loginName }; if (organization) { params.organization = organization; } if (authRequestId) { params.authRequestId = authRequestId; } return router.push(`/passkey/add?` + new URLSearchParams(params)); }); }) .catch((errorDetails: Error) => { setLoading(false); setError(errorDetails.message); }); } const { errors } = formState; const [tosAndPolicyAccepted, setTosAndPolicyAccepted] = useState(false); return (
{legal && ( )}

Select the method you would like to authenticate

{error && (
{error}
)}
); }