Wrong behavior ensureAuthenticated challenge test

Tell us what’s happening:
I need to write a function that check if the user is authenticated before enter in a route, if not redirect to the home page.

I wrote the function, so when I try to enter in the route the redirection works, but when I send my project the second test fail…

Your code so far

// function
const ensureAuthenticated = (req, res, next) => {
    if (!req.isAuthenticated()) {
      return res.redirect("/");
    }
    next();
 };

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0.

Challenge: Create New Middleware with “ensureAuthenticated”

Link to the challenge:
https://www.freecodecamp.org/learn/information-security-and-quality-assurance/advanced-node-and-express/create-new-middleware

1 Like

Update: this solved it for me: Advanced Node and Express - Create New Middleware

Same, going to /profile in the browser redirects to /, as it should.
Tests fail.

function ensureAuthenticated(req, res, next) {
  if (req.isAuthenticated()) {
    return next();
  }
  console.log('???????????') //this gets printed to the console
  res.redirect('/');
};


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