From a63a470a100672ccc8c7ac80905a371fcc781c49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=98=99=E2=97=A6=20The=20Tablet=20=E2=9D=80=20GamerGirla?= =?UTF-8?q?ndCo=20=E2=97=A6=E2=9D=A7?= Date: Wed, 11 Oct 2023 17:00:18 -0400 Subject: [PATCH] feat(api): create endpoints to check if a given username/email already exists in the database --- server/api/exists/email.get.ts | 16 ++++++++++++++++ server/api/exists/username.get.ts | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 server/api/exists/email.get.ts create mode 100644 server/api/exists/username.get.ts diff --git a/server/api/exists/email.get.ts b/server/api/exists/email.get.ts new file mode 100644 index 0000000..e5b768f --- /dev/null +++ b/server/api/exists/email.get.ts @@ -0,0 +1,16 @@ +import { usernameRegex } from "~/lib/server/constants"; +import { User } from "~/models/user"; + +export default eventHandler(async (event) => { + const query = getQuery(event); + const u = await User.findOne({ + email: query.email as string, + }).exec(); + if (u) { + return { + exists: true, + }; + } else { + return { exists: false }; + } +}); diff --git a/server/api/exists/username.get.ts b/server/api/exists/username.get.ts new file mode 100644 index 0000000..c2d6d3b --- /dev/null +++ b/server/api/exists/username.get.ts @@ -0,0 +1,16 @@ +import { usernameRegex } from "~/lib/server/constants"; +import { User } from "~/models/user"; + +export default eventHandler(async (event) => { + const query = getQuery(event); + const u = await User.findOne({ + username: usernameRegex(query.username as string), + }).exec(); + if (u) { + return { + exists: true, + }; + } else { + return { exists: false }; + } +});