Skip to content

Build your personal knowledge base with Trilium Notes

feat(oauth): toast on successful account connection after enrollment

Elian Doran3ad70939d833
diff --git a/apps/client/src/components/startup_checks.spec.ts b/apps/client/src/components/startup_checks.spec.ts
new file mode 100644
index 0000000000..759e1f6fef
--- /dev/null
+++ b/apps/client/src/components/startup_checks.spec.ts
@@ -0,0 +1,40 @@
+import { afterEach, describe, expect, it, vi } from "vitest";
+
+import toast from "../services/toast";
+
+import { showOAuthEnrollmentResultToast } from "./startup_checks";
+
+vi.mock("../services/toast", () => ({ default: { showMessage: vi.fn() } }));
+vi.mock("../services/i18n", () => ({ t: (key: string) => key }));
+
+const showMessage = vi.mocked(toast.showMessage);
+
+function setGlob(glob: Record<string, unknown> | undefined) {
+ (window as unknown as { glob?: unknown }).glob = glob;
+}
+
+describe("showOAuthEnrollmentResultToast", () => {
+ afterEach(() => {
+ vi.clearAllMocks();
+ setGlob(undefined);
+ });
+
+ it("toasts when the bootstrap reports a fresh enrollment", () => {
+ setGlob({ oauthJustEnrolled: true });
+ showOAuthEnrollmentResultToast();
+ expect(showMessage).toHaveBeenCalledWith("multi_factor_authentication.oauth_connect_success");
+ });
+
+ it("stays silent when the flag is absent or false", () => {
+ setGlob({ oauthJustEnrolled: false });
+ showOAuthEnrollmentResultToast();
+
+ setGlob({});
+ showOAuthEnrollmentResultToast();
+
+ setGlob(undefined);
+ showOAuthEnrollmentResultToast();
+
+ expect(showMessage).not.toHaveBeenCalled();
+ });
+});
diff --git a/apps/client/src/components/startup_checks.ts b/apps/client/src/components/startup_checks.ts
index b320d7e134..7f4fe22349 100644
--- a/apps/client/src/components/startup_checks.ts
+++ b/apps/client/src/components/startup_checks.ts
@@ -1,4 +1,6 @@
+import { t } from "../services/i18n";
import server from "../services/server";
+import toast from "../services/toast";
import Component from "./component";
// TODO: Deduplicate.
@@ -11,6 +13,9 @@ export class StartupChecks extends Component {
constructor() {
super();
this.checkCpuArchMismatch();
+ // Shared by desktop and mobile (both reach here via appContext.start), so the post-enrollment
+ // toast lives here rather than being duplicated in each entry point.
+ showOAuthEnrollmentResultToast();
}
async checkCpuArchMismatch() {
@@ -24,3 +29,15 @@ export class StartupChecks extends Component {
}
}
}
+
+/**
+ * Shows a one-shot "account connected" toast after the OAuth provider round-trip redirects back to the
+ * app root (which drops the Settings modal). The signal rides in the server's bootstrap payload
+ * (`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"));
+ }
+}
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 93d853dbe9..5e18d924b6 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -1768,7 +1768,8 @@
"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_disconnected": "Account disconnected.",
+ "oauth_connect_success": "Account connected successfully."
},
"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 e75452aaeb..eb2a0d6695 100644
--- a/apps/client/src/widgets/type_widgets/options/password.tsx
+++ b/apps/client/src/widgets/type_widgets/options/password.tsx
@@ -261,6 +261,8 @@ function OAuthStatusCard({ status, refreshStatus }: { status?: OAuthStatus, refr
// 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).
+ // The return lands on the app root and drops this modal; the resulting "connected" toast is driven by
+ // a one-shot bootstrap flag (see showOAuthEnrollmentResultToast), so nothing is tracked here.
const connectAccount = useCallback(() => {
window.location.href = "authenticate";
}, []);
diff --git a/apps/server/src/express.d.ts b/apps/server/src/express.d.ts
index b57e64250f..a5ff1b77ee 100644
--- a/apps/server/src/express.d.ts
+++ b/apps/server/src/express.d.ts
@@ -38,5 +38,11 @@ export declare module "express-session" {
* or an attempt before enrollment). Read and cleared by the login page so the message shows once.
*/
ssoError?: "wrong_account" | "not_enrolled";
+ /**
+ * One-shot flag set by the OIDC afterCallback when the owner binds their account for the first
+ * time. Read and cleared by /bootstrap so the client can show a single "account connected" toast
+ * after the post-enrollment redirect lands on the app root.
+ */
+ ssoJustEnrolled?: true;
}
}
diff --git a/apps/server/src/routes/index.ts b/apps/server/src/routes/index.ts
index 750aad3686..b51ec15d3e 100644
--- a/apps/server/src/routes/index.ts
+++ b/apps/server/src/routes/index.ts
@@ -67,10 +67,18 @@ export function bootstrap(req: Request, res: Response) {
const nativeTitleBarVisible = options.nativeTitleBarVisible === "true";
const iconPacks = iconPackService.getIconPacks();
+ // One-shot: consume the enrollment flag set by the OIDC afterCallback so the client toasts the
+ // successful connection exactly once after the post-enrollment redirect.
+ const oauthJustEnrolled = req.session.ssoJustEnrolled === true;
+ if (oauthJustEnrolled) {
+ delete req.session.ssoJustEnrolled;
+ }
+
res.send({
...commonItems,
dbInitialized: true,
csrfToken,
+ oauthJustEnrolled,
platform: process.platform,
hasNativeTitleBar: isElectron && nativeTitleBarVisible,
hasBackgroundEffects: options.backgroundEffects === "true"
diff --git a/apps/server/src/services/open_id.ts b/apps/server/src/services/open_id.ts
index b2ce1180ae..7e5da44926 100644
--- a/apps/server/src/services/open_id.ts
+++ b/apps/server/src/services/open_id.ts
@@ -174,6 +174,9 @@ function generateOAuthConfig(endSessionSupported = false) {
}
const incomingSubject = user.sub.toString();
+ // Distinguishes the owner binding their account for the first time (enrollment) from a routine
+ // login, so we can surface a one-shot "connected" toast only on enrollment (see below).
+ const isEnrollment = !openIDEncryption.isSubjectIdentifierSaved();
if (openIDEncryption.isSubjectIdentifierSaved()) {
// An account is already enrolled, so this is a login attempt. Only the enrolled identity
@@ -231,6 +234,11 @@ function generateOAuthConfig(endSessionSupported = false) {
totpEnabled: false,
ssoEnabled: true
};
+ // Set after regeneration (which wipes the prior session) so /bootstrap can deliver a one-shot
+ // "account connected" signal to the client when it reloads onto the app root post-redirect.
+ if (isEnrollment) {
+ req.session.ssoJustEnrolled = true;
+ }
return session;
},
diff --git a/packages/commons/src/lib/server_api.ts b/packages/commons/src/lib/server_api.ts
index 6bae31446e..cd518f0757 100644
--- a/packages/commons/src/lib/server_api.ts
+++ b/packages/commons/src/lib/server_api.ts
@@ -433,6 +433,11 @@ export type BootstrapDefinition = {
isRtl: boolean;
TRILIUM_SAFE_MODE: boolean;
componentId?: string;
+ /**
+ * True for exactly one bootstrap after the owner binds their OAuth account, letting the client show a
+ * one-shot "account connected" toast once the post-enrollment redirect lands on the app root.
+ */
+ oauthJustEnrolled?: boolean;
};
/**