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
2020-08-21 11:11:36 +02:00

29 lines
964 B
JavaScript

const express = require("express");
const router = express.Router();
const models = require("../models");
const reCaptcha = require("../middlewares/reCaptcha");
router.get("/", async (req, res) => {
if (req.session.user)
res.redirect("/");
else
res.render("login", { title: "SOD - Login" });
})
.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;