Advanced Node and Express - Registration of New Users - Need solution

I’m working on this freecodecamp challenge. I’m trying to register a user and login the same user but couldn’t do it successfully.

I’m getting below errors.

Login should work if previous test was done successfully and redirect successfully to the profile. Check your work and clear your DB

I should be able to register and it direct me to my profile. CLEAR YOUR DATABASE if this test fails (each time until its right!)

app.route('/register').post((req, res, next) => {
      db.collection('users').findOne({ username: req.body.username }, function (err, user) {
          if(err) {
            console.log('error')
              next(err);
          } else if (user) {
            console.log('redirect to home-page')
              res.redirect('/');
          } else {
              db.collection('users').insertOne({username: req.body.username, password: req.body.password}, (err, doc) => {
                    if(err) {
                      console.log('error after recored')
                        res.redirect('/');
                    } else {
                      console.log('error with user', doc);
                        next(null, user);
                    }
                }
              )
          }
      })},
    passport.authenticate('local', { failureRedirect: '/' }), (req, res, next) => {
        res.redirect('/profile');
    });

Here is the full code for your review.

1 Like

I have solved this issue. Complete code here.