"use client"; import { PasswordComplexitySettings } from "@zitadel/server"; import PasswordComplexity from "./PasswordComplexity"; import { useState } from "react"; import { Button, ButtonVariants } from "./Button"; import { TextInput } from "./Input"; import { FieldValues, useForm } from "react-hook-form"; import { lowerCaseValidator, numberValidator, symbolValidator, upperCaseValidator, } from "#/utils/validators"; import { useRouter } from "next/navigation"; import { Spinner } from "./Spinner"; import Alert from "./Alert"; import { AuthRequest } from "@zitadel/server"; type Inputs = | { password: string; confirmPassword: string; } | FieldValues; type Props = { passwordComplexitySettings: PasswordComplexitySettings; email: string; firstname: string; lastname: string; organization?: string; authRequestId?: string; }; export default function SetPasswordForm({ passwordComplexitySettings, email, firstname, lastname, organization, authRequestId, }: Props) { const { register, handleSubmit, watch, formState } = useForm({ mode: "onBlur", }); const [loading, setLoading] = useState(false); const [error, setError] = useState(""); const router = useRouter(); async function submitRegister(values: Inputs) { setLoading(true); const res = await fetch("/api/registeruser", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ email: email, firstName: firstname, lastName: lastname, organization: organization, authRequestId: authRequestId, password: values.password, }), }); setLoading(false); if (!res.ok) { const error = await res.json(); throw new Error(error.details); } return res.json(); } async function createSessionWithLoginNameAndPassword( loginName: string, password: string ) { const res = await fetch("/api/session", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ loginName: loginName, password: password, organization: organization, authRequestId, //, register does not need an oidc callback }), }); if (!res.ok) { throw new Error("Failed to set user"); } return res.json(); } function submitAndLink(value: Inputs): Promise { return submitRegister(value) .then((humanResponse: any) => { setError(""); return createSessionWithLoginNameAndPassword( email, value.password ).then((session) => { setLoading(false); const params: any = { userID: humanResponse.userId }; if (authRequestId) { params.authRequestId = authRequestId; } if (organization) { params.organization = organization; } if (session && session.sessionId) { params.sessionId = session.sessionId; } return router.push(`/verify?` + new URLSearchParams(params)); }); }) .catch((errorDetails: Error) => { setLoading(false); setError(errorDetails.message); }); } const { errors } = formState; const watchPassword = watch("password", ""); const watchConfirmPassword = watch("confirmPassword", ""); const hasMinLength = passwordComplexitySettings && watchPassword?.length >= passwordComplexitySettings.minLength; const hasSymbol = symbolValidator(watchPassword); const hasNumber = numberValidator(watchPassword); const hasUppercase = upperCaseValidator(watchPassword); const hasLowercase = lowerCaseValidator(watchPassword); const policyIsValid = passwordComplexitySettings && (passwordComplexitySettings.requiresLowercase ? hasLowercase : true) && (passwordComplexitySettings.requiresNumber ? hasNumber : true) && (passwordComplexitySettings.requiresUppercase ? hasUppercase : true) && (passwordComplexitySettings.requiresSymbol ? hasSymbol : true) && hasMinLength; return (
{passwordComplexitySettings && ( )} {error && {error}}
); }