feat(api): add registration endpoint

This commit is contained in:
parent a59b3ef303
commit 2bcedca7fb
Signed by: tablet
GPG Key ID: 924A5F6AF051E87C

@ -1,3 +1,45 @@
export default eventHandler((event) => {
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,
};
});