After login redirect to request page

This commit is contained in:
Ethanell 2020-04-28 16:51:43 +02:00
parent 4fb53bb741
commit 126e0e99c7
3 changed files with 13 additions and 4 deletions

View file

@ -8,7 +8,11 @@ router.get("/", (req, res) => {
if ("fail" in req.query) {
fail = true;
}
res.render("login", {title: "login", fail: fail});
let path = null;
if ("path" in req.query) {
path = req.query.path;
}
res.render("login", {title: "login", fail: fail, path: path});
});
router.post("/", (req, res) => {
@ -17,7 +21,11 @@ router.post("/", (req, res) => {
if (req.body.username in file && passwordHash.verify(req.body.password, file[req.body.username])) {
req.session.login = true;
req.session.save();
if ("path" in req.query) {
res.redirect(req.query.path);
} else {
res.redirect("/");
}
} else {
res.redirect("/login?fail");
}

View file

@ -19,7 +19,7 @@ function isAuth(req, res, next) {
if (req.session.login) {
next();
} else {
res.redirect("/login");
res.redirect("/login?path=" + req.originalUrl);
}
}

View file

@ -1,9 +1,10 @@
extend layout
block content
- path = path ? "/login?path=" + path : "/login"
h1 Login
if fail
h2.fail Invalid loggin !
form(method="POST", action="/login")
form(method="POST", action=`${path}`)
div
label(for="username") Username:
input#username(type="text", name="username")