2022-08-09 19:16:34 +03:00
|
|
|
const { UserModel, SecretModel } = 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-21 23:06:14 +03:00
|
|
|
const { RL } = require('../lib');
|
2022-03-21 23:53:22 +03:00
|
|
|
const app = Router();
|
|
|
|
|
2022-09-17 19:33:51 +03:00
|
|
|
app.get("/", (req, res) => res.reply("register", { user: null, discord: req.app.get("discord_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-17 15:19:41 +03:00
|
|
|
let { username, password: body_pass, about } = req.body;
|
2022-04-06 21:14:46 +03:00
|
|
|
|
2022-09-17 00:27:38 +03:00
|
|
|
if (!username || !body_pass) return res.error(400, "You forgot entering some values");
|
2022-09-21 23:06:14 +03:00
|
|
|
const { names } = req.app.get("limits");
|
2022-09-17 16:27:01 +03:00
|
|
|
if (username.length < 3 || names > 25) return res.error(400, "Username must be between 3 - 25 characters");
|
|
|
|
if (body_pass.length < 3 || names > 25) return res.error(400, "Password must be between 3 - 25 characters");
|
2022-09-17 00:27:38 +03:00
|
|
|
|
2022-08-27 10:31:16 +03:00
|
|
|
const user = await SecretModel.findOne({ username });
|
2022-03-13 16:16:46 +03:00
|
|
|
|
2022-09-17 00:27:38 +03:00
|
|
|
if (user) return res.error(400, `We have got an user named ${username}!`)
|
2022-03-13 16:16:46 +03:00
|
|
|
|
2022-09-17 00:27:38 +03:00
|
|
|
const user2 = new UserModel({ name: username })
|
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");
|
|
|
|
user2.about = about;
|
|
|
|
}
|
2022-08-29 19:31:59 +03:00
|
|
|
|
2022-08-27 10:31:16 +03:00
|
|
|
await user2.takeId()
|
|
|
|
await user2.save();
|
2022-04-06 21:14:46 +03:00
|
|
|
|
2022-08-27 10:31:16 +03:00
|
|
|
const salt = await bcrypt.genSalt(10);
|
|
|
|
const password = await bcrypt.hash(body_pass, salt);
|
|
|
|
await SecretModel.create({ username, password, id: user2.id })
|
2022-08-31 14:44:28 +03:00
|
|
|
req.session.userID = user2.id;
|
2022-08-10 00:22:12 +03:00
|
|
|
|
2022-08-27 10:31:16 +03:00
|
|
|
res.redirect('/');
|
2022-04-06 21:14:46 +03:00
|
|
|
|
2022-03-21 23:53:22 +03:00
|
|
|
|
|
|
|
|
|
|
|
})
|
2022-03-13 16:16:46 +03:00
|
|
|
|
|
|
|
|
2022-03-21 23:53:22 +03:00
|
|
|
module.exports = app;
|