Hey there.
I don´t understand how this code works. So, this function is created:
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
res.redirect('/');
};
And then is used as parameter in here:
app
.route('/profile')
.get(ensureAuthenticated, (req,res) => {
res.render(process.cwd() + '/views/pug/profile');
});
When “ensureAuthenticated” is used in “app.get”, is the “next()” inside it calling res.render() when a user is authenticated?
What confuses me is that if the function does not have a route, what happens when we use “next()”? What can be the next matching route if there isn´t one?
Also, when this function is inside the “get”, is not called (I mean, I’m not doing “ensureAuthenticated()” . Is it checking if a user is authenticated?
Sorry if my explanation may not be clear, as I am confused myself…
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36
Challenge: Create New Middleware
Link to the challenge: