akf-forum/routes/login.js

36 lines
999 B
JavaScript
Raw Normal View History

2022-03-21 23:53:22 +03:00
const { User } = require("../classes");
2022-04-06 21:14:46 +03:00
const { Router } = require("express");
const { SecretModel } = require("../models");
const error = require("../errors/error");
2022-03-21 23:53:22 +03:00
const app = Router();
app.get("/", (req, res) => res.render("login"));
2022-04-06 21:14:46 +03:00
app.post("/", async (req, res) => {
2022-03-13 16:16:46 +03:00
req.session.userid = null;
2022-04-06 21:14:46 +03:00
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) {
2022-04-06 21:14:46 +03:00
const user = await SecretModel.findOne({ username });
2022-03-13 16:16:46 +03:00
if (user) {
2022-04-06 21:14:46 +03:00
if (user.password !== password) return error(res, 403, 'Incorrect Password!')
const member = await new User().getByName(username)
if (!member || member.deleted) return error(res, 403, 'Incorrect Username and/or Password!')
2022-03-13 16:16:46 +03:00
req.session.userid = user.id;
res.redirect('/');
} else
error(res, 403, 'Incorrect Username and/or Password!')
2022-03-13 16:16:46 +03:00
} 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
})
2022-03-13 16:16:46 +03:00
2022-03-21 23:53:22 +03:00
module.exports = app;