Files
zitadel/apps/login/src/components/login-otp.tsx
T
d27833e1bc feat(login): redirect configuration options in multi-domain setups (#11527)
# 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]>
2026-02-24 11:02:29 +03:00

277 lines
8.2 KiB
TypeScript

"use client";
import { handleServerActionResponse } from "@/lib/client-utils";
import { updateOrCreateSession } from "@/lib/server/session";
import { create } from "@zitadel/client";
import { RequestChallengesSchema } from "@zitadel/proto/zitadel/session/v2/challenge_pb";
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 { useCallback, useEffect, useRef, useState } from "react";
import { useForm } from "react-hook-form";
import { Alert, AlertType } from "./alert";
import { BackButton } from "./back-button";
import { useTranslations } from "next-intl";
import { Button, ButtonVariants } from "./button";
import { TextInput } from "./input";
import { Spinner } from "./spinner";
import { Translated } from "./translated";
import { AutoSubmitForm } from "./auto-submit-form";
import { completeFlowOrGetUrl } from "@/lib/client";
// either loginName or sessionId must be provided
type Props = {
host: string | null;
loginName?: string;
sessionId?: string;
requestId?: string;
organization?: string;
method: string;
code?: string;
loginSettings?: LoginSettings;
};
type Inputs = {
code: string;
};
export function LoginOTP({ host, loginName, sessionId, requestId, organization, method, code, loginSettings }: Props) {
const t = useTranslations("otp");
const [error, setError] = useState<string>("");
const [loading, setLoading] = useState<boolean>(false);
const [samlData, setSamlData] = useState<{ url: string; fields: Record<string, string> } | null>(null);
const router = useRouter();
const initialized = useRef(false);
const { register, handleSubmit, formState } = useForm<Inputs>({
mode: "onChange",
defaultValues: {
code: code ? code : "",
},
});
const updateSessionForOTPChallenge = useCallback(async (): Promise<{
error?: string;
[key: string]: any;
}> => {
let challenges;
const basePath = process.env.NEXT_PUBLIC_BASE_PATH ?? "";
if (method === "email") {
challenges = create(RequestChallengesSchema, {
otpEmail: {
deliveryType: {
case: "sendCode",
value: host
? {
urlTemplate:
`${host.includes("localhost") ? "http://" : "https://"}${host}${basePath}/otp/${method}?code={{.Code}}&userId={{.UserID}}&sessionId={{.SessionID}}` +
(requestId ? `&requestId=${requestId}` : ""),
}
: {},
},
},
});
}
if (method === "sms") {
challenges = create(RequestChallengesSchema, {
otpSms: {},
});
}
let response;
try {
response = await updateOrCreateSession({
loginName,
sessionId,
organization,
challenges,
requestId,
});
} catch {
return { error: "Could not request OTP challenge" };
}
if (response && "error" in response && response.error) {
return { error: response.error };
}
return response;
}, [method, host, requestId, loginName, sessionId, organization]);
useEffect(() => {
if (!initialized.current && ["email", "sms"].includes(method) && !code) {
initialized.current = true;
setLoading(true);
updateSessionForOTPChallenge()
.then((response) => {
if (response?.error) {
setError(response.error);
}
})
.finally(() => {
setLoading(false);
});
}
}, [updateSessionForOTPChallenge, method, code]);
async function submitCode(values: Inputs, organization?: string) {
setLoading(true);
let body: any = {
code: values.code,
method,
};
if (organization) {
body.organization = organization;
}
if (requestId) {
body.requestId = requestId;
}
let checks;
if (method === "sms") {
checks = create(ChecksSchema, {
otpSms: { code: values.code },
});
}
if (method === "email") {
checks = create(ChecksSchema, {
otpEmail: { code: values.code },
});
}
if (method === "time-based") {
checks = create(ChecksSchema, {
totp: { code: values.code },
});
}
const response = await updateOrCreateSession({
loginName,
sessionId,
organization,
checks,
requestId,
})
.catch(() => {
setError("Could not verify OTP code");
return;
})
.finally(() => {
setLoading(false);
});
if (response && "error" in response && response.error) {
setError(response.error);
return;
}
return response;
}
function setCodeAndContinue(values: Inputs) {
return submitCode(values, organization).then(async (response) => {
if (response && "sessionId" in response) {
setLoading(true);
// Wait for 2 seconds to avoid eventual consistency issues with an OTP code being verified in the /login endpoint
await new Promise((resolve) => setTimeout(resolve, 2000));
// Use unified approach that handles both OIDC/SAML and regular flows
if (response.factors?.user) {
const callbackResponse = await completeFlowOrGetUrl(
requestId && response.sessionId
? {
sessionId: response.sessionId,
requestId: requestId,
organization: response.factors?.user?.organizationId,
}
: {
loginName: response.factors.user.loginName,
organization: response.factors?.user?.organizationId,
},
loginSettings?.defaultRedirectUri,
);
setLoading(false);
handleServerActionResponse(callbackResponse, router, setSamlData, setError);
} else {
setLoading(false);
}
}
});
}
return (
<>
{samlData && <AutoSubmitForm url={samlData.url} fields={samlData.fields} />}
<form className="w-full">
{["email", "sms"].includes(method) && (
<Alert type={AlertType.INFO}>
<div className="flex flex-row">
<span className="mr-auto flex-1 text-left">
<Translated i18nKey="verify.noCodeReceived" namespace="otp" />
</span>
<button
aria-label="Resend OTP Code"
disabled={loading}
type="button"
className="ml-4 cursor-pointer text-primary-light-500 hover:text-primary-light-400 disabled:cursor-default disabled:text-gray-400 dark:text-primary-dark-500 hover:dark:text-primary-dark-400 dark:disabled:text-gray-700"
onClick={async () => {
setLoading(true);
const response = await updateSessionForOTPChallenge();
if (response?.error) {
setError(response.error);
}
setLoading(false);
}}
data-testid="resend-button"
>
<Translated i18nKey="verify.resendCode" namespace="otp" />
</button>
</div>
</Alert>
)}
<div className="mt-4">
<TextInput
type="text"
autoFocus
{...register("code", { required: t("verify.required.code") })}
label={t("verify.labels.code")}
autoComplete="one-time-code"
data-testid="code-text-input"
/>
</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(setCodeAndContinue)}
data-testid="submit-button"
>
{loading && <Spinner className="mr-2 h-5 w-5" />} <Translated i18nKey="verify.submit" namespace="otp" />
</Button>
</div>
</form>
</>
);
}