Skip to content

armgit / ente

Public Imported from GitHub
0

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

Split errors

Manav Rathi35f710439f34
diff --git a/web/packages/accounts/components/Login.tsx b/web/packages/accounts/components/Login.tsx
index d782b151d7..bfd773c659 100644
--- a/web/packages/accounts/components/Login.tsx
+++ b/web/packages/accounts/components/Login.tsx
@@ -10,7 +10,7 @@ import { t } from "i18next";
import { useRouter } from "next/router";
import { PAGES } from "../constants/pages";
import { getSRPAttributes } from "../services/srp-remote";
-import { sendOTT } from "../services/user";
+import { isSendOTTUserNotRegisteredError, sendOTT } from "../services/user";
interface LoginProps {
signUp: () => void;
@@ -38,7 +38,11 @@ export const Login: React.FC<LoginProps> = ({ signUp, host }) => {
}
} catch (e) {
log.error("Login failed", e);
- setFieldError(t("generic_error_retry"));
+ if (await isSendOTTUserNotRegisteredError(e)) {
+ setFieldError("No account with the given email exists");
+ } else {
+ setFieldError(t("generic_error"));
+ }
}
};
diff --git a/web/packages/accounts/services/user.ts b/web/packages/accounts/services/user.ts
index dddb9f7b34..ee48c4d3cb 100644
--- a/web/packages/accounts/services/user.ts
+++ b/web/packages/accounts/services/user.ts
@@ -2,12 +2,14 @@ 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;
@@ -65,9 +67,13 @@ 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. In
- * cases where the purpose is ambiguous (e.g. we're not sure if it is an
+ * 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}).
+ *
+ * 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`.
*/
export const sendOTT = async (
@@ -82,6 +88,26 @@ 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,