Skip to content

armgit / ente

Public Imported from GitHub
0

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

[desktop] Add IndexedDB circuit breaker

Manav Rathi0cef0656f31c
diff --git a/web/apps/auth/src/pages/_app.tsx b/web/apps/auth/src/pages/_app.tsx
index e3c92a117a..187f28c706 100644
--- a/web/apps/auth/src/pages/_app.tsx
+++ b/web/apps/auth/src/pages/_app.tsx
@@ -38,7 +38,6 @@ const App: React.FC<AppProps> = ({ Component, pageProps }) => {
useEffect(() => {
const user = getData(LS_KEYS.USER) as User | undefined | null;
- void migrateKVToken(user);
logStartupBanner(user?.id);
HTTPService.setHeaders({ "X-Client-Package": clientPackageName });
}, []);
diff --git a/web/apps/photos/src/pages/_app.tsx b/web/apps/photos/src/pages/_app.tsx
index d551c03017..98bbec1235 100644
--- a/web/apps/photos/src/pages/_app.tsx
+++ b/web/apps/photos/src/pages/_app.tsx
@@ -34,8 +34,8 @@ import { AppContext } from "@/new/photos/types/context";
import HTTPService from "@ente/shared/network/HTTPService";
import {
getData,
+ isLocalStorageAndIndexedDBMismatch,
LS_KEYS,
- migrateKVToken,
} from "@ente/shared/storage/localStorage";
import type { User } from "@ente/shared/user/types";
import "@fontsource-variable/inter";
@@ -68,13 +68,21 @@ const App: React.FC<AppProps> = ({ Component, pageProps }) => {
const [watchFolderView, setWatchFolderView] = useState(false);
+ const logout = useCallback(() => void photosLogout(), []);
+
useEffect(() => {
const user = getData(LS_KEYS.USER) as User | undefined | null;
- void migrateKVToken(user);
logStartupBanner(user?.id);
HTTPService.setHeaders({ "X-Client-Package": clientPackageName });
- void runMigrations();
- }, []);
+ void isLocalStorageAndIndexedDBMismatch().then((mismatch) => {
+ if (mismatch) {
+ log.error("Logging out (IndexedDB and local storage mismatch)");
+ return logout();
+ } else {
+ return runMigrations();
+ }
+ });
+ }, [logout]);
useEffect(() => {
const electron = globalThis.electron;
@@ -149,8 +157,6 @@ const App: React.FC<AppProps> = ({ Component, pageProps }) => {
}, 0);
}, []);
- const logout = useCallback(() => void photosLogout(), []);
-
const appContext = useMemo(
() => ({
showLoadingBar,
diff --git a/web/packages/shared/storage/localStorage/index.ts b/web/packages/shared/storage/localStorage/index.ts
index c11f3d2d60..56baa1ab0b 100644
--- a/web/packages/shared/storage/localStorage/index.ts
+++ b/web/packages/shared/storage/localStorage/index.ts
@@ -95,3 +95,20 @@ export const migrateKVToken = async (user: unknown) => {
"The user's token was present in local storage but not in IndexedDB",
);
};
+
+/**
+ * Return true if the user's data is in local storage but not in IndexedDB.
+ *
+ * This acts a sanity check on IndexedDB by ensuring that if the user has a
+ * token in local storage, then it should also be present in IndexedDB.
+ */
+export const isLocalStorageAndIndexedDBMismatch = async () => {
+ const oldLSUser = getData(LS_KEYS.USER);
+ return (
+ oldLSUser &&
+ typeof oldLSUser == "object" &&
+ "token" in oldLSUser &&
+ typeof oldLSUser.token == "string" &&
+ !(await getKVS("token"))
+ );
+};