Skip to content

Build your personal knowledge base with Trilium Notes

feat(oauth): name the account and provider in connect/disconnect toasts

Elian Doran84573fb598b8
diff --git a/apps/client/src/components/startup_checks.spec.ts b/apps/client/src/components/startup_checks.spec.ts
index 759e1f6fef..e03683f4b6 100644
--- a/apps/client/src/components/startup_checks.spec.ts
+++ b/apps/client/src/components/startup_checks.spec.ts
@@ -1,12 +1,18 @@
import { afterEach, describe, expect, it, vi } from "vitest";
+import server from "../services/server";
import toast from "../services/toast";
import { showOAuthEnrollmentResultToast } from "./startup_checks";
+vi.mock("../services/server", () => ({ default: { get: vi.fn() } }));
vi.mock("../services/toast", () => ({ default: { showMessage: vi.fn() } }));
-vi.mock("../services/i18n", () => ({ t: (key: string) => key }));
+// Echo interpolation values so assertions can verify the resolved account/provider.
+vi.mock("../services/i18n", () => ({
+ t: (key: string, opts?: Record<string, unknown>) => (opts ? `${key} ${JSON.stringify(opts)}` : key)
+}));
+const serverGet = vi.mocked(server.get);
const showMessage = vi.mocked(toast.showMessage);
function setGlob(glob: Record<string, unknown> | undefined) {
@@ -19,22 +25,36 @@ describe("showOAuthEnrollmentResultToast", () => {
setGlob(undefined);
});
- it("toasts when the bootstrap reports a fresh enrollment", () => {
+ it("toasts the connected account/provider when the bootstrap reports a fresh enrollment", async () => {
setGlob({ oauthJustEnrolled: true });
- showOAuthEnrollmentResultToast();
- expect(showMessage).toHaveBeenCalledWith("multi_factor_authentication.oauth_connect_success");
+ serverGet.mockResolvedValue({ email: "[email protected]", issuerName: "Acme" });
+
+ await showOAuthEnrollmentResultToast();
+
+ expect(serverGet).toHaveBeenCalledWith("oauth/status");
+ const message = showMessage.mock.calls[0]?.[0];
+ expect(message).toContain("oauth_connect_success");
+ expect(message).toContain("[email protected]");
+ expect(message).toContain("Acme");
});
- it("stays silent when the flag is absent or false", () => {
- setGlob({ oauthJustEnrolled: false });
- showOAuthEnrollmentResultToast();
+ it("falls back to a generic message when the status probe fails", async () => {
+ setGlob({ oauthJustEnrolled: true });
+ serverGet.mockRejectedValue(new Error("network down"));
+
+ await showOAuthEnrollmentResultToast();
+
+ expect(showMessage).toHaveBeenCalledWith("multi_factor_authentication.oauth_connect_success_generic");
+ });
+ it("does nothing without the bootstrap flag", async () => {
setGlob({});
- showOAuthEnrollmentResultToast();
+ await showOAuthEnrollmentResultToast();
setGlob(undefined);
- showOAuthEnrollmentResultToast();
+ await showOAuthEnrollmentResultToast();
+ expect(serverGet).not.toHaveBeenCalled();
expect(showMessage).not.toHaveBeenCalled();
});
});
diff --git a/apps/client/src/components/startup_checks.ts b/apps/client/src/components/startup_checks.ts
index 7f4fe22349..013824e4ec 100644
--- a/apps/client/src/components/startup_checks.ts
+++ b/apps/client/src/components/startup_checks.ts
@@ -1,4 +1,7 @@
+import type { OAuthStatus } from "@triliumnext/commons";
+
import { t } from "../services/i18n";
+import { oauthAccountLabel, oauthProviderDisplayName } from "../services/oauth_status";
import server from "../services/server";
import toast from "../services/toast";
import Component from "./component";
@@ -36,8 +39,19 @@ export class StartupChecks extends Component {
* (`window.glob.oauthJustEnrolled`, set once by the OIDC afterCallback and cleared by /bootstrap), so
* nothing has to be stored on the client across the redirect.
*/
-export function showOAuthEnrollmentResultToast() {
- if (window.glob?.oauthJustEnrolled) {
- toast.showMessage(t("multi_factor_authentication.oauth_connect_success"));
+export async function showOAuthEnrollmentResultToast() {
+ if (!window.glob?.oauthJustEnrolled) {
+ return;
+ }
+
+ try {
+ const status = await server.get<OAuthStatus>("oauth/status");
+ toast.showMessage(t("multi_factor_authentication.oauth_connect_success", {
+ account: oauthAccountLabel(status),
+ provider: oauthProviderDisplayName(status)
+ }));
+ } catch {
+ // Couldn't resolve the account details — still confirm the connection generically.
+ toast.showMessage(t("multi_factor_authentication.oauth_connect_success_generic"));
}
}
diff --git a/apps/client/src/services/oauth_status.spec.ts b/apps/client/src/services/oauth_status.spec.ts
new file mode 100644
index 0000000000..ef6ba40b1f
--- /dev/null
+++ b/apps/client/src/services/oauth_status.spec.ts
@@ -0,0 +1,25 @@
+import { describe, expect, it, vi } from "vitest";
+
+import { oauthAccountLabel, oauthProviderDisplayName } from "./oauth_status";
+
+// t returns the key, so generic fallbacks surface as their translation key.
+vi.mock("./i18n", () => ({ t: (key: string) => key }));
+
+describe("oauthAccountLabel", () => {
+ it("prefers email, falls back to name, then a generic label", () => {
+ expect(oauthAccountLabel({ email: "[email protected]", name: "Alice" } as never)).toBe("[email protected]");
+ expect(oauthAccountLabel({ name: "Alice" } as never)).toBe("Alice");
+ expect(oauthAccountLabel({} as never)).toBe("multi_factor_authentication.oauth_account_unknown");
+ expect(oauthAccountLabel(undefined)).toBe("multi_factor_authentication.oauth_account_unknown");
+ });
+});
+
+describe("oauthProviderDisplayName", () => {
+ it("prefers issuer name, falls back to the issuer URL host, then a generic label", () => {
+ expect(oauthProviderDisplayName({ issuerName: "Acme", issuerUrl: "https://id.acme.com" } as never)).toBe("Acme");
+ expect(oauthProviderDisplayName({ issuerUrl: "https://id.acme.com/realms/x" } as never)).toBe("id.acme.com");
+ // A non-URL issuer string is returned as-is rather than throwing.
+ expect(oauthProviderDisplayName({ issuerUrl: "not a url" } as never)).toBe("not a url");
+ expect(oauthProviderDisplayName({} as never)).toBe("multi_factor_authentication.oauth_provider_unknown");
+ });
+});
diff --git a/apps/client/src/services/oauth_status.ts b/apps/client/src/services/oauth_status.ts
new file mode 100644
index 0000000000..56ab93c1cc
--- /dev/null
+++ b/apps/client/src/services/oauth_status.ts
@@ -0,0 +1,28 @@
+import type { OAuthStatus } from "@triliumnext/commons";
+
+import { t } from "./i18n";
+
+/** Human label for the connected account: its email, then display name, then a generic fallback. */
+export function oauthAccountLabel(status?: OAuthStatus) {
+ return status?.email || status?.name || t("multi_factor_authentication.oauth_account_unknown");
+}
+
+/**
+ * Provider display name: the configured issuer name, falling back to the host of the issuer URL
+ * (e.g. "auth.example.com"), and finally to a generic label when neither is available.
+ */
+export function oauthProviderDisplayName(status?: OAuthStatus) {
+ if (status?.issuerName) {
+ return status.issuerName;
+ }
+
+ if (status?.issuerUrl) {
+ try {
+ return new URL(status.issuerUrl).host;
+ } catch {
+ return status.issuerUrl;
+ }
+ }
+
+ return t("multi_factor_authentication.oauth_provider_unknown");
+}
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 5e18d924b6..12a55b099f 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -1768,8 +1768,10 @@
"oauth_disconnect_description": "Unbind the connected account. Sign-in will fall back to your password until you connect an account again.",
"oauth_disconnect_button": "Disconnect",
"oauth_disconnect_confirm": "Are you sure you want to disconnect this account? Sign-in will fall back to your password until you connect an account again.",
- "oauth_disconnected": "Account disconnected.",
- "oauth_connect_success": "Account connected successfully."
+ "oauth_disconnected": "Disconnected {{account}} from {{provider}}.",
+ "oauth_connect_success": "Connected to {{provider}} as {{account}}.",
+ "oauth_connect_success_generic": "Account connected successfully.",
+ "oauth_account_unknown": "your account"
},
"shortcuts": {
"keyboard_shortcuts": "Keyboard Shortcuts",
diff --git a/apps/client/src/widgets/type_widgets/options/password.tsx b/apps/client/src/widgets/type_widgets/options/password.tsx
index eb2a0d6695..ae383a044f 100644
--- a/apps/client/src/widgets/type_widgets/options/password.tsx
+++ b/apps/client/src/widgets/type_widgets/options/password.tsx
@@ -6,6 +6,7 @@ import { useCallback, useEffect, useState } from "preact/hooks";
import dialog from "../../../services/dialog";
import { t } from "../../../services/i18n";
+import { oauthAccountLabel, oauthProviderDisplayName } from "../../../services/oauth_status";
import protected_session_holder from "../../../services/protected_session_holder";
import server from "../../../services/server";
import toast from "../../../services/toast";
@@ -272,10 +273,14 @@ function OAuthStatusCard({ status, refreshStatus }: { status?: OAuthStatus, refr
return;
}
+ // Resolve the labels before disconnecting, while the account is still bound.
+ const account = oauthAccountLabel(status);
+ const provider = oauthProviderDisplayName(status);
+
await server.post("oauth/disconnect");
- toast.showMessage(t("multi_factor_authentication.oauth_disconnected"));
+ toast.showMessage(t("multi_factor_authentication.oauth_disconnected", { account, provider }));
refreshStatus();
- }, [ refreshStatus ]);
+ }, [ status, refreshStatus ]);
return (
<OptionsSection
@@ -389,26 +394,6 @@ function OAuthProviderIcon({ src }: { src?: string }) {
return <img className="oauth-provider-icon" src={src} alt="" onError={() => setFailed(true)} />;
}
-/**
- * Resolves the provider's display name: the configured issuer name, falling back to the host of the
- * issuer URL (e.g. "auth.example.com"), and finally to a generic label when neither is available.
- */
-function oauthProviderDisplayName(status?: OAuthStatus) {
- if (status?.issuerName) {
- return status.issuerName;
- }
-
- if (status?.issuerUrl) {
- try {
- return new URL(status.issuerUrl).host;
- } catch {
- return status.issuerUrl;
- }
- }
-
- return t("multi_factor_authentication.oauth_provider_unknown");
-}
-
/**
* "How to enable" instructions for OAuth, shown in the not-configured admonition's collapsible. Lists
* the config.ini section and the equivalent environment variables the server reads — OAuth has no