From ec24172bb51f4266151a4d586cfa0b93e55ec44f Mon Sep 17 00:00:00 2001 From: mapxn <16299874+mapxn@users.noreply.github.com> Date: Mon, 26 Oct 2026 19:40:17 +0800 Subject: [PATCH] =?UTF-8?q?feat(web):=20=E6=9C=AA=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E5=8F=91=E4=BB=B6=E6=9C=8D=E5=8A=A1=E6=97=B6=202FA/=E5=BF=98?= =?UTF-8?q?=E8=AE=B0=E5=AF=86=E7=A0=81/=E8=AE=BE=E7=BD=AE=E9=A1=B5?= =?UTF-8?q?=E6=8F=90=E7=A4=BA=E6=80=81=20(ONE-461)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 useEmailConfigured:读 GET /api/system/info 的 email_configured, 查询失败或字段缺失按 false(fail-closed),与 §3.1 同一口径 - 忘记密码页:未配置时整页提示态(保留 AuthShell 与标题,仅说明文案 + 返回登录),已配置逐字不变;保留竞态兜底,提交时收到 503 EMAIL_NOT_CONFIGURED 切到同一提示态 - 登录页:403 code=2FA_METHOD_UNAVAILABLE 映射为本地化文案后走既有 FormError,其余错误仍透传后端 message(api 层未吞 code/message) - 设置页邮件 OTP 一节:未配置且未启用 → 不渲染启用按钮,改 muted 说明; 未配置但已启用 → 保留徽标与禁用按钮 + 现成 AlertTriangle 警告行; 已配置逐字不变 - 新增文案全部进 zh.json / en.json 双语键,无硬编码字符串 --- web/src/hooks/useEmailConfigured.ts | 51 +++++++++++++++++++++++ web/src/locales/en.json | 4 ++ web/src/locales/zh.json | 4 ++ web/src/pages/auth/ForgotPasswordPage.tsx | 26 +++++++++++- web/src/pages/auth/LoginPage.tsx | 9 +++- web/src/pages/settings/SettingsPage.tsx | 41 ++++++++++++++---- 6 files changed, 124 insertions(+), 11 deletions(-) create mode 100644 web/src/hooks/useEmailConfigured.ts diff --git a/web/src/hooks/useEmailConfigured.ts b/web/src/hooks/useEmailConfigured.ts new file mode 100644 index 0000000..3320846 --- /dev/null +++ b/web/src/hooks/useEmailConfigured.ts @@ -0,0 +1,51 @@ +import { useEffect, useState } from "react"; +import { api } from "@/lib/api"; + +/** Subset of `GET /api/system/info` (public, unauthenticated) used here. */ +interface SystemInfo { + email_configured?: boolean; +} + +export interface EmailConfiguredState { + /** + * Whether the site can send email. Fail-closed: while unknown, or when the + * lookup fails, this is `false` — never expose an email entry point that + * cannot work (spec ONE-461 §3.1 / §3.8). + */ + configured: boolean; + loading: boolean; +} + +/** + * Reads `email_configured` from `GET /api/system/info` on mount — the same + * public endpoint the login page already uses for `registration_enabled`. + */ +export function useEmailConfigured(): EmailConfiguredState { + const [state, setState] = useState({ + configured: false, + loading: true, + }); + + useEffect(() => { + let cancelled = false; + api + .get("/system/info") + .then((info) => { + if (cancelled) return; + setState({ + configured: info.email_configured === true, + loading: false, + }); + }) + .catch(() => { + // Fail-closed: a failed lookup must not surface email entry points. + if (cancelled) return; + setState({ configured: false, loading: false }); + }); + return () => { + cancelled = true; + }; + }, []); + + return state; +} diff --git a/web/src/locales/en.json b/web/src/locales/en.json index a4af695..3bbf390 100644 --- a/web/src/locales/en.json +++ b/web/src/locales/en.json @@ -63,10 +63,12 @@ "sendCode": "Send Code", "setup2fa": "Set Up 2FA", "setup2faRequired": "Your administrator requires you to set up two-factor authentication before continuing", + "twoFactorUnavailable": "The two-factor method you enabled is currently unavailable: this site has no email delivery configured. Please contact an administrator.", "passwordMismatch": "Passwords do not match", "missingToken": "Missing verification token", "forgotPassword": "Forgot password?", "forgotPasswordDesc": "Enter your email address. If it's registered, you'll receive a verification code.", + "forgotPasswordUnavailable": "Email delivery is not configured on this site, so password recovery by email is unavailable. Please contact an administrator to reset your password.", "resetCodeSent": "If this email is registered, a code will arrive within a few minutes. Check your spam folder too.", "verificationCode": "Verification code", "newPassword": "New password", @@ -226,6 +228,8 @@ "notificationHours": "Notification Hours", "totpDescription": "Use an authenticator app (Google Authenticator, Authy, etc.)", "emailOtpDescription": "Receive a one-time code via email when signing in.", + "emailOtpUnavailable": "Email delivery is not configured on this site, so Email OTP cannot be enabled. An administrator can configure it under Admin → Email Service.", + "emailOtpUnavailableWarning": "This site has no email delivery configured, so Email OTP will not be offered at sign-in.", "passkeyDescription": "Use biometrics or a hardware key for sign-in.", "emailOtpVerifyPrompt": "A verification code has been sent to your email. Enter it to enable Email OTP.", "emailOtpVerifyCode": "Verification code", diff --git a/web/src/locales/zh.json b/web/src/locales/zh.json index a986a87..180118b 100644 --- a/web/src/locales/zh.json +++ b/web/src/locales/zh.json @@ -63,10 +63,12 @@ "sendCode": "发送验证码", "setup2fa": "设置两步验证", "setup2faRequired": "管理员要求您设置两步验证后才能继续使用", + "twoFactorUnavailable": "你启用的二次验证方式当前不可用(本站未配置邮件发送服务),请联系管理员处理。", "passwordMismatch": "两次输入的密码不一致", "missingToken": "缺少验证令牌", "forgotPassword": "忘记密码", "forgotPasswordDesc": "输入您的注册邮箱。如果该邮箱已注册,您将收到一封包含验证码的邮件。", + "forgotPasswordUnavailable": "本站尚未配置邮件发送服务,暂时无法通过邮件找回密码。请联系管理员为你重置密码。", "resetCodeSent": "如果该邮箱已注册,验证码将在几分钟内送达,请同时检查垃圾邮件文件夹。", "verificationCode": "验证码", "newPassword": "新密码", @@ -226,6 +228,8 @@ "notificationHours": "通知时段", "totpDescription": "使用验证器应用(如 Google Authenticator、Authy 等)", "emailOtpDescription": "登录时通过邮件接收一次性验证码", + "emailOtpUnavailable": "本站未配置邮件发送服务,无法启用邮件验证码。管理员可在「管理 → 邮件服务」中配置。", + "emailOtpUnavailableWarning": "本站当前未配置邮件发送服务,登录时不会提供邮件验证码选项。", "passkeyDescription": "使用生物识别或硬件密钥登录", "emailOtpVerifyPrompt": "验证码已发送至您的邮箱,请输入验证码以启用邮箱验证码登录。", "emailOtpVerifyCode": "验证码", diff --git a/web/src/pages/auth/ForgotPasswordPage.tsx b/web/src/pages/auth/ForgotPasswordPage.tsx index eb726ac..47b85d5 100644 --- a/web/src/pages/auth/ForgotPasswordPage.tsx +++ b/web/src/pages/auth/ForgotPasswordPage.tsx @@ -2,11 +2,19 @@ import { useState, useEffect, useCallback } from "react"; import { useTranslation } from "react-i18next"; import { Link } from "react-router"; import { api } from "@/lib/api"; +import { useEmailConfigured } from "@/hooks/useEmailConfigured"; import { AuthShell, FormError } from "@/components/AuthShell"; export function ForgotPasswordPage() { const { t } = useTranslation(); + const { configured: emailConfigured, loading: emailInfoLoading } = + useEmailConfigured(); + // Race fallback: the site may lose its email configuration between page load + // and submit — the backend then answers 503 EMAIL_NOT_CONFIGURED and we swap + // to the same notice instead of a generic error. + const [emailNotConfigured, setEmailNotConfigured] = useState(false); + const [step, setStep] = useState<"email" | "reset">("email"); const [email, setEmail] = useState(""); const [code, setCode] = useState(""); @@ -40,6 +48,10 @@ export function ForgotPasswordPage() { setStep("reset"); startCountdown(); } catch (err: any) { + if (err.code === "EMAIL_NOT_CONFIGURED") { + setEmailNotConfigured(true); + return; + } if (err.status === 429) { setError(t("auth.resetCodeCooldown")); if (step === "reset") startCountdown(); @@ -80,7 +92,19 @@ export function ForgotPasswordPage() {
- {done ? ( + {emailInfoLoading ? null : emailNotConfigured || !emailConfigured ? ( +
+

+ {t("auth.forgotPasswordUnavailable")} +

+ + {t("auth.backToLogin")} + +
+ ) : done ? (

{t("auth.resetPasswordSuccess")} diff --git a/web/src/pages/auth/LoginPage.tsx b/web/src/pages/auth/LoginPage.tsx index a4ab214..19eb907 100644 --- a/web/src/pages/auth/LoginPage.tsx +++ b/web/src/pages/auth/LoginPage.tsx @@ -46,7 +46,14 @@ export function LoginPage() { navigate("/"); } } catch (err: any) { - setError(err.message || t("common.error")); + // §3.3: the site's only enabled 2FA method became unavailable (email + // delivery not configured) — backend answers 403 with a machine-readable + // code, mapped here so the notice is localized (spec §3.10). + setError( + err.code === "2FA_METHOD_UNAVAILABLE" + ? t("auth.twoFactorUnavailable") + : err.message || t("common.error"), + ); } finally { setLoading(false); } diff --git a/web/src/pages/settings/SettingsPage.tsx b/web/src/pages/settings/SettingsPage.tsx index 078344d..2b2f0e1 100644 --- a/web/src/pages/settings/SettingsPage.tsx +++ b/web/src/pages/settings/SettingsPage.tsx @@ -27,6 +27,7 @@ import { api } from "@/lib/api"; import { useAuth } from "@/hooks/useAuth"; import { useTheme } from "@/components/ThemeProvider"; import { useIsMobile } from "@/hooks/useIsMobile"; +import { useEmailConfigured } from "@/hooks/useEmailConfigured"; import { ConfirmDialog } from "@/components/ConfirmDialog"; import { PasswordPromptDialog } from "@/components/PasswordPromptDialog"; import { BackupSection } from "@/components/backup/BackupSection"; @@ -182,6 +183,9 @@ function SecurityTab() { const twofa = user?.twofa; + const { configured: emailConfigured, loading: emailInfoLoading } = + useEmailConfigured(); + const [pendingDisable, setPendingDisable] = useState< "totp" | "passkey" | "email_otp" | null >(null); @@ -441,17 +445,36 @@ function SecurityTab() {

)} - {!twofa?.email_otp_enabled && !emailOtpVerifying && ( - + {/* §3.9: an enabled email OTP record survives a site without email + delivery (disable must stay possible), so warn instead of hiding. */} + {!emailInfoLoading && !emailConfigured && twofa?.email_otp_enabled && ( +
+ +

+ {t("settings.emailOtpUnavailableWarning")} +

+
)} + {!twofa?.email_otp_enabled && + !emailOtpVerifying && + !emailInfoLoading && + (emailConfigured ? ( + + ) : ( + /* §3.9: no email delivery → no enable affordance, muted note only. */ +

+ {t("settings.emailOtpUnavailable")} +

+ ))} + {!twofa?.email_otp_enabled && emailOtpVerifying && (