fix(login): use onChange instead of onBlur with react-hook-form (#11354)

# Which Problems Are Solved

Fixes a problem where submit buttons where not correctly enabled onBlur
event

# How the Problems Are Solved

By changing the react-hook-form mode property to onChange, buttons are
enabled as intended
This commit is contained in:
Max Peintner
2026-01-14 08:07:04 +00:00
committed by GitHub
parent 86fd769439
commit cd36abd725
13 changed files with 38 additions and 131 deletions
@@ -1,15 +1,7 @@
"use client"; "use client";
import { import { lowerCaseValidator, numberValidator, symbolValidator, upperCaseValidator } from "@/helpers/validators";
lowerCaseValidator, import { checkSessionAndSetPassword, sendPassword } from "@/lib/server/password";
numberValidator,
symbolValidator,
upperCaseValidator,
} from "@/helpers/validators";
import {
checkSessionAndSetPassword,
sendPassword,
} from "@/lib/server/password";
import { create } from "@zitadel/client"; import { create } from "@zitadel/client";
import { ChecksSchema } from "@zitadel/proto/zitadel/session/v2/session_service_pb"; import { ChecksSchema } from "@zitadel/proto/zitadel/session/v2/session_service_pb";
import { PasswordComplexitySettings } from "@zitadel/proto/zitadel/settings/v2/password_settings_pb"; import { PasswordComplexitySettings } from "@zitadel/proto/zitadel/settings/v2/password_settings_pb";
@@ -40,17 +32,11 @@ type Props = {
organization?: string; organization?: string;
}; };
export function ChangePasswordForm({ export function ChangePasswordForm({ passwordComplexitySettings, sessionId, loginName, requestId, organization }: Props) {
passwordComplexitySettings,
sessionId,
loginName,
requestId,
organization,
}: Props) {
const router = useRouter(); const router = useRouter();
const { register, handleSubmit, watch, formState } = useForm<Inputs>({ const { register, handleSubmit, watch, formState } = useForm<Inputs>({
mode: "onBlur", mode: "onChange",
defaultValues: { defaultValues: {
password: "", password: "",
comfirmPassword: "", comfirmPassword: "",
@@ -78,11 +64,7 @@ export function ChangePasswordForm({
}); });
if (changeResponse && "error" in changeResponse && changeResponse.error) { if (changeResponse && "error" in changeResponse && changeResponse.error) {
setError( setError(typeof changeResponse.error === "string" ? changeResponse.error : t("change.errors.unknownError"));
typeof changeResponse.error === "string"
? changeResponse.error
: t("change.errors.unknownError")
);
return; return;
} }
@@ -109,20 +91,12 @@ export function ChangePasswordForm({
setLoading(false); setLoading(false);
}); });
if ( if (passwordResponse && "error" in passwordResponse && passwordResponse.error) {
passwordResponse &&
"error" in passwordResponse &&
passwordResponse.error
) {
setError(passwordResponse.error); setError(passwordResponse.error);
return; return;
} }
if ( if (passwordResponse && "redirect" in passwordResponse && passwordResponse.redirect) {
passwordResponse &&
"redirect" in passwordResponse &&
passwordResponse.redirect
) {
return router.push(passwordResponse.redirect); return router.push(passwordResponse.redirect);
} }
@@ -134,9 +108,7 @@ export function ChangePasswordForm({
const watchPassword = watch("password", ""); const watchPassword = watch("password", "");
const watchConfirmPassword = watch("confirmPassword", ""); const watchConfirmPassword = watch("confirmPassword", "");
const hasMinLength = const hasMinLength = passwordComplexitySettings && watchPassword?.length >= passwordComplexitySettings.minLength;
passwordComplexitySettings &&
watchPassword?.length >= passwordComplexitySettings.minLength;
const hasSymbol = symbolValidator(watchPassword); const hasSymbol = symbolValidator(watchPassword);
const hasNumber = numberValidator(watchPassword); const hasNumber = numberValidator(watchPassword);
const hasUppercase = upperCaseValidator(watchPassword); const hasUppercase = upperCaseValidator(watchPassword);
@@ -196,17 +168,11 @@ export function ChangePasswordForm({
<Button <Button
type="submit" type="submit"
variant={ButtonVariants.Primary} variant={ButtonVariants.Primary}
disabled={ disabled={loading || !policyIsValid || !formState.isValid || watchPassword !== watchConfirmPassword}
loading ||
!policyIsValid ||
!formState.isValid ||
watchPassword !== watchConfirmPassword
}
onClick={handleSubmit(submitChange)} onClick={handleSubmit(submitChange)}
data-testid="submit-button" data-testid="submit-button"
> >
{loading && <Spinner className="mr-2 h-5 w-5" />}{" "} {loading && <Spinner className="mr-2 h-5 w-5" />} <Translated i18nKey="change.submit" namespace="password" />
<Translated i18nKey="change.submit" namespace="password" />
</Button> </Button>
</div> </div>
</form> </form>
@@ -20,7 +20,7 @@ export function DeviceCodeForm({ userCode }: { userCode?: string }) {
const router = useRouter(); const router = useRouter();
const { register, handleSubmit, formState } = useForm<Inputs>({ const { register, handleSubmit, formState } = useForm<Inputs>({
mode: "onBlur", mode: "onChange",
defaultValues: { defaultValues: {
userCode: userCode || "", userCode: userCode || "",
}, },
@@ -88,8 +88,7 @@ export function DeviceCodeForm({ userCode }: { userCode?: string }) {
onClick={handleSubmit(submitCodeAndContinue)} onClick={handleSubmit(submitCodeAndContinue)}
data-testid="submit-button" data-testid="submit-button"
> >
{loading && <Spinner className="mr-2 h-5 w-5" />}{" "} {loading && <Spinner className="mr-2 h-5 w-5" />} <Translated i18nKey="usercode.submit" namespace="device" />
<Translated i18nKey="usercode.submit" namespace="device" />
</Button> </Button>
</div> </div>
</form> </form>
@@ -24,7 +24,7 @@ type Props = {
export function LDAPUsernamePasswordForm({ idpId, link }: Props) { export function LDAPUsernamePasswordForm({ idpId, link }: Props) {
const { register, handleSubmit, formState } = useForm<Inputs>({ const { register, handleSubmit, formState } = useForm<Inputs>({
mode: "onBlur", mode: "onChange",
}); });
const t = useTranslations("ldap"); const t = useTranslations("ldap");
+1 -1
View File
@@ -44,7 +44,7 @@ export function LoginOTP({ host, loginName, sessionId, requestId, organization,
const initialized = useRef(false); const initialized = useRef(false);
const { register, handleSubmit, formState } = useForm<Inputs>({ const { register, handleSubmit, formState } = useForm<Inputs>({
mode: "onBlur", mode: "onChange",
defaultValues: { defaultValues: {
code: code ? code : "", code: code ? code : "",
}, },
+1 -1
View File
@@ -28,7 +28,7 @@ type Props = {
export function PasswordForm({ loginSettings, loginName, organization, requestId }: Props) { export function PasswordForm({ loginSettings, loginName, organization, requestId }: Props) {
const { register, handleSubmit, formState } = useForm<Inputs>({ const { register, handleSubmit, formState } = useForm<Inputs>({
mode: "onBlur", mode: "onChange",
}); });
const t = useTranslations("password"); const t = useTranslations("password");
@@ -47,7 +47,7 @@ export function RegisterFormIDPIncomplete({
idpUserName, idpUserName,
}: Props) { }: Props) {
const { register, handleSubmit, formState } = useForm<Inputs>({ const { register, handleSubmit, formState } = useForm<Inputs>({
mode: "onBlur", mode: "onChange",
defaultValues: { defaultValues: {
email: defaultValues?.email ?? "", email: defaultValues?.email ?? "",
firstname: defaultValues?.firstname ?? "", firstname: defaultValues?.firstname ?? "",
+1 -1
View File
@@ -46,7 +46,7 @@ export function RegisterForm({
idpCount = 0, idpCount = 0,
}: Props) { }: Props) {
const { register, handleSubmit, formState } = useForm<Inputs>({ const { register, handleSubmit, formState } = useForm<Inputs>({
mode: "onBlur", mode: "onChange",
defaultValues: { defaultValues: {
email: email ?? "", email: email ?? "",
firstName: firstname ?? "", firstName: firstname ?? "",
@@ -25,7 +25,7 @@ type Props = {
export function RegisterPasskey({ sessionId, userId, isPrompt, organization, requestId, code, codeId }: Props) { export function RegisterPasskey({ sessionId, userId, isPrompt, organization, requestId, code, codeId }: Props) {
const { handleSubmit, formState } = useForm<Inputs>({ const { handleSubmit, formState } = useForm<Inputs>({
mode: "onBlur", mode: "onChange",
}); });
const [error, setError] = useState<string>(""); const [error, setError] = useState<string>("");
@@ -1,16 +1,7 @@
"use client"; "use client";
import { import { lowerCaseValidator, numberValidator, symbolValidator, upperCaseValidator } from "@/helpers/validators";
lowerCaseValidator, import { changePassword, resetPassword, sendPassword } from "@/lib/server/password";
numberValidator,
symbolValidator,
upperCaseValidator,
} from "@/helpers/validators";
import {
changePassword,
resetPassword,
sendPassword,
} from "@/lib/server/password";
import { create } from "@zitadel/client"; import { create } from "@zitadel/client";
import { ChecksSchema } from "@zitadel/proto/zitadel/session/v2/session_service_pb"; import { ChecksSchema } from "@zitadel/proto/zitadel/session/v2/session_service_pb";
import { PasswordComplexitySettings } from "@zitadel/proto/zitadel/settings/v2/password_settings_pb"; import { PasswordComplexitySettings } from "@zitadel/proto/zitadel/settings/v2/password_settings_pb";
@@ -54,7 +45,7 @@ export function SetPasswordForm({
codeRequired, codeRequired,
}: Props) { }: Props) {
const { register, handleSubmit, watch, formState } = useForm<Inputs>({ const { register, handleSubmit, watch, formState } = useForm<Inputs>({
mode: "onBlur", mode: "onChange",
defaultValues: { defaultValues: {
code: code ?? "", code: code ?? "",
}, },
@@ -148,20 +139,12 @@ export function SetPasswordForm({
setLoading(false); setLoading(false);
}); });
if ( if (passwordResponse && "error" in passwordResponse && passwordResponse.error) {
passwordResponse &&
"error" in passwordResponse &&
passwordResponse.error
) {
setError(passwordResponse.error); setError(passwordResponse.error);
return; return;
} }
if ( if (passwordResponse && "redirect" in passwordResponse && passwordResponse.redirect) {
passwordResponse &&
"redirect" in passwordResponse &&
passwordResponse.redirect
) {
return router.push(passwordResponse.redirect); return router.push(passwordResponse.redirect);
} }
@@ -173,9 +156,7 @@ export function SetPasswordForm({
const watchPassword = watch("password", ""); const watchPassword = watch("password", "");
const watchConfirmPassword = watch("confirmPassword", ""); const watchConfirmPassword = watch("confirmPassword", "");
const hasMinLength = const hasMinLength = passwordComplexitySettings && watchPassword?.length >= passwordComplexitySettings.minLength;
passwordComplexitySettings &&
watchPassword?.length >= passwordComplexitySettings.minLength;
const hasSymbol = symbolValidator(watchPassword); const hasSymbol = symbolValidator(watchPassword);
const hasNumber = numberValidator(watchPassword); const hasNumber = numberValidator(watchPassword);
const hasUppercase = upperCaseValidator(watchPassword); const hasUppercase = upperCaseValidator(watchPassword);
@@ -271,17 +252,11 @@ export function SetPasswordForm({
<Button <Button
type="submit" type="submit"
variant={ButtonVariants.Primary} variant={ButtonVariants.Primary}
disabled={ disabled={loading || !policyIsValid || !formState.isValid || watchPassword !== watchConfirmPassword}
loading ||
!policyIsValid ||
!formState.isValid ||
watchPassword !== watchConfirmPassword
}
onClick={handleSubmit(submitPassword)} onClick={handleSubmit(submitPassword)}
data-testid="submit-button" data-testid="submit-button"
> >
{loading && <Spinner className="mr-2 h-5 w-5" />}{" "} {loading && <Spinner className="mr-2 h-5 w-5" />} <Translated i18nKey="set.submit" namespace="password" />
<Translated i18nKey="set.submit" namespace="password" />
</Button> </Button>
</div> </div>
</form> </form>
@@ -1,11 +1,6 @@
"use client"; "use client";
import { import { lowerCaseValidator, numberValidator, symbolValidator, upperCaseValidator } from "@/helpers/validators";
lowerCaseValidator,
numberValidator,
symbolValidator,
upperCaseValidator,
} from "@/helpers/validators";
import { registerUser } from "@/lib/server/register"; import { registerUser } from "@/lib/server/register";
import { PasswordComplexitySettings } from "@zitadel/proto/zitadel/settings/v2/password_settings_pb"; import { PasswordComplexitySettings } from "@zitadel/proto/zitadel/settings/v2/password_settings_pb";
import { useRouter } from "next/navigation"; import { useRouter } from "next/navigation";
@@ -45,7 +40,7 @@ export function SetRegisterPasswordForm({
requestId, requestId,
}: Props) { }: Props) {
const { register, handleSubmit, watch, formState } = useForm<Inputs>({ const { register, handleSubmit, watch, formState } = useForm<Inputs>({
mode: "onBlur", mode: "onChange",
defaultValues: { defaultValues: {
email: email ?? "", email: email ?? "",
firstname: firstname ?? "", firstname: firstname ?? "",
@@ -93,9 +88,7 @@ export function SetRegisterPasswordForm({
const watchPassword = watch("password", ""); const watchPassword = watch("password", "");
const watchConfirmPassword = watch("confirmPassword", ""); const watchConfirmPassword = watch("confirmPassword", "");
const hasMinLength = const hasMinLength = passwordComplexitySettings && watchPassword?.length >= passwordComplexitySettings.minLength;
passwordComplexitySettings &&
watchPassword?.length >= passwordComplexitySettings.minLength;
const hasSymbol = symbolValidator(watchPassword); const hasSymbol = symbolValidator(watchPassword);
const hasNumber = numberValidator(watchPassword); const hasNumber = numberValidator(watchPassword);
const hasUppercase = upperCaseValidator(watchPassword); const hasUppercase = upperCaseValidator(watchPassword);
@@ -155,17 +148,11 @@ export function SetRegisterPasswordForm({
<Button <Button
type="submit" type="submit"
variant={ButtonVariants.Primary} variant={ButtonVariants.Primary}
disabled={ disabled={loading || !policyIsValid || !formState.isValid || watchPassword !== watchConfirmPassword}
loading ||
!policyIsValid ||
!formState.isValid ||
watchPassword !== watchConfirmPassword
}
onClick={handleSubmit(submitRegister)} onClick={handleSubmit(submitRegister)}
data-testid="submit-button" data-testid="submit-button"
> >
{loading && <Spinner className="mr-2 h-5 w-5" />}{" "} {loading && <Spinner className="mr-2 h-5 w-5" />} <Translated i18nKey="password.submit" namespace="register" />
<Translated i18nKey="password.submit" namespace="register" />
</Button> </Button>
</div> </div>
</form> </form>
+1 -1
View File
@@ -36,7 +36,7 @@ export function TotpRegister({ uri, loginName, sessionId, requestId, organizatio
const router = useRouter(); const router = useRouter();
const { register, handleSubmit, formState } = useForm<Inputs>({ const { register, handleSubmit, formState } = useForm<Inputs>({
mode: "onBlur", mode: "onChange",
defaultValues: { defaultValues: {
code: "", code: "",
}, },
+3 -14
View File
@@ -27,17 +27,9 @@ type Props = {
allowRegister: boolean; allowRegister: boolean;
}; };
export function UsernameForm({ export function UsernameForm({ loginName, requestId, organization, suffix, loginSettings, submit, allowRegister }: Props) {
loginName,
requestId,
organization,
suffix,
loginSettings,
submit,
allowRegister,
}: Props) {
const { register, handleSubmit, formState } = useForm<Inputs>({ const { register, handleSubmit, formState } = useForm<Inputs>({
mode: "onBlur", mode: "onChange",
defaultValues: { defaultValues: {
loginName: loginName ? loginName : "", loginName: loginName ? loginName : "",
}, },
@@ -87,10 +79,7 @@ export function UsernameForm({
}, []); }, []);
let inputLabel = t("labels.loginname"); let inputLabel = t("labels.loginname");
if ( if (loginSettings?.disableLoginWithEmail && loginSettings?.disableLoginWithPhone) {
loginSettings?.disableLoginWithEmail &&
loginSettings?.disableLoginWithPhone
) {
inputLabel = t("labels.username"); inputLabel = t("labels.username");
} else if (loginSettings?.disableLoginWithEmail) { } else if (loginSettings?.disableLoginWithEmail) {
inputLabel = t("labels.usernameOrPhoneNumber"); inputLabel = t("labels.usernameOrPhoneNumber");
+3 -12
View File
@@ -25,18 +25,11 @@ type Props = {
requestId?: string; requestId?: string;
}; };
export function VerifyForm({ export function VerifyForm({ userId, loginName, organization, requestId, code, isInvite }: Props) {
userId,
loginName,
organization,
requestId,
code,
isInvite,
}: Props) {
const router = useRouter(); const router = useRouter();
const { register, handleSubmit, formState } = useForm<Inputs>({ const { register, handleSubmit, formState } = useForm<Inputs>({
mode: "onBlur", mode: "onChange",
defaultValues: { defaultValues: {
code: code ?? "", code: code ?? "",
}, },
@@ -73,9 +66,7 @@ export function VerifyForm({
} }
const fcn = useCallback( const fcn = useCallback(
async function submitCodeAndContinue( async function submitCodeAndContinue(value: Inputs): Promise<boolean | void> {
value: Inputs,
): Promise<boolean | void> {
setLoading(true); setLoading(true);
const response = await sendVerification({ const response = await sendVerification({