mirror of
https://github.com/andwati/zitadel.git
synced 2026-11-12 09:05:12 +00:00
<!-- Please inform yourself about the contribution guidelines on submitting a PR here: https://github.com/zitadel/zitadel/blob/main/CONTRIBUTING.md#submit-a-pull-request-pr. Take note of how PR/commit titles should be written and replace the template texts in the sections below. Don't remove any of the sections. It is important that the commit history clearly shows what is changed and why. Important: By submitting a contribution you agree to the terms from our Licensing Policy as described here: https://github.com/zitadel/zitadel/blob/main/LICENSING.md#community-contributions. --> # Which Problems Are Solved The first input field on login screens is not automatically focused, requiring users to manually click before typing. This creates unnecessary friction during the login flow, particularly for keyboard-oriented users and accessibility scenarios. # How the Problems Are Solved - Added the `autoFocus` prop to the first `TextInput` in all 12 login form components. - For forms with conditionally rendered first inputs (`set-password-form` and `register-form-idp-incomplete`), `autoFocus` is set dynamically so the correct field receives focus depending on which input is visible. # Additional Changes None # Additional Context - Closes #11565 Co-authored-by: Max Peintner <[email protected]>
160 lines
5.2 KiB
TypeScript
160 lines
5.2 KiB
TypeScript
"use client";
|
|
|
|
import { lowerCaseValidator, numberValidator, symbolValidator, upperCaseValidator } from "@/helpers/validators";
|
|
import { registerUser } from "@/lib/server/register";
|
|
import { handleServerActionResponse } from "@/lib/client";
|
|
import { PasswordComplexitySettings } from "@zitadel/proto/zitadel/settings/v2/password_settings_pb";
|
|
import { useRouter } from "next/navigation";
|
|
import { useState } from "react";
|
|
import { useTranslations } from "next-intl";
|
|
import { FieldValues, useForm } from "react-hook-form";
|
|
import { Alert } from "./alert";
|
|
import { BackButton } from "./back-button";
|
|
import { Button, ButtonVariants } from "./button";
|
|
import { TextInput } from "./input";
|
|
import { PasswordComplexity } from "./password-complexity";
|
|
import { Spinner } from "./spinner";
|
|
import { Translated } from "./translated";
|
|
import { AutoSubmitForm } from "./auto-submit-form";
|
|
|
|
type Inputs =
|
|
| {
|
|
password: string;
|
|
confirmPassword: string;
|
|
}
|
|
| FieldValues;
|
|
|
|
type Props = {
|
|
passwordComplexitySettings: PasswordComplexitySettings;
|
|
email: string;
|
|
firstname: string;
|
|
lastname: string;
|
|
organization: string;
|
|
requestId?: string;
|
|
};
|
|
|
|
export function SetRegisterPasswordForm({
|
|
passwordComplexitySettings,
|
|
email,
|
|
firstname,
|
|
lastname,
|
|
organization,
|
|
requestId,
|
|
}: Props) {
|
|
const { register, handleSubmit, watch, formState } = useForm<Inputs>({
|
|
mode: "onChange",
|
|
defaultValues: {
|
|
email: email ?? "",
|
|
firstname: firstname ?? "",
|
|
lastname: lastname ?? "",
|
|
},
|
|
});
|
|
|
|
const t = useTranslations("register");
|
|
|
|
const [loading, setLoading] = useState<boolean>(false);
|
|
const [error, setError] = useState<string>("");
|
|
const [samlData, setSamlData] = useState<{ url: string; fields: Record<string, string> } | null>(null);
|
|
|
|
const router = useRouter();
|
|
|
|
async function submitRegister(values: Inputs) {
|
|
setLoading(true);
|
|
try {
|
|
const response = await registerUser({
|
|
email: email,
|
|
firstName: firstname,
|
|
lastName: lastname,
|
|
organization: organization,
|
|
requestId: requestId,
|
|
password: values.password,
|
|
});
|
|
|
|
handleServerActionResponse(response, router, setSamlData, setError);
|
|
} catch {
|
|
setError(t("errors.couldNotRegisterUser"));
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}
|
|
|
|
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 (
|
|
<>
|
|
{samlData && <AutoSubmitForm url={samlData.url} fields={samlData.fields} />}
|
|
<form className="w-full">
|
|
<div className="mb-4 grid grid-cols-1 gap-4 pt-4">
|
|
<div className="">
|
|
<TextInput
|
|
type="password"
|
|
autoComplete="new-password"
|
|
autoFocus
|
|
required
|
|
{...register("password", {
|
|
required: t("password.required.password"),
|
|
})}
|
|
label={t("password.labels.password")}
|
|
error={errors.password?.message as string}
|
|
data-testid="password-text-input"
|
|
/>
|
|
</div>
|
|
<div className="">
|
|
<TextInput
|
|
type="password"
|
|
required
|
|
autoComplete="new-password"
|
|
{...register("confirmPassword", {
|
|
required: t("password.required.confirmPassword"),
|
|
})}
|
|
label={t("password.labels.confirmPassword")}
|
|
error={errors.confirmPassword?.message as string}
|
|
data-testid="password-confirm-text-input"
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
{passwordComplexitySettings && (
|
|
<PasswordComplexity
|
|
passwordComplexitySettings={passwordComplexitySettings}
|
|
password={watchPassword}
|
|
equals={!!watchPassword && watchPassword === watchConfirmPassword}
|
|
/>
|
|
)}
|
|
|
|
{error && <Alert>{error}</Alert>}
|
|
|
|
<div className="mt-8 flex w-full flex-row items-center justify-between">
|
|
<BackButton data-testid="back-button" />
|
|
<Button
|
|
type="submit"
|
|
variant={ButtonVariants.Primary}
|
|
disabled={loading || !policyIsValid || !formState.isValid || watchPassword !== watchConfirmPassword}
|
|
onClick={handleSubmit(submitRegister)}
|
|
data-testid="submit-button"
|
|
>
|
|
{loading && <Spinner className="mr-2 h-5 w-5" />} <Translated i18nKey="password.submit" namespace="register" />
|
|
</Button>
|
|
</div>
|
|
</form>
|
|
</>
|
|
);
|
|
}
|