Skip to content

armgit / ente

Public Imported from GitHub
0

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

fix(appLock): enhance-session-hydration-and-bootstrap-logic

Aswin Asokc1eecdbf836b
diff --git a/web/packages/new/photos/components/utils/use-app-lock.ts b/web/packages/new/photos/components/utils/use-app-lock.ts
index c487f3fa1f..5cd62d6133 100644
--- a/web/packages/new/photos/components/utils/use-app-lock.ts
+++ b/web/packages/new/photos/components/utils/use-app-lock.ts
@@ -2,6 +2,7 @@ import {
subscribeMainWindowBlur,
subscribeMainWindowFocus,
} from "ente-base/electron";
+import log from "ente-base/log";
import { updateSessionFromElectronSafeStorageIfNeeded } from "ente-base/session";
import { useEffect, useRef, useState } from "react";
import {
@@ -14,6 +15,52 @@ import {
type AppLockState,
} from "../../services/app-lock";
+const hydrateSessionFromSafeStorageIfNeeded = async () => {
+ try {
+ /**
+ * The current session's master key might already exist in the OS's safe
+ * storage, so if found then write it back into browser sessionStorage.
+ *
+ * Without this the user would need to re-enter the password on every
+ * desktop launch.
+ */
+ await updateSessionFromElectronSafeStorageIfNeeded();
+ } catch (e) {
+ log.warn(
+ "Failed to hydrate session from Electron safe storage during app lock bootstrap",
+ e,
+ );
+ }
+};
+
+const refreshBootstrapAppLockState = async () => {
+ try {
+ /**
+ * The app lock config is persisted across sessions, and refreshing it
+ * here recomputes the lock state after session hydration.
+ */
+ await refreshAppLockStateFromSession();
+ } catch (e) {
+ log.error("Failed to refresh app lock state during bootstrap", e);
+ }
+};
+
+const bootstrapAppLock = async () => {
+ try {
+ await initAppLock();
+ } catch (e) {
+ log.error("Failed to initialize app lock during bootstrap", e);
+ return;
+ }
+
+ if (!appLockSnapshot().enabled) {
+ return;
+ }
+
+ await hydrateSessionFromSafeStorageIfNeeded();
+ await refreshBootstrapAppLockState();
+};
+
/**
* Initialize app lock and return true once app-lock gated rendering can proceed.
*
@@ -21,32 +68,24 @@ import {
*/
export const useSetupAppLock = () => {
const [isAppLockReady, setIsAppLockReady] = useState(false);
+ const didCancelRef = useRef(false);
useEffect(() => {
- void (async () => {
- await initAppLock();
- if (!appLockSnapshot().enabled) {
- setIsAppLockReady(true);
- return;
- }
+ didCancelRef.current = false;
+ void (async () => {
try {
- /**
- * The current session's master key might be already existing in the OS's safe
- * storage, so if that is foudn then writing it to the browser sessionStorage
- *
- * Without this the user will have to enter the password again on every launch.
- */
- await updateSessionFromElectronSafeStorageIfNeeded();
+ await bootstrapAppLock();
} finally {
- /**
- * The app lock config is actually persisted across sessions, and for refreshing this
- * the user must haveMasterKeyInSession().
- */
- await refreshAppLockStateFromSession();
- setIsAppLockReady(true);
+ if (!didCancelRef.current) {
+ setIsAppLockReady(true);
+ }
}
})();
+
+ return () => {
+ didCancelRef.current = true;
+ };
}, []);
return isAppLockReady;