2022-09-23 23:10:13 +03:00
|
|
|
const { UserModel } = require("../models");
|
2022-03-21 23:53:22 +03:00
|
|
|
const { Router } = require("express")
|
2022-08-10 00:22:12 +03:00
|
|
|
const bcrypt = require("bcrypt");
|
2022-09-24 01:39:06 +03:00
|
|
|
const { RL, transporter, emailRegEx } = require('../lib');
|
2022-03-21 23:53:22 +03:00
|
|
|
const app = Router();
|
2022-09-24 01:39:06 +03:00
|
|
|
const { email_auth, forum_name, host } = require("../config.json");
|
|
|
|
app.get("/", (req, res) => res.reply("register", { user: null, discord: req.app.get("discord_auth"), mail: email_auth }));
|
2022-08-28 17:14:05 +03:00
|
|
|
|
2022-09-21 23:06:14 +03:00
|
|
|
app.post("/", RL(24 * 60 * 60_000, 5), async (req, res) => {
|
2022-09-10 21:02:24 +03:00
|
|
|
|
2022-09-17 00:27:38 +03:00
|
|
|
req.session.userID = null;
|
2022-03-13 16:16:46 +03:00
|
|
|
|
2022-09-23 23:10:13 +03:00
|
|
|
let { name, password, about } = req.body;
|
2022-04-06 21:14:46 +03:00
|
|
|
|
2022-09-23 23:10:13 +03:00
|
|
|
if (!name || !password) return res.error(400, "You forgot entering some values");
|
2022-09-21 23:06:14 +03:00
|
|
|
const { names } = req.app.get("limits");
|
2023-05-08 17:16:12 +03:00
|
|
|
if (name.length < 3 || name.length > names) return res.error(400, "Name must be between 3 - 25 characters");
|
|
|
|
if (password.length < 3 || password.length > names) return res.error(400, "Password must be between 3 - 25 characters");
|
2022-09-17 00:27:38 +03:00
|
|
|
|
2022-09-23 23:10:13 +03:00
|
|
|
if (await UserModel.exists({ name })) return res.error(400, `We have got an user named ${name}!`)
|
|
|
|
const user = new UserModel({ name });
|
2022-08-29 19:31:59 +03:00
|
|
|
|
2022-09-17 00:27:38 +03:00
|
|
|
if (about) {
|
|
|
|
if (about.length > 256) return res.error(400, "about must be under 256 characters");
|
2022-09-23 23:10:13 +03:00
|
|
|
user.about = about;
|
2022-09-17 00:27:38 +03:00
|
|
|
}
|
2022-08-29 19:31:59 +03:00
|
|
|
|
2022-09-23 23:10:13 +03:00
|
|
|
await user.takeId()
|
2022-09-24 01:39:06 +03:00
|
|
|
if (user.id === "0")
|
|
|
|
user.admin = true;
|
|
|
|
else if (email_auth) {
|
|
|
|
const email = req.body.email;
|
|
|
|
if (!email || !emailRegEx.test(email)) return res.error(400, "E-mail is not valid");
|
|
|
|
if (await UserModel.exists({ email })) return res.error(400, "E-mail is already in use");
|
|
|
|
user.email = email;
|
|
|
|
user.email_code = await bcrypt.hash(`${Date.now()}-${Math.floor(Math.random() * 1e20)}`, 10)
|
|
|
|
|
|
|
|
transporter.sendMail({
|
|
|
|
from: transporter.options.auth.user,
|
|
|
|
to: email,
|
|
|
|
subject: name + ", please verify your email",
|
|
|
|
html: `
|
|
|
|
<h1>Verify your email in ${forum_name}-forum</h1>
|
|
|
|
<a href="${host}/auth/email?code=${user.email_code}">Click here to verify your email</a>
|
|
|
|
`
|
2023-05-08 17:16:12 +03:00
|
|
|
}, (err) => {
|
2022-09-24 01:39:06 +03:00
|
|
|
if (err) return res.error(500, "Failed to send email");
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
2022-04-06 21:14:46 +03:00
|
|
|
|
2022-09-24 01:39:06 +03:00
|
|
|
user.password = await bcrypt.hash(password, 10);
|
2022-09-23 23:10:13 +03:00
|
|
|
await user.save();
|
2022-03-21 23:53:22 +03:00
|
|
|
|
2022-09-23 23:10:13 +03:00
|
|
|
req.session.userID = user.id;
|
2022-03-21 23:53:22 +03:00
|
|
|
|
2022-09-23 23:10:13 +03:00
|
|
|
res.redirect('/');
|
2022-03-13 16:16:46 +03:00
|
|
|
|
2022-09-23 23:10:13 +03:00
|
|
|
});
|
2022-03-13 16:16:46 +03:00
|
|
|
|
2022-03-21 23:53:22 +03:00
|
|
|
module.exports = app;
|