Files
zitadel/apps/login/src/components/password-form.tsx
T
26f8238499 fix(login): correctly redirect to /password when ignoreUnknownUsername is set (#11130)
Closes #11006 

# Which Problems Are Solved

This PR addresses an issue where the ignoreUnknownUsernames setting was
not being respected in certain scenarios during the login flow.
Specifically:

- When a user was found but rejected due to login settings (e.g.,
disableLoginWithEmail), the system would return a "User not found" error
instead of redirecting to the password page as dictated by
ignoreUnknownUsernames.
- When a user was not found, and the flow fell through the registration
checks (e.g., IDP redirect failed), it would also return "User not
found" without checking ignoreUnknownUsernames.

# How the Problems Are Solved

- Introduced a helper function `handleUserNotFound` in 
`apps/login/src/lib/server/loginname.ts` to centralize the logic for
checking ignoreUnknownUsernames and redirecting to the password page.
- Updated the user validation logic to call handleUserNotFound when a
user is found but rejected by policy.
- Updated the registration fallback logic to call handleUserNotFound
instead of directly returning an error.
- Added new test cases in `apps/login/src/lib/server/loginname.test.ts`
to verify the fix and ensure no regressions.

# Additional Changes

- Fixed inconsistency in translation files

---------

Co-authored-by: Livio Spring <[email protected]>
2026-01-14 08:24:48 +00:00

170 lines
4.6 KiB
TypeScript

"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 { useRouter } from "next/navigation";
import { useState } from "react";
import { useTranslations } from "next-intl";
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";
import { Translated } from "./translated";
type Inputs = {
password: string;
};
type Props = {
loginSettings: LoginSettings | undefined;
loginName: string;
organization?: string;
defaultOrganization?: string;
requestId?: string;
};
export function PasswordForm({ loginSettings, loginName, organization, defaultOrganization, requestId }: Props) {
const { register, handleSubmit, formState } = useForm<Inputs>({
mode: "onChange",
});
const t = useTranslations("password");
const [info, setInfo] = useState<string>("");
const [error, setError] = useState<string>("");
const [loading, setLoading] = useState<boolean>(false);
const router = useRouter();
async function submitPassword(values: Inputs) {
setError("");
setLoading(true);
const response = await sendPassword({
loginName,
organization,
defaultOrganization,
checks: create(ChecksSchema, {
password: { password: values.password },
}),
requestId,
})
.catch(() => {
setError(t("verify.errors.couldNotVerifyPassword"));
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,
defaultOrganization,
requestId,
})
.catch(() => {
setError(t("errors.couldNotSendResetLink"));
return;
})
.finally(() => {
setLoading(false);
});
if (response && "error" in response) {
setError(response.error as string);
return;
}
setInfo(t("verify.info.passwordResetSent"));
const params = new URLSearchParams({
loginName: loginName,
});
if (organization) {
params.append("organization", organization);
}
if (requestId) {
params.append("requestId", requestId);
}
return router.push("/password/set?" + params);
}
return (
<form className="w-full">
<div className={`${error && "transform-gpu animate-shake"}`}>
<TextInput
type="password"
autoComplete="password"
{...register("password", { required: t("verify.required.password") })}
label={t("verify.labels.password")}
data-testid="password-text-input"
/>
{!loginSettings?.hidePasswordReset && (
<button
className="text-sm transition-all hover:text-primary-light-500 dark:hover:text-primary-dark-500"
onClick={() => resetPasswordAndContinue()}
type="button"
disabled={loading}
data-testid="reset-button"
>
<Translated i18nKey="verify.resetPassword" namespace="password" />
</button>
)}
{loginName && <input type="hidden" name="loginName" autoComplete="username" value={loginName} />}
</div>
{info && (
<div className="py-4">
<Alert type={AlertType.INFO}>{info}</Alert>
</div>
)}
{error && (
<div className="py-4" data-testid="error">
<Alert>{error}</Alert>
</div>
)}
<div className="mt-8 flex w-full flex-row items-center">
<BackButton data-testid="back-button" />
<span className="flex-grow"></span>
<Button
type="submit"
className="self-end"
variant={ButtonVariants.Primary}
disabled={loading || !formState.isValid}
onClick={handleSubmit(submitPassword)}
data-testid="submit-button"
>
{loading && <Spinner className="mr-2 h-5 w-5" />} <Translated i18nKey="verify.submit" namespace="password" />
</Button>
</div>
</form>
);
}