"use client"; import { resetPassword, sendPassword } from "@/lib/server/password"; import { create } from "@zitadel/client"; import { ChecksSchema } from "@zitadel/proto/zitadel/session/v2/session_service_pb"; import { LoginSettings } from "@zitadel/proto/zitadel/settings/v2/login_settings_pb"; import { useTranslations } from "next-intl"; import { useRouter } from "next/navigation"; import { useState } from "react"; import { useForm } from "react-hook-form"; import { Alert, AlertType } from "./alert"; import { BackButton } from "./back-button"; import { Button, ButtonVariants } from "./button"; import { TextInput } from "./input"; import { Spinner } from "./spinner"; type Inputs = { password: string; }; type Props = { loginSettings: LoginSettings | undefined; loginName: string; organization?: string; authRequestId?: string; isAlternative?: boolean; // whether password was requested as alternative auth method promptPasswordless?: boolean; }; export function PasswordForm({ loginSettings, loginName, organization, authRequestId, promptPasswordless, isAlternative, }: Props) { const t = useTranslations("password"); const { register, handleSubmit, formState } = useForm({ mode: "onBlur", }); const [info, setInfo] = useState(""); const [error, setError] = useState(""); const [loading, setLoading] = useState(false); const router = useRouter(); async function submitPassword(values: Inputs) { setError(""); setLoading(true); const response = await sendPassword({ loginName, organization, checks: create(ChecksSchema, { password: { password: values.password }, }), authRequestId, }) .catch(() => { setError("Could not verify password"); return; }) .finally(() => { setLoading(false); }); if (response && "error" in response && response.error) { setError(response.error); return; } if (response && "redirect" in response && response.redirect) { return router.push(response.redirect); } } async function resetPasswordAndContinue() { setError(""); setInfo(""); setLoading(true); const response = await resetPassword({ loginName, organization, authRequestId, }) .catch(() => { setError("Could not reset password"); return; }) .finally(() => { setLoading(false); }); if (response && "error" in response) { setError(response.error); return; } setInfo("Password was reset. Please check your email."); const params = new URLSearchParams({ loginName: loginName, }); if (organization) { params.append("organization", organization); } if (authRequestId) { params.append("authRequestId", authRequestId); } return router.push("/password/set?" + params); } return (
{!loginSettings?.hidePasswordReset && ( )} {loginName && ( )}
{info && (
{info}
)} {error && (
{error}
)}
); }