399 lines
16 KiB
TypeScript
399 lines
16 KiB
TypeScript
import { useEffect, useRef, useState } from "react";
|
|
import { Form, Link, useNavigate, useNavigation, useSearchParams } from "react-router";
|
|
import { useTranslation } from "react-i18next";
|
|
import { FeatherArrowLeft, FeatherMail, FeatherRotateCw, FeatherShieldCheck, FeatherWand2 } from "@subframe/core";
|
|
import Captcha from "~/components/captcha/Captcha";
|
|
import { Alert } from "~/ui/components/Alert";
|
|
import { Button } from "~/ui/components/Button";
|
|
import { Card } from "~/ui/components/Card";
|
|
import { LinkButton } from "~/ui/components/LinkButton";
|
|
import { TextInput } from "~/ui/components/TextInput";
|
|
import { useRootData } from "~/utils/data/useRootData";
|
|
import { AuthShell, AuthStatus, AuthDivider } from "~/components/auth/AuthShell";
|
|
import type { LoginActionData } from "~/modules/users/components/LoginForm";
|
|
|
|
// Login screen based on David's Subframe design: logo, auth card, email/password,
|
|
// password reset, standard login, magic-link entry point, and sign-up footer.
|
|
// It is wired to the existing login action (`action=login`, csrf, redirectTo,
|
|
// field errors, 2FA redirect). The admin/SSO fallback path stays in login.tsx.
|
|
|
|
export default function LoginScreen({
|
|
actionData,
|
|
redirectTo,
|
|
}: {
|
|
actionData: LoginActionData | null | undefined;
|
|
redirectTo?: string;
|
|
}) {
|
|
const { t } = useTranslation();
|
|
const { appConfiguration, csrf } = useRootData();
|
|
const navigation = useNavigation();
|
|
const navigate = useNavigate();
|
|
const [searchParams] = useSearchParams();
|
|
const isSubmitting = navigation.state !== "idle";
|
|
const mfaVerifyPath = actionData?.mfaVerifyPath ?? "/login-mfa";
|
|
const emailError = actionData?.fieldErrors?.email;
|
|
const passwordError = actionData?.fieldErrors?.password;
|
|
const loginEmailRef = useRef<HTMLInputElement | null>(null);
|
|
const loginPasswordRef = useRef<HTMLInputElement | null>(null);
|
|
const [loginPassword, setLoginPassword] = useState("");
|
|
const [magicRequestOpen, setMagicRequestOpen] = useState(Boolean(actionData?.magicLinkRequest));
|
|
const [magicEmail, setMagicEmail] = useState(actionData?.magicLinkEmail ?? actionData?.fields?.email ?? "");
|
|
const showMagicRequest = !actionData?.magicLinkSent && (magicRequestOpen || Boolean(actionData?.magicLinkRequest));
|
|
const invalidCredentials = actionData?.error === t("api.errors.invalidPassword");
|
|
const credentialFieldError = invalidCredentials ? t("account.login.invalidCredentialsTitle") : undefined;
|
|
const passwordFieldError = credentialFieldError ?? passwordError;
|
|
const emailErrorId = emailError ? "login-email-error" : undefined;
|
|
const passwordErrorId = passwordFieldError ? "login-password-error" : undefined;
|
|
const shouldClearPassword = Boolean(
|
|
actionData && !actionData.magicLinkRequest && !actionData.magicLinkSent && (passwordError || actionData.error || actionData.requiresCaptcha)
|
|
);
|
|
const formError = actionData?.error && !actionData.requiresCaptcha ? actionData.error : undefined;
|
|
const captchaSitekey = actionData?.sitekey ?? appConfiguration.auth.captcha.loginSiteKey;
|
|
const compactActionButtons = Boolean(invalidCredentials || actionData?.requiresCaptcha);
|
|
|
|
// D1: password is valid but 2FA is required. The pending cookie is set; redirect to verification.
|
|
useEffect(() => {
|
|
if (actionData?.requiresMfa) {
|
|
navigate(mfaVerifyPath);
|
|
}
|
|
}, [actionData?.requiresMfa, mfaVerifyPath, navigate]);
|
|
|
|
useEffect(() => {
|
|
if (shouldClearPassword) {
|
|
setLoginPassword("");
|
|
}
|
|
if (passwordFieldError) {
|
|
loginPasswordRef.current?.focus();
|
|
}
|
|
}, [actionData, passwordFieldError, shouldClearPassword]);
|
|
|
|
if (actionData?.requiresMfa) {
|
|
return (
|
|
<AuthShell title={t("account.loginMfa.title")} dataUiId="ui.auth.login.page">
|
|
<div className="flex flex-col items-center gap-3 text-center" data-ui-id="ui.auth.login.mfaRedirect">
|
|
<p className="text-body-small-regular-0 font-body-small-regular-0 text-text-neutral-tertiary">
|
|
{t("account.loginMfa.redirecting")}
|
|
</p>
|
|
<Link to={mfaVerifyPath} className="no-underline" data-ui-id="ui.auth.login.mfaContinue">
|
|
<LinkButton hideIcon>{t("account.loginMfa.continue")}</LinkButton>
|
|
</Link>
|
|
</div>
|
|
</AuthShell>
|
|
);
|
|
}
|
|
|
|
if (showMagicRequest) {
|
|
return (
|
|
<AuthShell
|
|
title={t("account.login.magicLinkRequestTitle")}
|
|
subtitle={t("account.login.magicLinkRequestSubtitle")}
|
|
titleAlign="center"
|
|
headerGap="gap-1.5"
|
|
subtitleVariant="regular-150"
|
|
dataUiId="ui.auth.login.magicLinkRequest"
|
|
>
|
|
<Form method="post" noValidate className="flex w-full flex-col gap-4" data-ui-id="ui.auth.login.magicLinkRequest.form">
|
|
<input type="hidden" name="intent" value="magicLink" readOnly />
|
|
<input type="hidden" name="csrf" value={csrf} readOnly />
|
|
<input
|
|
type="hidden"
|
|
name="redirectTo"
|
|
value={redirectTo ?? searchParams.get("redirect") ?? searchParams.get("redirectTo") ?? ""}
|
|
readOnly
|
|
/>
|
|
<TextInput
|
|
className="h-auto w-full flex-none"
|
|
fillContainer
|
|
showLabel
|
|
label={t("account.shared.email")}
|
|
helpText={emailError ?? ""}
|
|
error={Boolean(emailError)}
|
|
type="email"
|
|
>
|
|
<TextInput.Input
|
|
name="email"
|
|
type="email"
|
|
required
|
|
autoComplete="email"
|
|
placeholder={t("account.shared.emailPlaceholder")}
|
|
value={magicEmail}
|
|
onChange={(event) => setMagicEmail(event.target.value)}
|
|
disabled={isSubmitting}
|
|
data-ui-id="ui.auth.login.magicEmail"
|
|
/>
|
|
</TextInput>
|
|
<Button type="submit" icon={<FeatherWand2 />} loading={isSubmitting} disabled={isSubmitting} data-ui-id="ui.auth.login.magicSubmit">
|
|
{t("account.login.magicLinkSubmit")}
|
|
</Button>
|
|
</Form>
|
|
<div className="flex w-full items-center justify-center">
|
|
<LinkButton
|
|
size="small"
|
|
icon={<FeatherArrowLeft />}
|
|
iconRight={null}
|
|
onClick={() => {
|
|
setMagicRequestOpen(false);
|
|
setMagicEmail("");
|
|
}}
|
|
data-ui-id="ui.auth.login.magicBack"
|
|
>
|
|
{t("account.login.backToLogin")}
|
|
</LinkButton>
|
|
</div>
|
|
</AuthShell>
|
|
);
|
|
}
|
|
|
|
// D-D0a: magic link issued, show David's "link sent" status screen.
|
|
if (actionData?.magicLinkSent) {
|
|
return (
|
|
<AuthStatus
|
|
icon={<FeatherMail />}
|
|
tone="positive"
|
|
title={t("account.login.magicLinkSentTitle")}
|
|
message={t("account.login.magicLinkSentBody")}
|
|
hint={t("account.login.magicLinkSentHint")}
|
|
dataUiId="ui.auth.login.magicLinkSent"
|
|
>
|
|
<Form method="post" className="contents">
|
|
<input type="hidden" name="intent" value="magicLink" readOnly />
|
|
<input type="hidden" name="csrf" value={csrf} readOnly />
|
|
<input type="hidden" name="email" value={actionData.magicLinkEmail ?? ""} readOnly />
|
|
<LinkButton
|
|
type="submit"
|
|
size="small"
|
|
icon={<FeatherRotateCw />}
|
|
iconRight={null}
|
|
disabled={isSubmitting}
|
|
data-ui-id="ui.auth.login.magicLinkResend"
|
|
>
|
|
{t("account.login.magicLinkResend")}
|
|
</LinkButton>
|
|
</Form>
|
|
<LinkButton
|
|
size="small"
|
|
icon={<FeatherMail />}
|
|
iconRight={null}
|
|
onClick={() => {
|
|
setMagicEmail(actionData.magicLinkEmail ?? "");
|
|
setMagicRequestOpen(true);
|
|
}}
|
|
data-ui-id="ui.auth.login.changeMagicEmail"
|
|
>
|
|
{t("account.login.magicLinkChangeEmail")}
|
|
</LinkButton>
|
|
<LinkButton
|
|
size="small"
|
|
icon={<FeatherArrowLeft />}
|
|
iconRight={null}
|
|
onClick={() => navigate("/login")}
|
|
data-ui-id="ui.auth.login.backToLogin"
|
|
>
|
|
{t("account.login.backToLogin")}
|
|
</LinkButton>
|
|
</AuthStatus>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<AuthShell
|
|
title={t("account.login.title")}
|
|
subtitle={t("account.login.headline")}
|
|
titleAlign="center"
|
|
headerGap="gap-1.5"
|
|
subtitleVariant="regular-150"
|
|
cardSize="large"
|
|
showLogo={true}
|
|
dataUiId="ui.auth.login.page"
|
|
footer={
|
|
<>
|
|
<span
|
|
className={
|
|
compactActionButtons
|
|
? "text-body-small-regular-0 font-body-small-regular-0 text-text-neutral-secondary"
|
|
: "text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-secondary"
|
|
}
|
|
>
|
|
{t("account.login.noAccount")}
|
|
</span>
|
|
<Link to="/signup" className="no-underline" data-ui-id="ui.auth.login.register">
|
|
<LinkButton size="small" variant="subtle" hideIcon>
|
|
{t("account.login.register")}
|
|
</LinkButton>
|
|
</Link>
|
|
</>
|
|
}
|
|
>
|
|
<Form id="loginForm" method="post" noValidate className="flex w-full flex-col gap-4" data-ui-id="ui.auth.login.form">
|
|
<input type="hidden" name="action" value="login" readOnly />
|
|
<input type="hidden" name="csrf" value={csrf} readOnly />
|
|
<input
|
|
type="hidden"
|
|
name="redirectTo"
|
|
value={redirectTo ?? searchParams.get("redirect") ?? searchParams.get("redirectTo") ?? ""}
|
|
readOnly
|
|
/>
|
|
|
|
<div className="flex w-full flex-col items-start gap-4">
|
|
{invalidCredentials ? (
|
|
<Alert
|
|
variant="error"
|
|
notDismissible
|
|
title={t("account.login.invalidCredentialsTitle")}
|
|
description={t("account.login.invalidCredentialsDescription")}
|
|
textSize="small"
|
|
data-ui-id="ui.auth.login.invalidCredentialsAlert"
|
|
/>
|
|
) : null}
|
|
{actionData?.requiresCaptcha ? (
|
|
<>
|
|
<Alert
|
|
variant="warning"
|
|
notDismissible
|
|
title={t("account.login.captchaTitle")}
|
|
description={t("account.login.captchaDescription")}
|
|
textSize="small"
|
|
data-ui-id="ui.auth.login.captchaAlert"
|
|
/>
|
|
<Card className="h-auto w-full flex-none" size="medium" variant="neutral" data-ui-id="ui.auth.login.captchaBlock">
|
|
<div className="flex w-full items-center gap-2">
|
|
<FeatherShieldCheck className="text-body-default-medium-0 font-body-default-medium-0 text-icon-neutral-tertiary" />
|
|
<span className="text-body-small-regular-0 font-body-small-regular-0 text-text-neutral-tertiary">
|
|
{t("account.login.captchaLabel")}
|
|
</span>
|
|
</div>
|
|
<div className="flex h-12 w-full flex-none items-center justify-center rounded-sm border border-dashed border-border-default bg-surface-neutral-secondary">
|
|
{appConfiguration.auth.captcha.enabled ? (
|
|
<Captcha
|
|
enabled={appConfiguration.auth.captcha.enabled}
|
|
provider={appConfiguration.auth.captcha.provider}
|
|
instanceUrl={appConfiguration.auth.captcha.instanceUrl}
|
|
sitekey={captchaSitekey}
|
|
/>
|
|
) : (
|
|
<span className="text-body-small-regular-0 font-body-small-regular-0 text-text-neutral-tertiary">
|
|
{t("account.login.captchaRequired")}
|
|
</span>
|
|
)}
|
|
</div>
|
|
</Card>
|
|
</>
|
|
) : null}
|
|
<div className="flex w-full flex-col items-start gap-2">
|
|
<TextInput
|
|
className="h-auto w-full flex-none"
|
|
fillContainer
|
|
showLabel
|
|
label={t("account.shared.email")}
|
|
message={credentialFieldError ?? ""}
|
|
error={Boolean(emailError || credentialFieldError)}
|
|
type="email"
|
|
>
|
|
<TextInput.Input
|
|
ref={loginEmailRef}
|
|
name="email"
|
|
type="email"
|
|
required
|
|
autoComplete="email"
|
|
placeholder={t("account.shared.emailPlaceholder")}
|
|
defaultValue={actionData?.fields?.email}
|
|
disabled={isSubmitting}
|
|
aria-invalid={emailError ? true : undefined}
|
|
aria-describedby={emailErrorId}
|
|
data-ui-id="ui.auth.login.email"
|
|
/>
|
|
</TextInput>
|
|
{emailError ? (
|
|
<span
|
|
id={emailErrorId}
|
|
className="text-body-small-regular-0 font-body-small-regular-0 text-text-feedback-negative"
|
|
role="alert"
|
|
>
|
|
{emailError}
|
|
</span>
|
|
) : null}
|
|
</div>
|
|
|
|
<div className="flex w-full flex-col items-start gap-2">
|
|
<TextInput
|
|
className="h-auto w-full flex-none"
|
|
fillContainer
|
|
showLabel
|
|
label={t("account.shared.password")}
|
|
error={Boolean(passwordFieldError)}
|
|
type="password"
|
|
>
|
|
<TextInput.Input
|
|
ref={loginPasswordRef}
|
|
name="password"
|
|
type="password"
|
|
required
|
|
autoComplete="current-password"
|
|
placeholder={t("account.login.passwordPlaceholder")}
|
|
value={loginPassword}
|
|
onChange={(event) => setLoginPassword(event.target.value)}
|
|
disabled={isSubmitting}
|
|
aria-invalid={passwordFieldError ? true : undefined}
|
|
aria-describedby={passwordErrorId}
|
|
data-ui-id="ui.auth.login.password"
|
|
/>
|
|
</TextInput>
|
|
{passwordFieldError ? (
|
|
<span
|
|
id={passwordErrorId}
|
|
className="text-body-small-regular-0 font-body-small-regular-0 text-text-feedback-negative"
|
|
role="alert"
|
|
>
|
|
{passwordFieldError}
|
|
</span>
|
|
) : null}
|
|
|
|
<div className="flex w-full items-start justify-end">
|
|
<Link to="/forgot-password" className="no-underline" data-ui-id="ui.auth.login.forgot">
|
|
<LinkButton size="small" variant="subtle" hideIcon>
|
|
{t("account.login.forgot")}
|
|
</LinkButton>
|
|
</Link>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<Button
|
|
type="submit"
|
|
size={compactActionButtons ? "medium" : "large"}
|
|
className={compactActionButtons ? "h-9 w-full flex-none" : "h-10 w-full flex-none"}
|
|
loading={isSubmitting}
|
|
disabled={isSubmitting}
|
|
data-ui-id="ui.auth.login.submit"
|
|
>
|
|
{t("account.login.button")}
|
|
</Button>
|
|
|
|
{formError && !invalidCredentials && navigation.state === "idle" ? (
|
|
<p className="text-center text-body-small-regular-0 font-body-small-regular-0 text-text-feedback-negative" role="alert">
|
|
{formError}
|
|
</p>
|
|
) : null}
|
|
</Form>
|
|
|
|
<AuthDivider label={t("account.login.divider")} labelSize={compactActionButtons ? "small" : "extra-small"} />
|
|
|
|
<Button
|
|
type="button"
|
|
variant="secondary"
|
|
size={compactActionButtons ? "medium" : "large"}
|
|
className={compactActionButtons ? "h-9 w-full flex-none" : "h-10 w-full flex-none"}
|
|
icon={<FeatherWand2 />}
|
|
loading={isSubmitting}
|
|
onClick={() => {
|
|
setMagicEmail(loginEmailRef.current?.value ?? actionData?.fields?.email ?? "");
|
|
setMagicRequestOpen(true);
|
|
}}
|
|
data-ui-id="ui.auth.login.magicLink"
|
|
>
|
|
{t("account.login.magicLink")}
|
|
</Button>
|
|
</AuthShell>
|
|
);
|
|
}
|