Skip to content

Build your personal knowledge base with Trilium Notes

refactor(mfa): share a status badge between the OpenID and TOTP cards

Elian Doranb476978d9120
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index f2819b0437..c5c1b135d6 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -1721,6 +1721,8 @@
"totp_enroll_finish": "Finish setup",
"totp_enroll_enable_error": "Couldn't enable two-factor authentication. Please try again.",
"totp_section_title": "Two-factor authentication",
+ "totp_status_active": "Active",
+ "totp_status_inactive": "Inactive",
"totp_remove_label": "Remove two-factor authentication",
"totp_remove_description": "Completely remove your two-factor authentication setup. Unlike switching it off above, you'd have to set it up again from scratch to use it.",
"totp_remove_button": "Remove",
diff --git a/apps/client/src/widgets/type_widgets/options/components/MfaStatusBadge.css b/apps/client/src/widgets/type_widgets/options/components/MfaStatusBadge.css
new file mode 100644
index 0000000000..6ceb949923
--- /dev/null
+++ b/apps/client/src/widgets/type_widgets/options/components/MfaStatusBadge.css
@@ -0,0 +1,19 @@
+/* Section-title layout: the title text with its status badge inline just after it. */
+.mfa-status-title {
+ display: inline-flex;
+ align-items: center;
+ gap: 10px;
+}
+
+/* The badge itself: a colored outline whose hue is driven by --color per tone. */
+.mfa-status-badge.active {
+ --color: var(--admonition-tip-accent-color, #40c025);
+}
+
+.mfa-status-badge.pending {
+ --color: var(--admonition-warning-accent-color, #e0a800);
+}
+
+.mfa-status-badge.inactive {
+ --color: var(--muted-text-color, #888);
+}
diff --git a/apps/client/src/widgets/type_widgets/options/components/MfaStatusBadge.tsx b/apps/client/src/widgets/type_widgets/options/components/MfaStatusBadge.tsx
new file mode 100644
index 0000000000..42dec86e72
--- /dev/null
+++ b/apps/client/src/widgets/type_widgets/options/components/MfaStatusBadge.tsx
@@ -0,0 +1,21 @@
+import "./MfaStatusBadge.css";
+
+import { Badge } from "../../../react/Badge";
+
+/** Visual tone of an MFA status badge: a live method, one configured but not yet bound, or an off one. */
+export type MfaStatusTone = "active" | "pending" | "inactive";
+
+const TONE_ICON: Record<MfaStatusTone, string> = {
+ active: "bx bx-check",
+ pending: "bx bx-link",
+ inactive: "bx bx-x"
+};
+
+/**
+ * Status badge shared by the sign-in method cards (OpenID Connect, TOTP): a colored outline whose hue
+ * and icon are driven by `tone` — green when the method is active, amber when it's configured but not
+ * yet bound, muted when it's off. Wrap it in a `.mfa-status-title` span to sit inline after a section title.
+ */
+export default function MfaStatusBadge({ tone, text }: { tone: MfaStatusTone, text: string }) {
+ return <Badge className={`mfa-status-badge ${tone}`} icon={TONE_ICON[tone]} text={text} outline />;
+}
diff --git a/apps/client/src/widgets/type_widgets/options/password.css b/apps/client/src/widgets/type_widgets/options/password.css
index 096742f835..735b9b661f 100644
--- a/apps/client/src/widgets/type_widgets/options/password.css
+++ b/apps/client/src/widgets/type_widgets/options/password.css
@@ -1,25 +1,3 @@
-/* OAuth section heading: the title text and its configured/not-configured status badge on one line,
- the badge sitting just after the title. */
-.oauth-status-title {
- display: inline-flex;
- align-items: center;
- gap: 10px;
-}
-
-/* Status badge mirrors the page-header MFA badge: a colored outline whose hue is driven by --color.
- Three states: connected (green), configured-but-not-connected (amber), not configured (muted). */
-.oauth-status-badge.connected {
- --color: var(--admonition-tip-accent-color, #40c025);
-}
-
-.oauth-status-badge.not-connected {
- --color: var(--admonition-warning-accent-color, #e0a800);
-}
-
-.oauth-status-badge.not-configured {
- --color: var(--muted-text-color, #888);
-}
-
/* Provider row: the issuer icon (or a fallback key glyph) sitting just before its display name. */
.oauth-provider {
display: inline-flex;
diff --git a/apps/client/src/widgets/type_widgets/options/password.tsx b/apps/client/src/widgets/type_widgets/options/password.tsx
index 09806b577f..9f6d58bba7 100644
--- a/apps/client/src/widgets/type_widgets/options/password.tsx
+++ b/apps/client/src/widgets/type_widgets/options/password.tsx
@@ -11,7 +11,6 @@ import server from "../../../services/server";
import toast from "../../../services/toast";
import { isElectron } from "../../../services/utils";
import Admonition from "../../react/Admonition";
-import { Badge } from "../../react/Badge";
import Button from "../../react/Button";
import Collapsible from "../../react/Collapsible";
import FormGroup from "../../react/FormGroup";
@@ -21,6 +20,7 @@ import FormTextBox from "../../react/FormTextBox";
import { useTriliumOption } from "../../react/hooks";
import Modal from "../../react/Modal";
import RawHtml from "../../react/RawHtml";
+import MfaStatusBadge, { MfaStatusTone } from "./components/MfaStatusBadge";
import OptionsPageHeader from "./components/OptionsPageHeader";
import OptionsRow, { OptionsRowWithButton } from "./components/OptionsRow";
import OptionsSection from "./components/OptionsSection";
@@ -254,11 +254,11 @@ function OAuthStatusCard({ status, refreshStatus }: { status?: OAuthStatus, refr
// The status badge mirrors the card's three states: an account is bound (green "Connected"),
// the server is set up but no account is bound yet (amber "Not connected"), or the server-side
// variables are missing entirely (muted "Not configured").
- const badge = enrolled
- ? { className: "connected", icon: "bx bx-check", text: t("multi_factor_authentication.oauth_status_connected") }
+ const badge: { tone: MfaStatusTone, text: string } = enrolled
+ ? { tone: "active", text: t("multi_factor_authentication.oauth_status_connected") }
: configured
- ? { className: "not-connected", icon: "bx bx-link", text: t("multi_factor_authentication.oauth_status_not_connected") }
- : { className: "not-configured", icon: "bx bx-x", text: t("multi_factor_authentication.oauth_status_not_configured") };
+ ? { tone: "pending", text: t("multi_factor_authentication.oauth_status_not_connected") }
+ : { tone: "inactive", text: t("multi_factor_authentication.oauth_status_not_configured") };
// Enrollment is a full-page provider round-trip (not an XHR): navigate to the OIDC login route,
// which on return binds the identity to this still-authenticated session (see open_id afterCallback).
@@ -279,14 +279,9 @@ function OAuthStatusCard({ status, refreshStatus }: { status?: OAuthStatus, refr
return (
<OptionsSection
title={
- <span className="oauth-status-title">
+ <span className="mfa-status-title">
{t("multi_factor_authentication.oauth_title")}
- <Badge
- className={`oauth-status-badge ${badge.className}`}
- icon={badge.icon}
- text={badge.text}
- outline
- />
+ <MfaStatusBadge tone={badge.tone} text={badge.text} />
</span>
}
>
diff --git a/apps/client/src/widgets/type_widgets/options/totp.tsx b/apps/client/src/widgets/type_widgets/options/totp.tsx
index bbc891ee31..86cb07f888 100644
--- a/apps/client/src/widgets/type_widgets/options/totp.tsx
+++ b/apps/client/src/widgets/type_widgets/options/totp.tsx
@@ -1,7 +1,7 @@
import "./totp.css";
import { TOTPEnableResponse, TOTPGenerate, TOTPRecoveryKeysResponse, TOTPStatus, TOTPVerifyResponse } from "@triliumnext/commons";
-import { RefObject } from "preact";
+import { ComponentChildren, RefObject } from "preact";
import { createPortal } from "preact/compat";
import { useCallback, useEffect, useMemo, useRef, useState } from "preact/hooks";
import qrcode from "qrcode-generator";
@@ -21,6 +21,7 @@ import FormTextBox from "../../react/FormTextBox";
import { useStaticTooltip } from "../../react/hooks";
import Modal from "../../react/Modal";
import { RawHtmlBlock } from "../../react/RawHtml";
+import MfaStatusBadge from "./components/MfaStatusBadge";
import { OptionsRowWithButton } from "./components/OptionsRow";
import OptionsSection from "./components/OptionsSection";
@@ -102,17 +103,32 @@ export function TotpSettings({ totpStatus, refreshTotpStatus }: {
refreshRecoveryKeys();
}, [ refreshRecoveryKeys ]);
+ // Status badge mirroring the OpenID card: TOTP is either enabled (a secret is set) or off.
+ const totpEnabled = totpStatus?.set ?? false;
+ const totpTitle = (
+ <span className="mfa-status-title">
+ {t("multi_factor_authentication.totp_section_title")}
+ <MfaStatusBadge
+ tone={totpEnabled ? "active" : "inactive"}
+ text={totpEnabled
+ ? t("multi_factor_authentication.totp_status_active")
+ : t("multi_factor_authentication.totp_status_inactive")}
+ />
+ </span>
+ );
+
return (<>
{/* Before enrollment, a single call-to-action opens the dialog — which is where the secret is
generated, verified, and (only on Finish) persisted. Once a secret is set, that's replaced
by the recovery-codes panel; recovery codes are part of TOTP and never exist without it. */}
{totpStatus?.set
? <TotpRecoveryKeys
+ title={totpTitle}
status={recoveryStatus}
onRegenerate={regenerateRecoveryCodes}
onRemoveTotp={removeTotp}
/>
- : <OptionsSection title={t("multi_factor_authentication.totp_section_title")}>
+ : <OptionsSection title={totpTitle}>
<OptionsRowWithButton
label={t("multi_factor_authentication.totp_setup_label")}
description={t("multi_factor_authentication.totp_setup_description")}
@@ -477,7 +493,8 @@ function downloadRecoveryCodes(codes: string[]) {
* remove-TOTP action. The codes themselves are only ever shown once — in a modal right after
* enrollment or regeneration (see {@link RecoveryCodesModal}).
*/
-function TotpRecoveryKeys({ status, onRegenerate, onRemoveTotp }: {
+function TotpRecoveryKeys({ title, status, onRegenerate, onRemoveTotp }: {
+ title: ComponentChildren,
status?: string[],
onRegenerate: () => void,
onRemoveTotp: () => void
@@ -485,7 +502,7 @@ function TotpRecoveryKeys({ status, onRegenerate, onRemoveTotp }: {
const remaining = status?.filter(isUnusedRecoveryCode).length ?? 0;
return (
- <OptionsSection title={t("multi_factor_authentication.totp_section_title")}>
+ <OptionsSection title={title}>
<OptionsRowWithButton
label={
<span className="recovery-codes-title">