Skip to content

armgit / ente

Public Imported from GitHub
0

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

Generic

Manav Rathia91027c33584
diff --git a/web/packages/accounts/components/Login.tsx b/web/packages/accounts/components/Login.tsx
index a5062a309a..66989d84c3 100644
--- a/web/packages/accounts/components/Login.tsx
+++ b/web/packages/accounts/components/Login.tsx
@@ -10,7 +10,8 @@ import { t } from "i18next";
import { useRouter } from "next/router";
import { PAGES } from "../constants/pages";
import { getSRPAttributes } from "../services/srp-remote";
-import { isSendOTTUserNotRegisteredError, sendOTT } from "../services/user";
+import { sendOTT } from "../services/user";
+import { isMuseumHTTPError } from "@/base/http";
interface LoginProps {
signUp: () => void;
@@ -32,7 +33,9 @@ export const Login: React.FC<LoginProps> = ({ signUp, host }) => {
try {
await sendOTT(email, "login");
} catch (e) {
- if (await isSendOTTUserNotRegisteredError(e)) {
+ if (
+ await isMuseumHTTPError(e, 404, "USER_NOT_REGISTERED")
+ ) {
setFieldError("Email not registered");
return;
}
diff --git a/web/packages/accounts/services/user.ts b/web/packages/accounts/services/user.ts
index ee48c4d3cb..a6f4c2672f 100644
--- a/web/packages/accounts/services/user.ts
+++ b/web/packages/accounts/services/user.ts
@@ -2,14 +2,12 @@ import type { B64EncryptionResult } from "@/base/crypto/libsodium";
import {
authenticatedRequestHeaders,
ensureOk,
- HTTPError,
publicRequestHeaders,
} from "@/base/http";
import { apiURL } from "@/base/origins";
import HTTPService from "@ente/shared/network/HTTPService";
import { getToken } from "@ente/shared/storage/localStorage/helpers";
import type { KeyAttributes } from "@ente/shared/user/types";
-import { z } from "zod";
export interface UserVerificationResponse {
id: number;
@@ -67,11 +65,8 @@ export interface RecoveryKey {
* @param email The email to verify.
*
* @param purpose In which context is the email being verified. Remote applies
- * additional business rules depending on this.
- *
- * For example, passing the purpose "login" ensures that the OTT is only sent to
- * an already registered email, otherwise an error is thrown (which can be
- * checked for by using {@link isSendOTTUserNotRegisteredError}).
+ * additional business rules depending on this. For example, passing the purpose
+ * "login" ensures that the OTT is only sent to an already registered email.
*
* In cases where the purpose is ambiguous (e.g. we're not sure if it is an
* existing login or a new signup), the purpose can be set to `undefined`.
@@ -88,26 +83,6 @@ export const sendOTT = async (
}),
);
-/**
- * Return `true` if this is an error returned by remote when we try to call
- * {@link sendOTT} with purpose "login" for an email which does not have an
- * existing account corresponding to it.
- */
-export const isSendOTTUserNotRegisteredError = async (e: unknown) => {
- if (e instanceof HTTPError && e.res.status == 404) {
- try {
- // {"code":"USER_NOT_REGISTERED","message":"User is not registered"}
- const code = z
- .object({ code: z.string() })
- .parse(await e.res.json()).code;
- return code == "USER_NOT_REGISTERED";
- } catch {
- return false;
- }
- }
- return false;
-};
-
export const verifyOtt = async (
email: string,
ott: string,
diff --git a/web/packages/base/http.ts b/web/packages/base/http.ts
index f1f11d56fb..081ecaf5c1 100644
--- a/web/packages/base/http.ts
+++ b/web/packages/base/http.ts
@@ -1,6 +1,8 @@
import { retryAsyncOperation } from "@/utils/promise";
+import { z } from "zod";
import { clientPackageName } from "./app";
import { ensureAuthToken } from "./local-user";
+import log from "./log";
/**
* Return headers that should be passed alongwith (almost) all authenticated
@@ -120,6 +122,36 @@ export const isHTTP4xxError = (e: unknown) =>
export const isHTTP401Error = (e: unknown) =>
e instanceof HTTPError && e.res.status == 401;
+/**
+ * Return `true` if this is an error because of a HTTP failure response returned
+ * by museum with the given "code" and HTTP status.
+ *
+ * For some known set of errors, museum returns a payload of the form
+ *
+ * {"code":"USER_NOT_REGISTERED","message":"User is not registered"}
+ *
+ * where the code can be used to match a specific reason for the HTTP request
+ * failing. This function can be used as a predicate to check both the HTTP
+ * status code and the "code" within the payload.
+ */
+export const isMuseumHTTPError = async (
+ e: unknown,
+ httpStatus: number,
+ code: string,
+) => {
+ if (e instanceof HTTPError && e.res.status == httpStatus) {
+ try {
+ const payload = z
+ .object({ code: z.string() })
+ .parse(await e.res.json());
+ return payload.code == code;
+ } catch (e) {
+ log.warn("Ignoring error when parsing error payload", e);
+ return false;
+ }
+ }
+ return false;
+};
/**
* A helper function to adapt {@link retryAsyncOperation} for HTTP fetches.
*