Skip to content

armgit / ente

Public Imported from GitHub
0

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

Use

Manav Rathi3f6ef61204ee
diff --git a/web/packages/accounts/pages/credentials.tsx b/web/packages/accounts/pages/credentials.tsx
index 25de0668d3..227c513bfd 100644
--- a/web/packages/accounts/pages/credentials.tsx
+++ b/web/packages/accounts/pages/credentials.tsx
@@ -52,6 +52,7 @@ import {
passkeyVerificationRedirectURL,
} from "../services/passkey";
import { appHomeRoute } from "../services/redirect";
+import { checkSessionValidity } from "../services/session";
import {
configureSRP,
generateSRPSetupAttributes,
@@ -116,10 +117,34 @@ const Page: React.FC<PageProps> = ({ appContext }) => {
LS_KEYS.SRP_ATTRIBUTES,
);
- if (srpAttributes && user?.email) {
- void didPasswordChangeElsewhere(user.email, srpAttributes).then(
- (changed) => changed && showSessionExpiredDialog(),
- );
+ if (token) {
+ void checkSessionValidity()
+ .then((session) => {
+ switch (session.status) {
+ case "invalid":
+ showSessionExpiredDialog();
+ break;
+ case "valid":
+ if (session.updatedKeyAttributes) {
+ setData(
+ LS_KEYS.KEY_ATTRIBUTES,
+ session.updatedKeyAttributes,
+ );
+ // This should be a rare occurence, instead of
+ // building the scaffolding to update all the
+ // in-memory state, just reload everything.
+ router.reload();
+ }
+ }
+ })
+ .catch((e) => {
+ // Ignore errors since we shouldn't be logging the user out for
+ // potentially transient issues.
+ log.warn(
+ "Ignoring error when determining session validity",
+ e,
+ );
+ });
}
if (kekEncryptedAttributes && keyAttributes) {
diff --git a/web/packages/accounts/services/session.ts b/web/packages/accounts/services/session.ts
index 7d47ce452c..e4e3aa658d 100644
--- a/web/packages/accounts/services/session.ts
+++ b/web/packages/accounts/services/session.ts
@@ -1,14 +1,26 @@
import { authenticatedRequestHeaders } from "@/next/http";
import { apiOrigin } from "@ente/shared/network/api";
-import { LS_KEYS } from "@ente/shared/storage/localStorage";
+import { LS_KEYS, getData } from "@ente/shared/storage/localStorage";
import type { KeyAttributes } from "@ente/shared/user/types";
type SessionValidity =
| { status: "invalid" }
- | { status: "valid"; updatedKeyAttributes: KeyAttributes | undefined };
+ | { status: "valid"; updatedKeyAttributes?: KeyAttributes };
/**
- * [Note: Handle password changes]
+ * Check if the local token and/or key attributes we have are still valid.
+ *
+ * This function does not take any parameters because it reads the current state
+ * (key attributes) from local storage.
+ *
+ * @returns status "invalid" if the current token has been invalidated, "valid"
+ * otherwise. In case the {@link KeyAttributes} returned by remote are different
+ * from the ones we have locally, then the {@link updatedKeyAttributes} property
+ * will also be set alongwith the valid {@link status} in the result.
+ *
+ * ---
+ *
+ * [Note: Handling password changes]
*
* If the user changes their password on a different device, then we need to
* update our local state so that we use the latest password for verification.
@@ -18,12 +30,6 @@ type SessionValidity =
* to the 99% user experience (of normal unlocks) for the 1% case (they've
* changed their password elsewhere).
*
- * Another alternative would be to non-blockingly check if their password has
- * changed (e.g. by comparing the remote and local SRP attributes), and if so,
- * log them out. This would be a robust solution, except users might've chosen
- * the "Don't log me out of other devices" option when changing their password
- * from the mobile app.
- *
* The approach we instead use is to make an non-blocking /session-validity API
* call when this page is loaded. This API call tells us:
*
@@ -35,15 +41,8 @@ type SessionValidity =
* If the session has been invalidated, we log them out here too.
*
* If the key attributes we get are different from the ones we have locally, we
- * regenerate new ones locally, and then use those for verifying the password.
- *
- * It does not take any parameters because it reads the current state (key
- * attributes) from local storage.
- *
- * @returns status "invalid" if the current token has been invalidated, "valid"
- * otherwise. In case the {@link KeyAttributes} returned by remote are different
- * from the ones we have locally, then the {@link updatedKeyAttributes} property
- * will also be set alongwith the valid {@link status} in the result.
+ * regenerate new ones locally, and then use those for verifying the password
+ * subsequently.
*/
export const checkSessionValidity = async (): Promise<SessionValidity> => {
const url = `${apiOrigin()}/users/session-validity/v2`;