Skip to content

armgit / ente

Public Imported from GitHub
0

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

Combine

Manav Rathi841de87a3380
diff --git a/web/packages/accounts/components/Verify2FACodeForm.tsx b/web/packages/accounts/components/Verify2FACodeForm.tsx
index 75afbc8bd2..3084fa4d76 100644
--- a/web/packages/accounts/components/Verify2FACodeForm.tsx
+++ b/web/packages/accounts/components/Verify2FACodeForm.tsx
@@ -14,19 +14,11 @@ interface Verify2FACodeFormProps {
* 1. The fill-in all the required 6 digits, or
* 2. They press the "Enable" button.
*
- * The form will stay in a waiting state until this callback returns.
+ * The form will stay in a submitting state until this callback returns.
*
* @param otp The OTP that the user entered.
*/
onSubmit: (otp: string) => Promise<void>;
- /**
- * Called when the process was completed successfully.
- *
- * This will be called immediately after onSubmit fulfills, but having two
- * separate callbacks allows the form to indicate the success state in the
- * interim.
- */
- onSuccess: () => void;
/**
* The label for the submit button.
*/
@@ -39,10 +31,8 @@ interface Verify2FACodeFormProps {
*/
export const Verify2FACodeForm: React.FC<Verify2FACodeFormProps> = ({
onSubmit,
- onSuccess,
submitButtonText,
}) => {
- const [waiting, setWaiting] = useState(false);
const [shouldAutoFocus, setShouldAutoFocus] = useState(true);
const formik = useFormik<{ otp: string }>({
@@ -51,25 +41,28 @@ export const Verify2FACodeForm: React.FC<Verify2FACodeFormProps> = ({
validateOnChange: false,
onSubmit: async ({ otp }, { setFieldError, resetForm }) => {
try {
- setWaiting(true);
await onSubmit(otp);
- setWaiting(false);
- onSuccess();
} catch (e) {
log.error("Failed to submit 2FA code", e);
resetForm();
setFieldError("otp", t("generic_error"));
- // Workaround (toggling shouldAutoFocus) to reset the focus back to
- // the first input field in case of errors.
+ // Workaround (toggling shouldAutoFocus) to reset the focus back
+ // to the first input field in case of errors.
// https://github.com/devfolioco/react-otp-input/issues/420
setShouldAutoFocus(false);
setTimeout(() => setShouldAutoFocus(true), 100);
}
- setWaiting(false);
},
});
- const { values, errors, handleChange, handleSubmit, submitForm } = formik;
+ const {
+ values,
+ errors,
+ handleChange,
+ handleSubmit,
+ submitForm,
+ isSubmitting,
+ } = formik;
const onChange =
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
@@ -104,7 +97,7 @@ export const Verify2FACodeForm: React.FC<Verify2FACodeFormProps> = ({
type="submit"
color="accent"
fullWidth
- loading={waiting}
+ loading={isSubmitting}
disabled={values.otp.length < 6}
>
{submitButtonText}
diff --git a/web/packages/accounts/pages/two-factor/setup.tsx b/web/packages/accounts/pages/two-factor/setup.tsx
index 20435c6f91..27caee28bd 100644
--- a/web/packages/accounts/pages/two-factor/setup.tsx
+++ b/web/packages/accounts/pages/two-factor/setup.tsx
@@ -12,7 +12,7 @@ import { getData, LS_KEYS, setLSUser } from "@ente/shared/storage/localStorage";
import { Paper, Stack, styled, Typography } from "@mui/material";
import { t } from "i18next";
import { useRouter } from "next/router";
-import React, { useCallback, useEffect, useState } from "react";
+import React, { useEffect, useState } from "react";
const Page: React.FC = () => {
const [twoFactorSecret, setTwoFactorSecret] = useState<
@@ -22,11 +22,10 @@ const Page: React.FC = () => {
const router = useRouter();
useEffect(() => {
- if (twoFactorSecret) return; // HMR
void setupTwoFactor().then(setTwoFactorSecret);
}, []);
- const handleSubmit = useCallback(async (otp: string) => {
+ const handleSubmit = async (otp: string) => {
const {
encryptedData: encryptedTwoFactorSecret,
nonce: twoFactorSecretDecryptionNonce,
@@ -40,11 +39,8 @@ const Page: React.FC = () => {
...getData(LS_KEYS.USER),
isTwoFactorEnabled: true,
});
- }, []);
-
- const handleSuccess = useCallback(() => {
void router.push(appHomeRoute);
- }, [router]);
+ };
return (
<Stack sx={{ minHeight: "100svh" }}>
@@ -56,7 +52,6 @@ const Page: React.FC = () => {
<Instructions twoFactorSecret={twoFactorSecret} />
<Verify2FACodeForm
onSubmit={handleSubmit}
- onSuccess={handleSuccess}
submitButtonText={t("enable")}
/>
<Stack sx={{ alignItems: "center" }}>
diff --git a/web/packages/accounts/pages/two-factor/verify.tsx b/web/packages/accounts/pages/two-factor/verify.tsx
index e9174d7cd6..bb93262509 100644
--- a/web/packages/accounts/pages/two-factor/verify.tsx
+++ b/web/packages/accounts/pages/two-factor/verify.tsx
@@ -14,7 +14,7 @@ import type { User } from "@ente/shared/user/types";
import { HttpStatusCode } from "axios";
import { t } from "i18next";
import { useRouter } from "next/router";
-import { useCallback, useEffect, useState } from "react";
+import { useEffect, useState } from "react";
import {
AccountsPageContents,
AccountsPageFooter,
@@ -43,43 +43,36 @@ const Page: React.FC = () => {
}
}, []);
- const handleSubmit = useCallback(
- async (otp: string) => {
- try {
- const resp = await verifyTwoFactor(otp, sessionID);
- const { keyAttributes, encryptedToken, token, id } = resp;
- await setLSUser({
- ...getData(LS_KEYS.USER),
- token,
- encryptedToken,
- id,
- });
- setData(LS_KEYS.KEY_ATTRIBUTES, keyAttributes!);
- } catch (e) {
- if (
- e instanceof ApiError &&
- // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
- e.httpStatusCode === HttpStatusCode.NotFound
- ) {
- logout();
- } else {
- throw e;
- }
+ const handleSubmit = async (otp: string) => {
+ try {
+ const resp = await verifyTwoFactor(otp, sessionID);
+ const { keyAttributes, encryptedToken, token, id } = resp;
+ await setLSUser({
+ ...getData(LS_KEYS.USER),
+ token,
+ encryptedToken,
+ id,
+ });
+ setData(LS_KEYS.KEY_ATTRIBUTES, keyAttributes!);
+ void router.push(unstashRedirect() ?? PAGES.CREDENTIALS);
+ } catch (e) {
+ if (
+ e instanceof ApiError &&
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
+ e.httpStatusCode === HttpStatusCode.NotFound
+ ) {
+ logout();
+ } else {
+ throw e;
}
- },
- [logout],
- );
-
- const handleSuccess = useCallback(() => {
- void router.push(unstashRedirect() ?? PAGES.CREDENTIALS);
- }, [router]);
+ }
+ };
return (
<AccountsPageContents>
<AccountsPageTitle>{t("two_factor")}</AccountsPageTitle>
<Verify2FACodeForm
onSubmit={handleSubmit}
- onSuccess={handleSuccess}
submitButtonText={t("verify")}
/>
<AccountsPageFooter>