mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-07 21:27:31 +00:00
fix: enhance email validation using zod
This commit is contained in:
@@ -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
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user