Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Handle tab logout

Manav Rathi5df217c87e5f
diff --git a/web/apps/auth/src/pages/auth.tsx b/web/apps/auth/src/pages/auth.tsx
index 81d9968be6..9600eb68a9 100644
--- a/web/apps/auth/src/pages/auth.tsx
+++ b/web/apps/auth/src/pages/auth.tsx
@@ -3,6 +3,7 @@ import { stashRedirect } from "@/accounts/services/redirect";
import { EnteLogo } from "@/base/components/EnteLogo";
import { ActivityIndicator } from "@/base/components/mui/ActivityIndicator";
import { NavbarBase } from "@/base/components/Navbar";
+import { masterKeyFromSessionIfLoggedIn } from "@/base/session-store";
import {
HorizontalFlex,
VerticallyCentered,
@@ -10,7 +11,7 @@ import {
import OverflowMenu from "@ente/shared/components/OverflowMenu/menu";
import { OverflowMenuOption } from "@ente/shared/components/OverflowMenu/option";
import { AUTH_PAGES as PAGES } from "@ente/shared/constants/pages";
-import { ApiError, CustomError } from "@ente/shared/error";
+import { ApiError } from "@ente/shared/error";
import LogoutOutlined from "@mui/icons-material/LogoutOutlined";
import MoreHoriz from "@mui/icons-material/MoreHoriz";
import {
@@ -41,16 +42,17 @@ const Page: React.FC = () => {
useEffect(() => {
const fetchCodes = async () => {
+ const masterKey = await masterKeyFromSessionIfLoggedIn();
+ if (!masterKey) {
+ stashRedirect(PAGES.AUTH);
+ void router.push("/");
+ return;
+ }
+
try {
- setCodes(await getAuthCodes());
+ setCodes(await getAuthCodes(masterKey));
} catch (e) {
- if (
- e instanceof Error &&
- e.message == CustomError.KEY_MISSING
- ) {
- stashRedirect(PAGES.AUTH);
- void router.push("/");
- } else if (e instanceof ApiError && e.httpStatusCode == 401) {
+ if (e instanceof ApiError && e.httpStatusCode == 401) {
// We get back a 401 Unauthorized if the token is not valid.
showSessionExpiredDialog();
} else {
diff --git a/web/apps/auth/src/pages/index.tsx b/web/apps/auth/src/pages/index.tsx
index 81dd02b769..c3d16a7ce9 100644
--- a/web/apps/auth/src/pages/index.tsx
+++ b/web/apps/auth/src/pages/index.tsx
@@ -4,7 +4,7 @@ import React, { useEffect } from "react";
const Page: React.FC = () => {
const router = useRouter();
- useEffect(() => void router.push(PAGES.LOGIN), []);
+ useEffect(() => void router.push(PAGES.LOGIN), [router]);
return <></>;
};
diff --git a/web/apps/auth/src/services/remote.ts b/web/apps/auth/src/services/remote.ts
index da42a1b68e..7cca599484 100644
--- a/web/apps/auth/src/services/remote.ts
+++ b/web/apps/auth/src/services/remote.ts
@@ -7,8 +7,7 @@ import { ensureString } from "@/utils/ensure";
import { codeFromURIString, type Code } from "services/code";
import { z } from "zod";
-export const getAuthCodes = async (): Promise<Code[]> => {
- const masterKey = await masterKeyFromSession();
+export const getAuthCodes = async (masterKey: Uint8Array): Promise<Code[]> => {
const authenticatorEntityKey = await getAuthenticatorEntityKey();
if (!authenticatorEntityKey) {
// The user might not have stored any codes yet from the mobile app.
diff --git a/web/packages/base/session-store.ts b/web/packages/base/session-store.ts
index 989375941d..b7b386a4d7 100644
--- a/web/packages/base/session-store.ts
+++ b/web/packages/base/session-store.ts
@@ -8,18 +8,28 @@ import { toB64 } from "./crypto/libsodium";
* Precondition: The user should be logged in.
*/
export const masterKeyFromSession = async () => {
- // TODO: Same value as the deprecated SESSION_KEYS.ENCRYPTION_KEY.
- const value = sessionStorage.getItem("encryptionKey");
- if (!value) {
+ const key = await masterKeyFromSessionIfLoggedIn();
+ if (key) {
+ return key;
+ } else {
throw new Error(
"The user's master key was not found in session storage. Likely they are not logged in.",
);
}
+};
+
+/**
+ * Return the user's master key from session storage if they are logged in,
+ * otherwise return `undefined`.
+ */
+export const masterKeyFromSessionIfLoggedIn = async () => {
+ // TODO: Same value as the deprecated SESSION_KEYS.ENCRYPTION_KEY.
+ const value = sessionStorage.getItem("encryptionKey");
+ if (!value) return undefined;
const { encryptedData, key, nonce } = EncryptionKeyAttributes.parse(
JSON.parse(value),
);
-
return decryptBox({ encryptedData, nonce }, key);
};