Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions web/src/hooks/useEmailConfigured.ts
Original file line number Diff line number Diff line change
@@ -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<EmailConfiguredState>({
configured: false,
loading: true,
});

useEffect(() => {
let cancelled = false;
api
.get<SystemInfo>("/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;
}
4 changes: 4 additions & 0 deletions web/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions web/src/locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@
"sendCode": "发送验证码",
"setup2fa": "设置两步验证",
"setup2faRequired": "管理员要求您设置两步验证后才能继续使用",
"twoFactorUnavailable": "你启用的二次验证方式当前不可用(本站未配置邮件发送服务),请联系管理员处理。",
"passwordMismatch": "两次输入的密码不一致",
"missingToken": "缺少验证令牌",
"forgotPassword": "忘记密码",
"forgotPasswordDesc": "输入您的注册邮箱。如果该邮箱已注册,您将收到一封包含验证码的邮件。",
"forgotPasswordUnavailable": "本站尚未配置邮件发送服务,暂时无法通过邮件找回密码。请联系管理员为你重置密码。",
"resetCodeSent": "如果该邮箱已注册,验证码将在几分钟内送达,请同时检查垃圾邮件文件夹。",
"verificationCode": "验证码",
"newPassword": "新密码",
Expand Down Expand Up @@ -226,6 +228,8 @@
"notificationHours": "通知时段",
"totpDescription": "使用验证器应用(如 Google Authenticator、Authy 等)",
"emailOtpDescription": "登录时通过邮件接收一次性验证码",
"emailOtpUnavailable": "本站未配置邮件发送服务,无法启用邮件验证码。管理员可在「管理 → 邮件服务」中配置。",
"emailOtpUnavailableWarning": "本站当前未配置邮件发送服务,登录时不会提供邮件验证码选项。",
"passkeyDescription": "使用生物识别或硬件密钥登录",
"emailOtpVerifyPrompt": "验证码已发送至您的邮箱,请输入验证码以启用邮箱验证码登录。",
"emailOtpVerifyCode": "验证码",
Expand Down
26 changes: 25 additions & 1 deletion web/src/pages/auth/ForgotPasswordPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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("");
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -80,7 +92,19 @@ export function ForgotPasswordPage() {
<AuthShell title={t("app.name")} subtitle={t("auth.forgotPassword")}>
<div>
<div>
{done ? (
{emailInfoLoading ? null : emailNotConfigured || !emailConfigured ? (
<div className="space-y-4 text-center">
<p className="text-body-sm text-muted-foreground">
{t("auth.forgotPasswordUnavailable")}
</p>
<Link
to="/login"
className="block text-sm text-primary hover:underline font-medium"
>
{t("auth.backToLogin")}
</Link>
</div>
) : done ? (
<div className="space-y-4 text-center">
<p className="text-body-sm text-success">
{t("auth.resetPasswordSuccess")}
Expand Down
9 changes: 8 additions & 1 deletion web/src/pages/auth/LoginPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
41 changes: 32 additions & 9 deletions web/src/pages/settings/SettingsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -441,17 +445,36 @@ function SecurityTab() {
</p>
)}

{!twofa?.email_otp_enabled && !emailOtpVerifying && (
<button
type="button"
onClick={sendEmailOtpVerify}
disabled={emailOtpSending}
className="py-1.5 px-3 bg-primary hover:bg-primary/90 disabled:opacity-50 text-primary-foreground text-xs font-medium rounded-md transition-colors"
>
{emailOtpSending ? t("common.loading") : t("common.enable")}
</button>
{/* §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 && (
<div className="flex items-start gap-2 mb-3">
<AlertTriangle className="w-4 h-4 text-warning shrink-0 mt-0.5" />
<p className="text-xs text-muted-foreground">
{t("settings.emailOtpUnavailableWarning")}
</p>
</div>
)}

{!twofa?.email_otp_enabled &&
!emailOtpVerifying &&
!emailInfoLoading &&
(emailConfigured ? (
<button
type="button"
onClick={sendEmailOtpVerify}
disabled={emailOtpSending}
className="py-1.5 px-3 bg-primary hover:bg-primary/90 disabled:opacity-50 text-primary-foreground text-xs font-medium rounded-md transition-colors"
>
{emailOtpSending ? t("common.loading") : t("common.enable")}
</button>
) : (
/* §3.9: no email delivery → no enable affordance, muted note only. */
<p className="text-xs text-muted-foreground">
{t("settings.emailOtpUnavailable")}
</p>
))}

{!twofa?.email_otp_enabled && emailOtpVerifying && (
<div className="space-y-3">
<p className="text-xs text-muted-foreground">
Expand Down
Loading