From 76f3c706c686d3f6be241c357caf97ee975bc07d Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Wed, 6 May 2026 16:38:28 -0400 Subject: [PATCH 1/7] apply new design to logout dialog --- .../res/css/views/dialogs/_LogoutDialog.pcss | 19 +- .../components/views/dialogs/LogoutDialog.tsx | 285 ++++++----------- apps/web/src/hooks/useKeyBackupStatus.ts | 66 ++++ apps/web/src/i18n/strings/en_EN.json | 11 +- .../views/dialogs/LogoutDialog-test.tsx | 20 +- .../__snapshots__/LogoutDialog-test.tsx.snap | 288 ++++++++++++------ .../settings/UserProfileSettings-test.tsx | 7 +- 7 files changed, 384 insertions(+), 312 deletions(-) create mode 100644 apps/web/src/hooks/useKeyBackupStatus.ts diff --git a/apps/web/res/css/views/dialogs/_LogoutDialog.pcss b/apps/web/res/css/views/dialogs/_LogoutDialog.pcss index 8f11f8c7c27..3213eec15d9 100644 --- a/apps/web/res/css/views/dialogs/_LogoutDialog.pcss +++ b/apps/web/res/css/views/dialogs/_LogoutDialog.pcss @@ -6,6 +6,21 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Com Please see LICENSE files in the repository root for full details. */ -.mx_LogoutDialog_ExportKeyAdvanced { - width: fit-content; +.mx_LogoutDialog { + width: 26rem; + + .mx_EncryptionCard { + gap: 0; + h2 { + text-align: center; + color: var(--cpd-color-text-primary); + } + .mx_EncryptionCard_emphasisedContent { + text-align: center; + margin-bottom: var(--cpd-space-12x); + p { + margin: 0; + } + } + } } diff --git a/apps/web/src/components/views/dialogs/LogoutDialog.tsx b/apps/web/src/components/views/dialogs/LogoutDialog.tsx index 830e5fb5898..e853a5dbaca 100644 --- a/apps/web/src/components/views/dialogs/LogoutDialog.tsx +++ b/apps/web/src/components/views/dialogs/LogoutDialog.tsx @@ -7,11 +7,14 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Com Please see LICENSE files in the repository root for full details. */ -import React, { lazy } from "react"; -import { logger } from "matrix-js-sdk/src/logger"; +import React, { type JSX } from "react"; import { type MatrixClient } from "matrix-js-sdk/src/matrix"; +import { Button } from "@vector-im/compound-web"; +import ErrorIcon from "@vector-im/compound-design-tokens/assets/web/icons/error-solid"; +import KeyIcon from "@vector-im/compound-design-tokens/assets/web/icons/key"; +import PopOutIcon from "@vector-im/compound-design-tokens/assets/web/icons/pop-out"; +import SignOutIcon from "@vector-im/compound-design-tokens/assets/web/icons/sign-out"; -import Modal from "../../../Modal"; import dis from "../../../dispatcher/dispatcher"; import { type OpenToTabPayload } from "../../../dispatcher/payloads/OpenToTabPayload"; import { Action } from "../../../dispatcher/actions"; @@ -21,39 +24,15 @@ import { MatrixClientPeg } from "../../../MatrixClientPeg"; import QuestionDialog from "./QuestionDialog"; import BaseDialog from "./BaseDialog"; import Spinner from "../elements/Spinner"; -import DialogButtons from "../elements/DialogButtons"; +import { BackupStatus, useKeyBackupStatus } from "../../../hooks/useKeyBackupStatus"; +import { EncryptionCard } from "../settings/encryption/EncryptionCard"; +import { EncryptionCardButtons } from "../settings/encryption/EncryptionCardButtons"; +import { EncryptionCardEmphasisedContent } from "../settings/encryption/EncryptionCardEmphasisedContent"; interface IProps { onFinished: (success: boolean) => void; } -enum BackupStatus { - /** we're trying to figure out if there is an active backup */ - LOADING, - - /** crypto is disabled in this client (so no need to back up) */ - NO_CRYPTO, - - /** Key backup is active and working */ - BACKUP_ACTIVE, - - /** there is a backup on the server but we are not backing up to it */ - SERVER_BACKUP_BUT_DISABLED, - - /** Key backup is set up but recovery (4s) is not */ - BACKUP_NO_RECOVERY, - - /** backup is not set up locally and there is no backup on the server */ - NO_BACKUP, - - /** there was an error fetching the state */ - ERROR, -} - -interface IState { - backupStatus: BackupStatus; -} - /** * Checks if the `LogoutDialog` should be shown instead of the simple logout flow. * The `LogoutDialog` will check the crypto recovery status of the account and @@ -61,191 +40,103 @@ interface IState { */ export async function shouldShowLogoutDialog(cli: MatrixClient): Promise { const crypto = cli?.getCrypto(); - if (!crypto) return false; - - // If any room is encrypted, we need to show the advanced logout flow - const allRooms = cli!.getRooms(); - for (const room of allRooms) { - const isE2e = await crypto.isEncryptionEnabledInRoom(room.roomId); - if (isE2e) return true; - } - - return false; + return !!crypto; } -export default class LogoutDialog extends React.Component { - public static defaultProps = { - onFinished: function () {}, - }; - - public constructor(props: IProps) { - super(props); - - this.state = { - backupStatus: BackupStatus.LOADING, - }; - } - - public componentDidMount(): void { - this.startLoadBackupStatus(); - } - - /** kick off the asynchronous calls to populate `state.backupStatus` in the background */ - private startLoadBackupStatus(): void { - this.loadBackupStatus().catch((e) => { - logger.log("Unable to fetch key backup status", e); - this.setState({ - backupStatus: BackupStatus.ERROR, - }); - }); - } - - private async loadBackupStatus(): Promise { - const client = MatrixClientPeg.safeGet(); - const crypto = client.getCrypto(); - if (!crypto) { - this.setState({ backupStatus: BackupStatus.NO_CRYPTO }); - return; - } - - if ((await crypto.getActiveSessionBackupVersion()) !== null) { - if (await crypto.isSecretStorageReady()) { - this.setState({ backupStatus: BackupStatus.BACKUP_ACTIVE }); - } else { - this.setState({ backupStatus: BackupStatus.BACKUP_NO_RECOVERY }); - } - return; - } - - // backup is not active. see if there is a backup version on the server we ought to back up to. - const backupInfo = await crypto.getKeyBackupInfo(); - this.setState({ backupStatus: backupInfo ? BackupStatus.SERVER_BACKUP_BUT_DISABLED : BackupStatus.NO_BACKUP }); - } - - private onExportE2eKeysClicked = (): void => { - Modal.createDialog( - lazy(() => import("../../../async-components/views/dialogs/security/ExportE2eKeysDialog")), - { - matrixClient: MatrixClientPeg.safeGet(), - }, - ); - }; +export default function LogoutDialog(props: IProps): JSX.Element { + const client = MatrixClientPeg.safeGet(); + const backupStatus = useKeyBackupStatus(client); - private onFinished = (confirmed?: boolean): void => { + const onFinished = (confirmed?: boolean): void => { if (confirmed) { dis.dispatch({ action: "logout" }); } + props.onFinished(!!confirmed); + }; + + const onLogoutConfirm = (): void => { + dis.dispatch({ action: "logout" }); + // close dialog - this.props.onFinished(!!confirmed); + props.onFinished(true); }; - private onSetRecoveryMethodClick = (): void => { - // Open the user settings dialog to the encryption tab and start the flow to reset encryption + const onGoToSettings = (): void => { + // Open the user settings dialog to the encryption tab and start the flow to get recovery key const payload: OpenToTabPayload = { action: Action.ViewUserSettings, initialTabId: UserTab.Encryption, + props: { + initialEncryptionState: "set_recovery_key", + }, }; dis.dispatch(payload); - // close dialog - this.props.onFinished(true); - }; - - private onLogoutConfirm = (): void => { - dis.dispatch({ action: "logout" }); - - // close dialog - this.props.onFinished(true); + props.onFinished(false); }; - /** - * Show a dialog prompting the user to set up their recovery method. - * - * Either: - * * There is no backup at all ({@link BackupStatus.NO_BACKUP}) - * * There is a backup set up but recovery (4s) is not ({@link BackupStatus.BACKUP_NO_RECOVERY}) - * * There is a backup on the server but we are not connected to it ({@link BackupStatus.SERVER_BACKUP_BUT_DISABLED}) - * * We were unable to pull the backup data ({@link BackupStatus.ERROR}). - * - * In all four cases, we should prompt the user to set up a method of recovery. - */ - private renderSetupRecoveryMethod(): React.ReactNode { - const description = ( -
-

{_t("auth|logout_dialog|setup_secure_backup_description_1")}

-

{_t("auth|logout_dialog|setup_secure_backup_description_2")}

-

{_t("encryption|setup_secure_backup|explainer")}

-
- ); - - const dialogContent = ( -
-
- {description} -
- - - -
- {_t("common|advanced")} -

- -

-
-
- ); - // Not quite a standard question dialog as the primary button cancels - // the action and does something else instead, whilst non-default button - // confirms the action. - return ( - - {dialogContent} - - ); - } - - public render(): React.ReactNode { - switch (this.state.backupStatus) { - case BackupStatus.LOADING: - // while we're deciding if we have backups, show a spinner - return ( - + + ); + + case BackupStatus.NO_CRYPTO: + case BackupStatus.BACKUP_ACTIVE: + return ( + + ); + + case BackupStatus.NO_BACKUP: + case BackupStatus.SERVER_BACKUP_BUT_DISABLED: + case BackupStatus.ERROR: + case BackupStatus.BACKUP_NO_RECOVERY: { + return ( + + - - - ); - - case BackupStatus.NO_CRYPTO: - case BackupStatus.BACKUP_ACTIVE: - return ( - - ); - - case BackupStatus.NO_BACKUP: - case BackupStatus.SERVER_BACKUP_BUT_DISABLED: - case BackupStatus.ERROR: - case BackupStatus.BACKUP_NO_RECOVERY: - return this.renderSetupRecoveryMethod(); + +

{_t("auth|logout_dialog|setup_secure_backup_description")}

+ +
+ + + + + + + ); } } } diff --git a/apps/web/src/hooks/useKeyBackupStatus.ts b/apps/web/src/hooks/useKeyBackupStatus.ts new file mode 100644 index 00000000000..be90c6cf78f --- /dev/null +++ b/apps/web/src/hooks/useKeyBackupStatus.ts @@ -0,0 +1,66 @@ +/* +Copyright 2026 Element Creations Ltd. + +SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial +Please see LICENSE files in the repository root for full details. +*/ + +import { type MatrixClient } from "matrix-js-sdk/src/matrix"; + +import { useAsyncMemo } from "./useAsyncMemo.ts"; + +/** + * The status of the user's key backup. + */ +export enum BackupStatus { + /** we're trying to figure out if there is an active backup */ + LOADING, + + /** crypto is disabled in this client (so no need to back up) */ + NO_CRYPTO, + + /** Key backup is active and working */ + BACKUP_ACTIVE, + + /** there is a backup on the server but we are not backing up to it */ + SERVER_BACKUP_BUT_DISABLED, + + /** Key backup is set up but recovery (4s) is not */ + BACKUP_NO_RECOVERY, + + /** backup is not set up locally and there is no backup on the server */ + NO_BACKUP, + + /** there was an error fetching the state */ + ERROR, +} + +/** + * Get the status of the user's key backup. + */ +export function useKeyBackupStatus(client: MatrixClient): BackupStatus { + return useAsyncMemo( + async () => { + const crypto = client.getCrypto(); + if (!crypto) return BackupStatus.NO_CRYPTO; + + try { + if ((await crypto.getActiveSessionBackupVersion()) !== null) { + if (await crypto.isSecretStorageReady()) { + return BackupStatus.BACKUP_ACTIVE; + } else { + return BackupStatus.BACKUP_NO_RECOVERY; + } + } + + // backup is not active. see if there is a backup version on the server we ought to back up to. + const backupInfo = await crypto.getKeyBackupInfo(); + return backupInfo ? BackupStatus.SERVER_BACKUP_BUT_DISABLED : BackupStatus.NO_BACKUP; + } catch { + return BackupStatus.ERROR; + } + }, + [], + BackupStatus.LOADING, + ); +} diff --git a/apps/web/src/i18n/strings/en_EN.json b/apps/web/src/i18n/strings/en_EN.json index c838f38bf84..81e1d8d3f9a 100644 --- a/apps/web/src/i18n/strings/en_EN.json +++ b/apps/web/src/i18n/strings/en_EN.json @@ -220,11 +220,9 @@ "log_in_new_account": "Log in to your new account.", "logout_dialog": { "description": "Are you sure you want to remove this device?", - "megolm_export": "Manually export keys", - "setup_key_backup_title": "You'll lose access to your encrypted messages", - "setup_secure_backup_description_1": "Encrypted messages are secured with end-to-end encryption. Only you and the recipient(s) have the keys to read these messages.", - "setup_secure_backup_description_2": "When you remove this device you won't be able to read encrypted messages unless you have the keys for them on your other devices, or backed them up to the server.", - "skip_key_backup": "I don't want my encrypted messages" + "setup_key_backup_title": "You're about to lose access to your encrypted chats", + "setup_secure_backup_description": "This is your only device. If you remove it you'll need a recovery key in order to confirm your digital identity and restore your encrypted chats the next time you sign in.", + "skip_key_backup": "Remove this device anyway" }, "misconfigured_body": "Ask your %(brand)s admin to check your config for incorrect or duplicate entries.", "misconfigured_title": "Your %(brand)s is misconfigured", @@ -1008,9 +1006,6 @@ "set_up_recovery": "Back up your chats", "set_up_recovery_toast_description": "Your chats are automatically backed up with end-to-end encryption. To restore this backup and retain your digital identity when you lose access to all your devices, you will need your recovery key.", "set_up_toast_title": "Set up Secure Backup", - "setup_secure_backup": { - "explainer": "Back up your keys before removing this device to avoid losing them." - }, "turn_on_key_storage": "Turn on key storage", "turn_on_key_storage_description": "This will allow you to view your chat history on any new devices and is required for backup of chats and digital identity.", "udd": { diff --git a/apps/web/test/unit-tests/components/views/dialogs/LogoutDialog-test.tsx b/apps/web/test/unit-tests/components/views/dialogs/LogoutDialog-test.tsx index e1bc70f9d51..41bca18caf6 100644 --- a/apps/web/test/unit-tests/components/views/dialogs/LogoutDialog-test.tsx +++ b/apps/web/test/unit-tests/components/views/dialogs/LogoutDialog-test.tsx @@ -56,33 +56,33 @@ describe("LogoutDialog", () => { mockCrypto.getActiveSessionBackupVersion.mockResolvedValue("1"); mockCrypto.isSecretStorageReady.mockResolvedValue(false); const rendered = renderComponent(); - await rendered.findByText("You'll lose access to your encrypted messages"); + await rendered.findByText("You're about to lose access to your encrypted chats"); }); - it("Prompts user to go to settings if there is a backup on the server", async () => { + it("Prompts user to set up recovery if there is a backup on the server but no secret storage", async () => { mockCrypto.getKeyBackupInfo.mockResolvedValue({} as KeyBackupInfo); const rendered = renderComponent(); - await rendered.findByText("Go to Settings"); + await rendered.findByText("Get recovery key"); expect(rendered.container).toMatchSnapshot(); jest.spyOn(dispatch, "dispatch"); - fireEvent.click(await screen.findByRole("button", { name: "Go to Settings" })); + fireEvent.click(await screen.findByRole("button", { name: "Get recovery key" })); await waitFor(() => expect(dispatch.dispatch).toHaveBeenCalledWith({ action: Action.ViewUserSettings, initialTabId: UserTab.Encryption, + props: { + initialEncryptionState: "set_recovery_key", + }, }), ); }); - it("Prompts user to go to settings if there is no backup on the server", async () => { + it("Prompts user to set up recovery if there is no backup on the server", async () => { mockCrypto.getKeyBackupInfo.mockResolvedValue(null); const rendered = renderComponent(); - await rendered.findByText("Go to Settings"); + await rendered.findByText("Get recovery key"); expect(rendered.container).toMatchSnapshot(); - - fireEvent.click(await screen.findByRole("button", { name: "Manually export keys" })); - await expect(screen.findByRole("heading", { name: "Export room keys" })).resolves.toBeInTheDocument(); }); describe("when there is an error fetching backups", () => { @@ -92,7 +92,7 @@ describe("LogoutDialog", () => { throw new Error("beep"); }); const rendered = renderComponent(); - await rendered.findByText("Go to Settings"); + await rendered.findByText("Get recovery key"); }); }); }); diff --git a/apps/web/test/unit-tests/components/views/dialogs/__snapshots__/LogoutDialog-test.tsx.snap b/apps/web/test/unit-tests/components/views/dialogs/__snapshots__/LogoutDialog-test.tsx.snap index 1951293f563..89b7fd7e71e 100644 --- a/apps/web/test/unit-tests/components/views/dialogs/__snapshots__/LogoutDialog-test.tsx.snap +++ b/apps/web/test/unit-tests/components/views/dialogs/__snapshots__/LogoutDialog-test.tsx.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing -exports[`LogoutDialog Prompts user to go to settings if there is a backup on the server 1`] = ` +exports[`LogoutDialog Prompts user to set up recovery if there is a backup on the server but no secret storage 1`] = `
`; -exports[`LogoutDialog Prompts user to go to settings if there is no backup on the server 1`] = ` +exports[`LogoutDialog Prompts user to set up recovery if there is no backup on the server 1`] = `
{ expect(await screen.findByText("Mocked EditInPlace: Alice")).toBeInTheDocument(); }); - it("signs out directly if no rooms are encrypted", async () => { + it("displays confirmation dialog if no rooms are encrypted", async () => { + jest.spyOn(Modal, "createDialog"); + renderProfileSettings(toastRack, client); const signOutButton = await screen.findByText("Remove this device"); await userEvent.click(signOutButton); - expect(dis.dispatch).toHaveBeenCalledWith({ action: "logout" }); + expect(Modal.createDialog).toHaveBeenCalled(); }); it("displays confirmation dialog if rooms are encrypted", async () => { From 9bef4c1ae8f6c6d3d067da73acea5814c1efddd9 Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Thu, 7 May 2026 19:14:58 -0400 Subject: [PATCH 2/7] factor out check for other verified devices --- .../src/hooks/useHasOtherVerifiedDevices.ts | 56 +++++++++++++++++++ apps/web/src/stores/SetupEncryptionStore.ts | 19 ++----- .../components/structures/MatrixChat-test.tsx | 9 ++- 3 files changed, 68 insertions(+), 16 deletions(-) create mode 100644 apps/web/src/hooks/useHasOtherVerifiedDevices.ts diff --git a/apps/web/src/hooks/useHasOtherVerifiedDevices.ts b/apps/web/src/hooks/useHasOtherVerifiedDevices.ts new file mode 100644 index 00000000000..58c7a9db824 --- /dev/null +++ b/apps/web/src/hooks/useHasOtherVerifiedDevices.ts @@ -0,0 +1,56 @@ +/* +Copyright 2026 Element Creations Ltd. + +SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial +Please see LICENSE files in the repository root for full details. +*/ + +import { type Device, type MatrixClient } from "matrix-js-sdk/src/matrix"; +import { type CryptoApi } from "matrix-js-sdk/src/crypto-api"; + +import { useAsyncMemo } from "./useAsyncMemo.ts"; +import { asyncSome } from "../utils/arrays"; + +/** + * Check whether the user has other verified devices, not counting dehydrated devices. + */ +export async function hasOtherVerifiedDevices( + ownUserId: string, + ownDeviceId: string, + crypto: CryptoApi | undefined, +): Promise { + if (!crypto) return null; + const userDevices: Iterable = (await crypto.getUserDeviceInfo([ownUserId])).get(ownUserId)?.values() ?? []; + + return asyncSome(userDevices, async (device) => { + // Ignore our own device. + if (device.deviceId === ownDeviceId) return false; + + // Ignore dehydrated devices. MSC3814 proposes that devices + // should set a `dehydrated` flag in the device key. + if (device.dehydrated) return false; + + // Ignore devices without an identity key. + if (!device.getIdentityKey()) return false; + + const verificationStatus = await crypto.getDeviceVerificationStatus(ownUserId, device.deviceId); + return !!verificationStatus?.signedByOwner; + }); +} + +/** + * Hook to check whether the user has other verified devices, not counting + * dehydrated devices. + */ +export function useHasOtherVerifiedDevices(client: MatrixClient): boolean | null | undefined { + return useAsyncMemo( + async () => { + const ownUserId = client.getUserId()!; + const ownDeviceId = client.getDeviceId()!; + const crypto = client.getCrypto(); + return await hasOtherVerifiedDevices(ownUserId, ownDeviceId, crypto); + }, + [], + undefined, + ); +} diff --git a/apps/web/src/stores/SetupEncryptionStore.ts b/apps/web/src/stores/SetupEncryptionStore.ts index 9e324b64965..659a524edd1 100644 --- a/apps/web/src/stores/SetupEncryptionStore.ts +++ b/apps/web/src/stores/SetupEncryptionStore.ts @@ -15,12 +15,12 @@ import { CryptoEvent, } from "matrix-js-sdk/src/crypto-api"; import { logger } from "matrix-js-sdk/src/logger"; -import { type Device, type SecretStorage } from "matrix-js-sdk/src/matrix"; +import { type SecretStorage } from "matrix-js-sdk/src/matrix"; import { MatrixClientPeg } from "../MatrixClientPeg"; import { AccessCancelledError, accessSecretStorage } from "../SecurityManager"; -import { asyncSome } from "../utils/arrays"; import { initialiseDehydrationIfEnabled } from "../utils/device/dehydration"; +import { hasOtherVerifiedDevices } from "../hooks/useHasOtherVerifiedDevices"; export enum Phase { Loading = 0, @@ -102,21 +102,10 @@ export class SetupEncryptionStore extends EventEmitter { } const ownUserId = cli.getUserId()!; + const ownDeviceId = cli.getDeviceId()!; const crypto = cli.getCrypto()!; // do we have any other verified devices which are E2EE which we can verify against? - const userDevices: Iterable = - (await crypto.getUserDeviceInfo([ownUserId])).get(ownUserId)?.values() ?? []; - this.hasDevicesToVerifyAgainst = await asyncSome(userDevices, async (device) => { - // Ignore dehydrated devices. MSC3814 proposes that devices - // should set a `dehydrated` flag in the device key. - if (device.dehydrated) return false; - - // ignore devices without an identity key - if (!device.getIdentityKey()) return false; - - const verificationStatus = await crypto.getDeviceVerificationStatus(ownUserId, device.deviceId); - return !!verificationStatus?.signedByOwner; - }); + this.hasDevicesToVerifyAgainst = (await hasOtherVerifiedDevices(ownUserId, ownDeviceId, crypto)) === true; this.phase = Phase.Intro; this.emit("update"); diff --git a/apps/web/test/unit-tests/components/structures/MatrixChat-test.tsx b/apps/web/test/unit-tests/components/structures/MatrixChat-test.tsx index bf53fe8910e..37250e22114 100644 --- a/apps/web/test/unit-tests/components/structures/MatrixChat-test.tsx +++ b/apps/web/test/unit-tests/components/structures/MatrixChat-test.tsx @@ -1196,7 +1196,14 @@ describe("", () => { .fn() .mockReturnValue( new Map([ - ["devid", { dehydrated: false, getIdentityKey: jest.fn().mockReturnValue("k") }], + [ + "devid", + { + deviceId: "devid", + dehydrated: false, + getIdentityKey: jest.fn().mockReturnValue("k"), + }, + ], ]), ), }), From 7631cbf438db2756e81580979be109ede7e8da26 Mon Sep 17 00:00:00 2001 From: Hubert Chathi Date: Thu, 7 May 2026 19:15:31 -0400 Subject: [PATCH 3/7] only show recovery warning when user has no other verified devices --- .../components/views/dialogs/LogoutDialog.tsx | 58 ++++--- apps/web/test/test-utils/client.ts | 1 + .../components/structures/MatrixChat-test.tsx | 26 ++- .../views/dialogs/LogoutDialog-test.tsx | 109 +++++++++++- .../__snapshots__/LogoutDialog-test.tsx.snap | 157 +++++++++++++++++- .../security/SetupEncryptionDialog-test.tsx | 2 + .../settings/UserProfileSettings-test.tsx | 1 + 7 files changed, 315 insertions(+), 39 deletions(-) diff --git a/apps/web/src/components/views/dialogs/LogoutDialog.tsx b/apps/web/src/components/views/dialogs/LogoutDialog.tsx index e853a5dbaca..92ebcaf49a9 100644 --- a/apps/web/src/components/views/dialogs/LogoutDialog.tsx +++ b/apps/web/src/components/views/dialogs/LogoutDialog.tsx @@ -25,6 +25,7 @@ import QuestionDialog from "./QuestionDialog"; import BaseDialog from "./BaseDialog"; import Spinner from "../elements/Spinner"; import { BackupStatus, useKeyBackupStatus } from "../../../hooks/useKeyBackupStatus"; +import { useHasOtherVerifiedDevices } from "../../../hooks/useHasOtherVerifiedDevices"; import { EncryptionCard } from "../settings/encryption/EncryptionCard"; import { EncryptionCardButtons } from "../settings/encryption/EncryptionCardButtons"; import { EncryptionCardEmphasisedContent } from "../settings/encryption/EncryptionCardEmphasisedContent"; @@ -46,6 +47,7 @@ export async function shouldShowLogoutDialog(cli: MatrixClient): Promise { if (confirmed) { @@ -75,31 +77,47 @@ export default function LogoutDialog(props: IProps): JSX.Element { props.onFinished(false); }; + // Dialog contents to show a spinner while deciding whether to prompt the + // user to set up recovery + function loading(): JSX.Element { + return ( + + + + ); + } + + // Dialog contents to confirm whether the user is sure if they want to log + // out. + function confirmLogout(): JSX.Element { + return ( + + ); + } + + if (hasOtherVerifiedDevices === undefined) { + return loading(); + } else if (hasOtherVerifiedDevices) { + return confirmLogout(); + } switch (backupStatus) { case BackupStatus.LOADING: - // while we're deciding if we have backups, show a spinner - return ( - - - - ); + return loading(); case BackupStatus.NO_CRYPTO: case BackupStatus.BACKUP_ACTIVE: - return ( - - ); + return confirmLogout(); case BackupStatus.NO_BACKUP: case BackupStatus.SERVER_BACKUP_BUT_DISABLED: diff --git a/apps/web/test/test-utils/client.ts b/apps/web/test/test-utils/client.ts index 7ab88da0b72..b54951ce42e 100644 --- a/apps/web/test/test-utils/client.ts +++ b/apps/web/test/test-utils/client.ts @@ -160,6 +160,7 @@ export const mockClientMethodsCrypto = (): Partial< }, getCrypto: jest.fn().mockReturnValue({ getUserDeviceInfo: jest.fn(), + getDeviceVerificationStatus: jest.fn().mockResolvedValue(null), getCrossSigningStatus: jest.fn().mockResolvedValue({ publicKeysOnDevice: true, privateKeysInSecretStorage: false, diff --git a/apps/web/test/unit-tests/components/structures/MatrixChat-test.tsx b/apps/web/test/unit-tests/components/structures/MatrixChat-test.tsx index 37250e22114..9d4084d2209 100644 --- a/apps/web/test/unit-tests/components/structures/MatrixChat-test.tsx +++ b/apps/web/test/unit-tests/components/structures/MatrixChat-test.tsx @@ -1192,20 +1192,18 @@ describe("", () => { getVersion: jest.fn().mockReturnValue("Version 0"), getVerificationRequestsToDeviceInProgress: jest.fn().mockReturnValue([]), getUserDeviceInfo: jest.fn().mockReturnValue({ - get: jest - .fn() - .mockReturnValue( - new Map([ - [ - "devid", - { - deviceId: "devid", - dehydrated: false, - getIdentityKey: jest.fn().mockReturnValue("k"), - }, - ], - ]), - ), + get: jest.fn().mockReturnValue( + new Map([ + [ + "devid", + { + deviceId: "devid", + dehydrated: false, + getIdentityKey: jest.fn().mockReturnValue("k"), + }, + ], + ]), + ), }), getUserVerificationStatus: jest .fn() diff --git a/apps/web/test/unit-tests/components/views/dialogs/LogoutDialog-test.tsx b/apps/web/test/unit-tests/components/views/dialogs/LogoutDialog-test.tsx index 41bca18caf6..40082be4ec7 100644 --- a/apps/web/test/unit-tests/components/views/dialogs/LogoutDialog-test.tsx +++ b/apps/web/test/unit-tests/components/views/dialogs/LogoutDialog-test.tsx @@ -8,11 +8,17 @@ Please see LICENSE files in the repository root for full details. import React from "react"; import { mocked, type MockedObject } from "jest-mock"; -import { type MatrixClient } from "matrix-js-sdk/src/matrix"; -import { type CryptoApi, type KeyBackupInfo } from "matrix-js-sdk/src/crypto-api"; +import { Device, DeviceVerification, type MatrixClient } from "matrix-js-sdk/src/matrix"; +import { type CryptoApi, DeviceVerificationStatus, type KeyBackupInfo } from "matrix-js-sdk/src/crypto-api"; import { fireEvent, render, type RenderResult, screen, waitFor } from "jest-matrix-react"; -import { filterConsole, getMockClientWithEventEmitter, mockClientMethodsCrypto } from "../../../../test-utils"; +import { + filterConsole, + getMockClientWithEventEmitter, + mockClientMethodsCrypto, + mockClientMethodsDevice, + mockClientMethodsUser, +} from "../../../../test-utils"; import LogoutDialog from "../../../../../src/components/views/dialogs/LogoutDialog"; import dispatch from "../../../../../src/dispatcher/dispatcher"; import { Action } from "../../../../../src/dispatcher/actions"; @@ -25,10 +31,13 @@ describe("LogoutDialog", () => { beforeEach(() => { mockClient = getMockClientWithEventEmitter({ ...mockClientMethodsCrypto(), + ...mockClientMethodsUser(), + ...mockClientMethodsDevice(), }); mockCrypto = mocked(mockClient.getCrypto()!); Object.assign(mockCrypto, { + getUserDeviceInfo: jest.fn().mockResolvedValue(new Map()), getActiveSessionBackupVersion: jest.fn().mockResolvedValue(null), }); }); @@ -52,6 +61,34 @@ describe("LogoutDialog", () => { await rendered.findByText("Are you sure you want to remove this device?"); }); + it("shows a regular dialog if the user has another verified device", async () => { + const userId = mockClient.getUserId()!; + mockCrypto.getUserDeviceInfo.mockResolvedValue( + new Map([ + [ + userId, + new Map([ + [ + "otherDevice", + new Device({ + deviceId: "otherDevice", + userId: userId, + algorithms: [], + keys: new Map([["curve25519:otherDevice", "akey"]]), + verified: DeviceVerification.Verified, + dehydrated: false, + }), + ], + ]), + ], + ]), + ); + mockCrypto.getDeviceVerificationStatus.mockResolvedValue(new DeviceVerificationStatus({ signedByOwner: true })); + + const rendered = renderComponent(); + await rendered.findByText("Are you sure you want to remove this device?"); + }); + it("prompts user to set up recovery if backups are enabled but recovery isn't", async () => { mockCrypto.getActiveSessionBackupVersion.mockResolvedValue("1"); mockCrypto.isSecretStorageReady.mockResolvedValue(false); @@ -85,6 +122,72 @@ describe("LogoutDialog", () => { expect(rendered.container).toMatchSnapshot(); }); + it("Prompts user to set up recovery if there is no backup on the server, and the user has other unverified/dehydrated devices", async () => { + const userId = mockClient.getUserId()!; + const testDeviceId = mockClient.getDeviceId()!; + mockCrypto.getUserDeviceInfo.mockResolvedValue( + new Map([ + [ + userId, + new Map([ + // the current device + [ + testDeviceId, + new Device({ + deviceId: testDeviceId, + userId: userId, + algorithms: [], + keys: new Map([["curve25519:test-device-id", "akey"]]), + verified: DeviceVerification.Verified, + dehydrated: false, + }), + ], + // a dehydrated device + [ + "dehydratedDevice", + new Device({ + deviceId: "otherDevice", + userId: userId, + algorithms: [], + keys: new Map([["curve25519:dehydratedDevice", "akey"]]), + verified: DeviceVerification.Verified, + dehydrated: true, + }), + ], + // an unverified device + [ + "otherDevice", + new Device({ + deviceId: "otherDevice", + userId: userId, + algorithms: [], + keys: new Map([["curve25519:otherDevice", "akey"]]), + verified: DeviceVerification.Unverified, + dehydrated: false, + }), + ], + ]), + ], + ]), + ); + mockCrypto.getDeviceVerificationStatus.mockImplementation(async (_userId: string, deviceId: string) => { + switch (deviceId) { + case testDeviceId: + case "dehydratedDevice": + return new DeviceVerificationStatus({ signedByOwner: true }); + case "otherDevice": + return new DeviceVerificationStatus({ signedByOwner: false }); + default: + throw new Error("Unknown device ID"); + } + }); + + mockCrypto.getKeyBackupInfo.mockResolvedValue(null); + const rendered = renderComponent(); + await rendered.findByText("Get recovery key"); + expect(rendered.container).toMatchSnapshot(); + }); + describe("when there is an error fetching backups", () => { filterConsole("Unable to fetch key backup status"); it("prompts user to go to settings", async () => { diff --git a/apps/web/test/unit-tests/components/views/dialogs/__snapshots__/LogoutDialog-test.tsx.snap b/apps/web/test/unit-tests/components/views/dialogs/__snapshots__/LogoutDialog-test.tsx.snap index 89b7fd7e71e..8d5fda1084d 100644 --- a/apps/web/test/unit-tests/components/views/dialogs/__snapshots__/LogoutDialog-test.tsx.snap +++ b/apps/web/test/unit-tests/components/views/dialogs/__snapshots__/LogoutDialog-test.tsx.snap @@ -126,7 +126,7 @@ exports[`LogoutDialog Prompts user to set up recovery if there is a backup on th
+ + + +
+
+
+
+`; + +exports[`LogoutDialog Prompts user to set up recovery if there is no backup on the server, and the user has other unverified/dehydrated devices 1`] = ` +
+
+