fix: enhance email validation using zod

This commit is contained in:
Piyush Gupta
2025-10-09 02:24:24 +05:30
parent 555468eec0
commit ca57b4abe4
@@ -1,9 +1,8 @@
import { useState } from "react"; import { useState } from "react";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { Link } from "@tanstack/react-router"; import { Link } from "@tanstack/react-router";
import axios from "axios"; import { z } from "zod";
import { createNotification } from "@app/components/notifications";
import { useSendVerificationEmail } from "@app/hooks/api"; import { useSendVerificationEmail } from "@app/hooks/api";
import { Button, Input } from "../v2"; import { Button, Input } from "../v2";
@@ -35,11 +34,10 @@ export default function EnterEmailStep({
* Verifies if the entered email "looks" correct * Verifies if the entered email "looks" correct
*/ */
const emailCheck = async () => { const emailCheck = async () => {
const isValid = z.string().email().safeParse(email);
let emailCheckBool = false; let emailCheckBool = false;
if (!email) { if (!isValid.success) {
setEmailError(true);
emailCheckBool = true;
} else if (!email.includes("@") || !email.includes(".") || !/[a-z]/.test(email)) {
setEmailError(true); setEmailError(true);
emailCheckBool = true; emailCheckBool = true;
} else { } else {
@@ -48,24 +46,9 @@ export default function EnterEmailStep({
// If everything is correct, go to the next step // If everything is correct, go to the next step
if (!emailCheckBool) { if (!emailCheckBool) {
try { await mutateAsync({ email: email.toLowerCase() });
await mutateAsync({ email: email.toLowerCase() }); setEmail(email.toLowerCase());
setEmail(email.toLowerCase()); incrementStep();
incrementStep();
} catch (e) {
if (axios.isAxiosError(e)) {
let { message } = e.response?.data as { message: string };
if (typeof message !== "string") {
message = "Something went wrong";
}
createNotification({
type: "error",
text: message
});
}
}
} }
}; };