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) { if ("fail" in req.query) {
fail = true; 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) => { 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])) { if (req.body.username in file && passwordHash.verify(req.body.password, file[req.body.username])) {
req.session.login = true; req.session.login = true;
req.session.save(); req.session.save();
if ("path" in req.query) {
res.redirect(req.query.path);
} else {
res.redirect("/"); res.redirect("/");
}
} else { } else {
res.redirect("/login?fail"); res.redirect("/login?fail");
} }

View file

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

View file

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