Skip to content

armgit / ente

Public Imported from GitHub
0

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

Split

Manav Rathi093ff9c3955f
diff --git a/web/packages/accounts/services/user.ts b/web/packages/accounts/services/user.ts
index ce3dd2b183..61afd8f07e 100644
--- a/web/packages/accounts/services/user.ts
+++ b/web/packages/accounts/services/user.ts
@@ -43,12 +43,16 @@ const LocalUser = z.object({
token: z.string(),
});
-/** Locally available data for the logged in user */
+/**
+ * The local storage data about the user after they've logged in.
+ */
export type LocalUser = z.infer<typeof LocalUser>;
/**
* The local storage data about the user before login or signup is complete.
*
+ * [Note: Partial local user]
+ *
* During login or signup, the user object exists in various partial states in
* local storage.
*
@@ -63,13 +67,32 @@ export type LocalUser = z.infer<typeof LocalUser>;
*
* - Once they verify their TOTP based second factor, their {@link id} and
* {@link encryptedToken} will also get filled in.
+ *
+ * - TODO add more steps...
+ *
+ * So while the underlying storage is the same, we offer two APIs for code to
+ * obtain the user:
+ *
+ * - Before login is complete, or when it is unknown if login is complete or
+ * not, then {@link partialLocalUser} can be used to obtain a
+ * {@link LocalUser} with all of its properties set to be optional.
+ *
+ * - When we know that the login has completed, we can use either
+ * {@link localUser} (which returns `undefined` if our presumption is false)
+ * or {@link ensureLocalUser} (which throws if our presumption is false) to
+ * obtain an object with all the properties expected to be present for a
+ * locally persisted user set to be required.
*/
-// TODO: Start using me.
-export const PreLoginLocalUser = LocalUser.partial();
+export const partialLocalUser = (): Partial<LocalUser> | undefined => {
+ // TODO: duplicate of getData("user")
+ const s = localStorage.getItem("user");
+ if (!s) return undefined;
+ return LocalUser.partial().parse(JSON.parse(s));
+};
/**
* Return the logged-in user, if someone is indeed logged in. Otherwise return
- * `undefined` (TODO: That's not what it is doing...).
+ * `undefined`.
*
* The user's data is stored in the browser's localStorage. Thus, this function
* only works from the main thread, not from web workers (local storage is not
@@ -79,7 +102,8 @@ export const localUser = (): LocalUser | undefined => {
// TODO: duplicate of getData("user")
const s = localStorage.getItem("user");
if (!s) return undefined;
- return LocalUser.parse(JSON.parse(s));
+ const { success, data } = LocalUser.safeParse(JSON.parse(s));
+ return success ? data : undefined;
};
/**
diff --git a/web/packages/new/photos/services/settings.ts b/web/packages/new/photos/services/settings.ts
index 3d3649d211..fadea19a42 100644
--- a/web/packages/new/photos/services/settings.ts
+++ b/web/packages/new/photos/services/settings.ts
@@ -2,7 +2,7 @@
* @file Storage (in-memory, local, remote) and update of various settings.
*/
-import { localUser } from "ente-accounts/services/user";
+import { partialLocalUser } from "ente-accounts/services/user";
import { isDevBuild } from "ente-base/env";
import log from "ente-base/log";
import { updateShouldDisableCFUploadProxy } from "ente-gallery/services/upload";
@@ -188,16 +188,16 @@ const setSettingsSnapshot = (snapshot: Settings) => {
};
/**
- * Return `true` if this is a development build, and the current user is marked
- * as an "development" user.
+ * Return `true` if this is a development build, and the current user (if any)
+ * is marked as an "development" user.
*
* Emails that end in "@ente.io" are considered as dev users.
*/
export const isDevBuildAndUser = () => isDevBuild && isDevUserViaEmail();
const isDevUserViaEmail = () => {
- const user = localUser();
- return !!user?.email.endsWith("@ente.io");
+ const user = partialLocalUser();
+ return !!user?.email?.endsWith("@ente.io");
};
/**