I am using Node (latest version) + Express as well as the latest version. I have 2 folders, public and protected. The protected folder should be accessible only after logging in.
I created the login system myself, now I wonder how I can protect the route to this "protected folder".
I tried to install a static route in my "secure" folder (for example, I did it with a public folder), and then check if the user is logged in, but it does not work.
This is what I thought should work ...
(...)
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'secured')));
(...)
function requireLogin(req, res, next) {
if (req.session.loggedIn) {
next();
} else {
res.redirect("/login");
}
}
app.all("/secured/*", requireLogin, function(req, res, next) {
next();
});
source
share