Express.js: Using middleware to protect routes
We are usually protecting routes which shouldnt be accessible for not logged users, for example orders, cart, …
We can use our own middleware for this.
Prerequisities
We have some login page, we have default authenticate system, user will enter email and password, app will look into db, if there is that user with email and password, it will set req.session.isLoggedIn = true;
. We are using sessions which are stored in db and we are using mongoose
.
middleware/is-auth.js
If somebody wants to visit protected route, we redirect him to login page.