Skip to content

armgit / ente

Public Imported from GitHub
0

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

Redirect

Manav Rathi671199e28638
diff --git a/web/packages/accounts/components/utils/use-redirect.ts b/web/packages/accounts/components/utils/use-redirect.ts
new file mode 100644
index 0000000000..981debc5bd
--- /dev/null
+++ b/web/packages/accounts/components/utils/use-redirect.ts
@@ -0,0 +1,24 @@
+import { haveCredentialsInSession } from "@/base/session-store";
+import { useRouter } from "next/router";
+import { useEffect } from "react";
+import { stashRedirect } from "../../services/redirect";
+
+/**
+ * Redirect to the appropriate credential step (password reverify, or a full
+ * login) and then back to the current page if the user's master key is not
+ * present in the session storage.
+ *
+ * @param currentPageSlug The slug for the current page where this hook is being
+ * used. If a redirect happens, then this slug will be stashed and used to
+ * redirect back here after the credentials have been obtained.
+ */
+export const useRedirectIfNeedsCredentials = (currentPageSlug: string) => {
+ const router = useRouter();
+
+ useEffect(() => {
+ if (!haveCredentialsInSession()) {
+ stashRedirect(currentPageSlug);
+ void router.push("/");
+ }
+ }, [router]);
+};
diff --git a/web/packages/base/session-store.ts b/web/packages/base/session-store.ts
index b7b386a4d7..6b9b38387f 100644
--- a/web/packages/base/session-store.ts
+++ b/web/packages/base/session-store.ts
@@ -3,7 +3,7 @@ import { decryptBox } from "./crypto";
import { toB64 } from "./crypto/libsodium";
/**
- * Return the user's master key from session storage.
+ * Return the decrypted user's master key from session storage.
*
* Precondition: The user should be logged in.
*/
@@ -19,8 +19,18 @@ export const masterKeyFromSession = async () => {
};
/**
- * Return the user's master key from session storage if they are logged in,
- * otherwise return `undefined`.
+ * Return `true` if the user's encrypted master key is present in the session.
+ *
+ * Use {@link masterKeyFromSessionIfLoggedIn} to get the actual master key after
+ * decrypting it. This function is instead useful as a quick check to verify if
+ * we have credentials at hand or not.
+ */
+export const haveCredentialsInSession = () =>
+ !!sessionStorage.getItem("encryptionKey");
+
+/**
+ * Return the decrypted 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.
diff --git a/web/packages/new/photos/pages/duplicates.tsx b/web/packages/new/photos/pages/duplicates.tsx
index 017a254034..1078d7f83a 100644
--- a/web/packages/new/photos/pages/duplicates.tsx
+++ b/web/packages/new/photos/pages/duplicates.tsx
@@ -1,3 +1,4 @@
+import { useRedirectIfNeedsCredentials } from "@/accounts/components/utils/use-redirect";
import { ActivityErrorIndicator } from "@/base/components/ErrorIndicator";
import { ActivityIndicator } from "@/base/components/mui/ActivityIndicator";
import { CenteredFill } from "@/base/components/mui/Container";
@@ -24,6 +25,8 @@ const Page: React.FC = () => {
const [state, dispatch] = useReducer(dedupReducer, initialDedupState);
+ useRedirectIfNeedsCredentials("/duplicates");
+
useEffect(() => {
// TODO: Remove me
showNavBar(false);