You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.4 KiB
JavaScript

1 year ago
// Strict Mode
"use strict";
// Imports
import cryptography from "../../cryptography/cryptography.js";
import database from "../../database/database.js";
import mail from "../../mail/mail.js";
// Handler
export default async function handler ( req, res ) {
try {
const host = "http://localhost:3000/";
const response = {
success: false,
error: ""
};
req.body.Username = req.body.Username.toUpperCase();
if (req.body.Username.length < 1) {
response.error = "Enter A Username";
res.send({
response
});
} else if (req.body.Username.length > 10) {
response.error = "Username Must Be 10 Characters Or Less";
res.send({
response
});
} else if (req.body.Username.match(/^\w+$/) == null) {
response.error = "Username Must Only Contain Alphanumberic Characters";
res.send({
response
});
} else if (await database.usernameExists(req.body.Username)) {
response.error = "Username Already In Use";
res.send({
response
});
} else if (await mail.validateEmail(req.body.Email) == false) {
response.error = "Email Is Not Valid";
res.send({
response
});
} else if (await database.emailExists(req.body.Email)) {
response.error = "Email Already In Use";
res.send({
response
});
} else if (req.body.Password.length < 4) {
response.error = "Password Must Be At Least 4 Characters Long";
res.send({
response
});
} else if (await database.createUser(req.body.Username, req.body.Email, await cryptography.hash(req.body.Password), req.body.Username)) {
response.success = true;
const t = await mail.sendAccountActivationEmail(req.body.Email, host + "activate/" + req.body.Username);
console.log(t)
await database.createStat(req.body.Username);
res.send({
response
});
} else {
response.error = "Error Creating User";
res.send({
response
});
};
} catch {
response.success = false;
response.error = "Error";
res.send({
response
});
};
};