From 2bcedca7fb51bc5026a49ef807ed654b2ad25f1f 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: Sun, 1 Oct 2023 19:25:40 -0400 Subject: [PATCH] feat(api): add registration endpoint --- server/api/auth/register.post.ts | 48 ++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/server/api/auth/register.post.ts b/server/api/auth/register.post.ts index a4c2bf8..91e9057 100644 --- a/server/api/auth/register.post.ts +++ b/server/api/auth/register.post.ts @@ -1,3 +1,45 @@ -export default eventHandler((event) => { - -}) \ No newline at end of file +import { weirdToNormalChars as w2nc } from "weird-to-normal-chars"; +import crypto from "crypto"; +import { usernameRegex } from "~/lib/server/constants"; +import { User } from "~/models/user"; +import mongoose from "mongoose"; +import captcha from "~/lib/server/middlewareButNotReally/captcha"; + +export default eventHandler(async (event) => { + const body = await readBody(event); + console.log(typeof body); + console.log(body); + if (!body.username || !body.password || !body.email) { + throw createError({ statusCode: 400 }); + } + await captcha(event); + console.log("fields exist"); + const user = await User.findOne({ + $or: [ + { username: usernameRegex(body.username) }, + { email: (body.email as string).toLowerCase() }, + ], + }); + console.log("after f0", user); + if (user) + throw createError({ + statusCode: 400, + message: "A user with that username or email already exists.", + }); + let nuser = new User({ + email: body.email.toLowerCase(), + username: w2nc(body.username.trim()), + password: User.generateHash(body.password), + auth: { + activationKey: crypto.randomBytes(256).toString("hex"), + emailVerified: false, + }, + }); + console.log("nu", nuser); + console.log("nnibg", mongoose.connections); + await nuser.save(); + console.log("savey", await nuser.save()); + return { + success: true, + }; +});