Advanced Node and Express - Create New Middleware Solution

What is your hint or solution suggestion?

function ensureAuthenticated(req, res, next) {
  if (req.isAuthenticated()) {
    return next();
  }
  res.redirect('/');
};

!!!MAKE SURE, THAT YOUR CODE HAS title: ‘Home page’ IN res.render!!!

app.route("/").get((req, res) => {
  //Change the response to render the Pug template
  res.render(process.cwd() + '/views/pug/index', {title: 'Home page', message: 'Please login', showLogin: true});
});

app.route("/login").post(passport.authenticate('local', { failureRedirect: '/' }), (req, res) => {
  res.redirect("/profile");
});

app.route('/profile').get(ensureAuthenticated, (req,res) => {
    res.render(process.cwd() + '/views/pug/profile');
 });

Challenge: Create New Middleware

Link to the challenge:

Thank you, for your contribution. I will update the guide appropriately.

Just to let you know, we are in the process of fixing these lessons: https://github.com/freeCodeCamp/freeCodeCamp/pull/39080

But this hint will help the current users.

1 Like