2022-08-09 19:16:34 +03:00
|
|
|
const { UserModel, SecretModel } = require("../models");
|
2022-04-06 21:14:46 +03:00
|
|
|
const { Router } = require("express");
|
2022-03-21 23:53:22 +03:00
|
|
|
const app = Router();
|
2022-08-10 00:22:12 +03:00
|
|
|
const bcrypt = require("bcrypt");
|
2022-03-21 23:53:22 +03:00
|
|
|
|
2022-08-11 00:38:44 +03:00
|
|
|
app.get("/", (req, res) => res.render("login",{redirect: req.query.redirect}));
|
2022-03-21 23:53:22 +03:00
|
|
|
|
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-08-10 00:22:12 +03:00
|
|
|
|
|
|
|
const validPassword = await bcrypt.compare(password, user.password);
|
|
|
|
|
2022-08-11 00:49:44 +03:00
|
|
|
if (!validPassword) return res.error( 403, 'Incorrect Password!')
|
2022-08-09 19:16:34 +03:00
|
|
|
const member = await UserModel.findOne({ name: username });
|
2022-08-11 00:49:44 +03:00
|
|
|
if (!member || member.deleted) return res.error( 403, 'Incorrect Username and/or Password!')
|
2022-04-06 21:14:46 +03:00
|
|
|
|
2022-03-13 16:16:46 +03:00
|
|
|
req.session.userid = user.id;
|
|
|
|
|
2022-08-11 00:38:44 +03:00
|
|
|
res.redirect( req.query.redirect || '/');
|
2022-03-13 16:16:46 +03:00
|
|
|
} else
|
2022-08-11 00:49:44 +03:00
|
|
|
res.error( 403, 'Incorrect Username and/or Password!')
|
2022-03-13 16:16:46 +03:00
|
|
|
|
|
|
|
|
|
|
|
} else
|
2022-08-11 00:49:44 +03:00
|
|
|
res.error( 400, "You forgot entering some values")
|
2022-03-21 23:53:22 +03:00
|
|
|
|
|
|
|
|
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;
|