Skip to content

armgit / ente

Public Imported from GitHub
0

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

Split

Manav Rathi81eff305d34b
diff --git a/web/packages/accounts/pages/change-password.tsx b/web/packages/accounts/pages/change-password.tsx
index 26147db887..d7219860b9 100644
--- a/web/packages/accounts/pages/change-password.tsx
+++ b/web/packages/accounts/pages/change-password.tsx
@@ -19,11 +19,13 @@ import {
} from "ente-accounts/services/srp";
import {
ensureSavedKeyAttributes,
+ localUser,
+ type LocalUser,
type UpdatedKey,
- type User,
} from "ente-accounts/services/user";
import { generateAndSaveIntermediateKeyAttributes } from "ente-accounts/utils/helpers";
import { LinkButton } from "ente-base/components/LinkButton";
+import { LoadingIndicator } from "ente-base/components/loaders";
import { sharedCryptoWorker } from "ente-base/crypto";
import type { DerivedKey } from "ente-base/crypto/types";
import {
@@ -33,25 +35,40 @@ import {
import { getData, setData } from "ente-shared/storage/localStorage";
import { t } from "i18next";
import { useRouter } from "next/router";
-import { useEffect, useState } from "react";
+import React, { useEffect, useState } from "react";
+/**
+ * A page that allows a logged-in user to change their password.
+ */
const Page: React.FC = () => {
- const [token, setToken] = useState<string>();
- const [user, setUser] = useState<User>();
+ const [user, setUser] = useState<LocalUser>();
const router = useRouter();
useEffect(() => {
- const user = getData("user");
- setUser(user);
- if (!user?.token) {
+ const user = localUser();
+ if (user) {
+ setUser(user);
+ } else {
stashRedirect("/change-password");
void router.push("/");
- } else {
- setToken(user.token);
}
}, [router]);
+ return user ? <PageContents {...{ user }} /> : <LoadingIndicator />;
+};
+
+export default Page;
+
+interface PageContentsProps {
+ user: LocalUser;
+}
+
+const PageContents: React.FC<PageContentsProps> = ({ user }) => {
+ const token = user.token;
+
+ const router = useRouter();
+
const onSubmit: SetPasswordFormProps["callback"] = async (
passphrase,
setFieldError,
@@ -89,7 +106,7 @@ const Page: React.FC = () => {
const srpA = convertBufferToBase64(srpClient.computeA());
- const { setupID, srpB } = await startSRPSetup(token!, {
+ const { setupID, srpB } = await startSRPSetup(token, {
srpUserID,
srpSalt,
srpVerifier,
@@ -100,18 +117,16 @@ const Page: React.FC = () => {
const srpM1 = convertBufferToBase64(srpClient.computeM1());
- await updateSRPAndKeys(token!, {
+ await updateSRPAndKeys(token, {
setupID,
srpM1,
updatedKeyAttr: updatedKey,
});
// Update the SRP attributes that are stored locally.
- if (user?.email) {
- const srpAttributes = await getSRPAttributes(user.email);
- if (srpAttributes) {
- setData("srpAttributes", srpAttributes);
- }
+ const srpAttributes = await getSRPAttributes(user.email);
+ if (srpAttributes) {
+ setData("srpAttributes", srpAttributes);
}
const updatedKeyAttributes = Object.assign(keyAttributes, updatedKey);
@@ -131,12 +146,11 @@ const Page: React.FC = () => {
void router.push(appHomeRoute);
};
- // TODO: Handle the case where user is not loaded yet.
return (
<AccountsPageContents>
<AccountsPageTitle>{t("change_password")}</AccountsPageTitle>
<SetPasswordForm
- userEmail={user?.email ?? ""}
+ userEmail={user.email}
callback={onSubmit}
buttonText={t("change_password")}
/>
@@ -150,5 +164,3 @@ const Page: React.FC = () => {
</AccountsPageContents>
);
};
-
-export default Page;
diff --git a/web/packages/accounts/services/user.ts b/web/packages/accounts/services/user.ts
index 61afd8f07e..ff68030618 100644
--- a/web/packages/accounts/services/user.ts
+++ b/web/packages/accounts/services/user.ts
@@ -61,14 +61,17 @@ export type LocalUser = z.infer<typeof LocalUser>;
* - When the user enters their email, the email property of the stored object
* is set, but nothing else.
*
- * - If they have second factor verification set, then after entering their
- * password {@link isTwoFactorEnabled} and {@link twoFactorSessionID} will
- * also get filled in.
+ * - After they verify their password, we have two cases: if second factor
+ * verification is not set, and when it is set.
*
- * - Once they verify their TOTP based second factor, their {@link id} and
- * {@link encryptedToken} will also get filled in.
+ * - If second factor verification is not set, then after verifying their
+ * password their {@link id} and {@link encryptedToken} will get filled in,
+ * and {@link isTwoFactorEnabled} will be set to false.
*
- * - TODO add more steps...
+ * - If they have second factor verification set, then after verifying their
+ * password {@link isTwoFactorEnabled} and {@link twoFactorSessionID} will
+ * also get filled in. Once they verify their TOTP based second factor, their
+ * {@link id} and {@link encryptedToken} will also get filled in.
*
* So while the underlying storage is the same, we offer two APIs for code to
* obtain the user: