Skip to content

Commit 3118ef9

Browse files
authored
Merge pull request #216 from codeableorg/fix-check-email-uniqueness
refactor: simplify email validation by using verifyUniqueEmail function
2 parents 7cf07dc + a8c3ab0 commit 3118ef9

File tree

5 files changed

+38
-7
lines changed

5 files changed

+38
-7
lines changed

.react-router/types/+register.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ type Params = {
2828
"/account/profile": {};
2929
"/account/orders": {};
3030
"/not-found": {};
31+
"/verify-email": {};
3132
};

src/routes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default [
2323
route("orders", "./routes/account/orders/index.tsx"),
2424
]),
2525
route("/not-found", "./routes/not-found/index.tsx"),
26+
route("/verify-email", "./routes/verify-email/index.tsx"),
2627
],
2728
},
2829
] satisfies RouteConfig;

src/routes/verify-email/index.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { verifyUniqueEmail } from "@/services/user.service";
2+
3+
import type { Route } from "./+types";
4+
5+
export async function loader({ request }: Route.ActionArgs) {
6+
const url = new URL(request.url);
7+
const email = url.searchParams.get("email");
8+
9+
if (email) {
10+
const response = await verifyUniqueEmail(email);
11+
return response;
12+
}
13+
return false;
14+
}
Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
import { client } from "@/lib/utils";
2-
3-
// Se mantiene para hacer la validación de correo electrónico en el registro del lado del cliente
41
export async function findEmail(email: string): Promise<boolean> {
5-
const body = await client<boolean>("/users/findEmail", {
6-
body: { email },
7-
});
2+
try {
3+
const response = await fetch(
4+
`/verify-email?email=${encodeURIComponent(email)}`
5+
);
6+
7+
if (!response.ok) {
8+
throw new Error(`HTTP error! status: ${response.status}`);
9+
}
810

9-
return body;
11+
const result = await response.json();
12+
return result;
13+
} catch (error) {
14+
console.error("Error verifying email:", error);
15+
return false;
16+
}
1017
}

src/services/user.service.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,11 @@ export async function getOrCreateUser(email: string): Promise<User> {
4848

4949
return existingUser;
5050
}
51+
52+
export async function verifyUniqueEmail(email: string): Promise<boolean> {
53+
const user = await prisma.user.findUnique({
54+
where: { email },
55+
});
56+
57+
return user ? false : true;
58+
}

0 commit comments

Comments
 (0)