Skip to content

armgit / ente

Public Imported from GitHub
0

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

Revert shared web package churn

Prateek Sunalfdf54fff6563
diff --git a/web/packages/accounts/services/accounts-db.ts b/web/packages/accounts/services/accounts-db.ts
index 586e933225..a1b6214674 100644
--- a/web/packages/accounts/services/accounts-db.ts
+++ b/web/packages/accounts/services/accounts-db.ts
@@ -109,21 +109,6 @@ const LocalUser = z.object({
isTwoFactorEnabled: z.boolean().nullish().transform(nullToUndefined),
});
-const readLocalUserRecord = (): PartialLocalUser | undefined => {
- const jsonString = localStorage.getItem("user");
- if (!jsonString) return undefined;
- return PartialLocalUser.parse(JSON.parse(jsonString));
-};
-
-const writeLocalUserRecord = (user: PartialLocalUser | undefined) => {
- if (!user || Object.keys(user).length === 0) {
- localStorage.removeItem("user");
- return;
- }
-
- localStorage.setItem("user", JSON.stringify(user));
-};
-
/**
* Return the local storage value of the user's data.
*
@@ -134,8 +119,9 @@ const writeLocalUserRecord = (user: PartialLocalUser | undefined) => {
* Use {@link replaceSavedLocalUser} to updated the saved value.
*/
export const savedPartialLocalUser = (): PartialLocalUser | undefined => {
- const result = readLocalUserRecord();
- if (!result) return undefined;
+ const jsonString = localStorage.getItem("user");
+ if (!jsonString) return undefined;
+ const result = PartialLocalUser.parse(JSON.parse(jsonString));
void ensureTokensMatch(result);
return result;
};
@@ -148,9 +134,8 @@ export const savedPartialLocalUser = (): PartialLocalUser | undefined => {
* This method replaces the existing data. Use {@link updateSavedLocalUser} to
* update selected fields while keeping the other fields as it is.
*/
-export const replaceSavedLocalUser = (partialLocalUser: PartialLocalUser) => {
- writeLocalUserRecord(partialLocalUser);
-};
+export const replaceSavedLocalUser = (partialLocalUser: PartialLocalUser) =>
+ localStorage.setItem("user", JSON.stringify(partialLocalUser));
/**
* Partially update the saved user data.
@@ -182,6 +167,7 @@ export const updateSavedLocalUser = (updates: Partial<PartialLocalUser>) =>
export const savedLocalUser = (): LocalUser | undefined => {
const jsonString = localStorage.getItem("user");
if (!jsonString) return undefined;
+ // We might have some data, but not all of it. So do a non-throwing parse.
const { success, data } = LocalUser.safeParse(JSON.parse(jsonString));
if (success) void ensureTokensMatch(data);
return success ? data : undefined;
diff --git a/web/packages/base/token.ts b/web/packages/base/token.ts
index 2fab35dd9f..109322b339 100644
--- a/web/packages/base/token.ts
+++ b/web/packages/base/token.ts
@@ -19,27 +19,24 @@ export const ensureAuthToken = async () => {
* The user's auth token is stored in KV DB using {@link saveAuthToken} during
* the login / signup flow. This function returns that saved auth token.
*
+ * The underlying data is stored in IndexedDB, and can be accessed from web
+ * workers.
+ *
* If your code is running in a context where the user is already expected to be
* logged in, use {@link ensureAuthToken} instead.
*/
-export const savedAuthToken = async () => {
- return getKVS("token");
-};
+export const savedAuthToken = () => getKVS("token");
/**
- * Save the user's auth token.
+ * Save the user's auth token in KV DB.
*
* This is the setter corresponding to {@link savedAuthToken}.
*/
-export const saveAuthToken = (token: string) => {
- return setKV("token", token);
-};
+export const saveAuthToken = (token: string) => setKV("token", token);
/**
- * Remove the user's auth token.
+ * Remove the user's auth token from KV DB.
*
* See {@link saveAuthToken}.
*/
-export const removeAuthToken = () => {
- return removeKV("token");
-};
+export const removeAuthToken = () => removeKV("token");