2022-09-23 23:10:13 +03:00
|
|
|
const { UserModel } = 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-09-17 19:33:51 +03:00
|
|
|
app.get("/", (req, res) => res.reply("login", { redirect: req.query.redirect, user: null, discord: req.app.get("discord_auth") }));
|
2022-03-21 23:53:22 +03:00
|
|
|
|
2022-04-06 21:14:46 +03:00
|
|
|
app.post("/", async (req, res) => {
|
2022-08-31 14:44:28 +03:00
|
|
|
req.session.userID = null;
|
2022-04-06 21:14:46 +03:00
|
|
|
|
2022-09-23 23:10:13 +03:00
|
|
|
const { name, password } = req.body;
|
2022-03-21 23:53:22 +03:00
|
|
|
|
2022-09-23 23:10:13 +03:00
|
|
|
if (!name || !password) return res.error(400, "You forgot entering some values")
|
2022-08-10 00:22:12 +03:00
|
|
|
|
2023-05-24 15:43:40 +03:00
|
|
|
const member = await UserModel.findOne({
|
|
|
|
$or: [{ name }, { email: name }]
|
|
|
|
}, "+password");
|
|
|
|
if (!member || member.deleted) return res.error(401, 'Incorrect username or email!');
|
2022-09-24 01:39:06 +03:00
|
|
|
if (!await bcrypt.compare(password, member.password)) return res.error(401, 'Incorrect password!');
|
2022-04-06 21:14:46 +03:00
|
|
|
|
2022-09-23 23:10:13 +03:00
|
|
|
req.session.userID = member.id;
|
2022-03-13 16:16:46 +03:00
|
|
|
|
2022-09-04 22:19:19 +03:00
|
|
|
res.redirect(req.query.redirect || '/');
|
2022-03-21 23:53:22 +03:00
|
|
|
|
2022-09-01 14:02:47 +03:00
|
|
|
});
|
2022-03-13 16:16:46 +03:00
|
|
|
|
2022-03-21 23:53:22 +03:00
|
|
|
module.exports = app;
|