Archived
1
0
Fork 0
This repository has been archived on 2024-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
SOD/routes/login.js

30 lines
964 B
JavaScript
Raw Normal View History

const express = require("express");
const router = express.Router();
const models = require("../models");
2020-08-20 22:46:46 +02:00
const reCaptcha = require("../middlewares/reCaptcha");
router.get("/", async (req, res) => {
if (req.session.user)
2020-08-18 15:49:58 +02:00
res.redirect("/");
else
2020-08-17 19:13:00 +02:00
res.render("login", { title: "SOD - Login" });
})
2020-08-20 22:46:46 +02:00
.post("/", reCaptcha, async (req, res) => {
if (!req.body.username || !req.body.password)
res.redirect("/login");
else {
let u = await models.User.findByPk(req.body.username);
if (!u || !u.checkPassword(req.body.password))
res.redirect("/login?err=true");
else {
req.session.user = u;
if (req.session.lastUrl && !req.session.lastUrl.startsWith("/login"))
res.redirect(req.session.lastUrl);
else
res.redirect("/");
}
}
});
module.exports = router;