mirror of
https://github.com/andwati/zitadel.git
synced 2026-10-28 18:12:16 +00:00
# Which Problems Are Solved This PR introduces robust redirect configuration options and enhances security for Server Actions in multi-domain setups. # How the Problems Are Solved - Redirect Fallback Logic: Implemented a unified `resolveRedirectUri` utility that prioritizes redirect targets as follows: - Environment override (`DEFAULT_REDIRECT_URI`) - Organization settings (`defaultRedirectUri`). - Relative fallback (`/signedin?...`). - Host Reflection: `DEFAULT_REDIRECT_URI` supports absolute URLs or relative paths via host reflection. - Server Action Security: Configured `serverActions.allowedOrigins` to be dynamically manageable via the `SERVER_ACTION_ALLOWED_ORIGINS` environment variable (comma-separated list). It defaults to an empty list for maximum security. - UI Integration: Updated the /signedin page to use the unified redirect logic, ensuring consistent behavior for the "Continue" button. - Documentation: Added type definitions and detailed descriptions for new environment variables in `next-env-vars.d.ts` --------- Co-authored-by: Copilot <[email protected]>
169 lines
4.9 KiB
TypeScript
169 lines
4.9 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";
|
|
import { handleServerActionResponse } from "@/lib/client-utils";
|
|
import { AutoSubmitForm } from "./auto-submit-form";
|
|
|
|
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 [samlData, setSamlData] = useState<{ url: string; fields: Record<string, string> } | null>(null);
|
|
|
|
const [loading, setLoading] = useState<boolean>(false);
|
|
|
|
const router = useRouter();
|
|
|
|
async function submitPassword(values: Inputs) {
|
|
setError("");
|
|
setLoading(true);
|
|
|
|
try {
|
|
const response = await sendPassword({
|
|
loginName,
|
|
organization,
|
|
defaultOrganization,
|
|
checks: create(ChecksSchema, {
|
|
password: { password: values.password },
|
|
}),
|
|
requestId,
|
|
});
|
|
|
|
handleServerActionResponse(response, router, setSamlData, setError);
|
|
} catch {
|
|
setError(t("verify.errors.couldNotVerifyPassword"));
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}
|
|
|
|
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 (
|
|
<>
|
|
{samlData && <AutoSubmitForm url={samlData.url} fields={samlData.fields} />}
|
|
<form className="w-full">
|
|
<div className={`${error && "transform-gpu animate-shake"}`}>
|
|
<TextInput
|
|
type="password"
|
|
autoComplete="password"
|
|
autoFocus
|
|
{...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>
|
|
</>
|
|
);
|
|
}
|