akf-forum/routes/register.js

41 lines
1 KiB
JavaScript
Raw Normal View History

2022-03-21 23:53:22 +03:00
const { User } = require("../classes");
const db = require("quick.db")
2022-03-13 16:16:46 +03:00
2022-03-21 23:53:22 +03:00
const { Router } = require("express")
const error = require("../errors/error")
const app = Router();
app.get("/", (req, res) => res.render("register"));
app.post("/", (req, res) => {
2022-03-13 16:16:46 +03:00
req.session.loggedin = false;
req.session.username = null;
req.session.userid = null;
2022-03-21 23:53:22 +03:00
const { username = null, password = null } = req.body;
2022-03-13 16:16:46 +03:00
if (username && password) {
const user = db.get("secret." + username)
2022-03-21 23:53:22 +03:00
if (user)
2022-03-13 16:16:46 +03:00
error(res, 404, `We have got an user named ${username}!`)
2022-03-21 23:53:22 +03:00
else {
const user2 = new User(req.body.username, req.body.avatar).takeId()
2022-03-13 16:16:46 +03:00
db.set("secret." + username, { id: user2.id, key: password })
req.session.loggedin = true;
req.session.username = username;
req.session.userid = user2.id;
user2.write()
res.redirect('/');
}
} else
2022-03-21 23:53:22 +03:00
error(res, 400, "You forgot entering some values")
})
2022-03-13 16:16:46 +03:00
2022-03-21 23:53:22 +03:00
module.exports = app;