Advanced Node and Express - Registration of New Users help

i know my code is a little different from the one mentioned in the course…
but this code seems to work for me just fine
i did it like so because i cant make out anything of whats given in the course

WHATS GIVEN IN THE COURSE -

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

WHAT I DID

	app.route('/register')
		.post(async (req, res) => {
			try {
				const user = await users.findOne({ username: req.body.username });
				if (user) {
					return res.redirect('/');
				}
				const newuser = await users.insertOne({
					username: req.body.username,
					password: req.body.password
				});
				req.login(newuser.ops[0], (e) => {
					if (e) {
						throw (e);
					}
					res.redirect('/profile');
				});
			} catch (e) {
				res.redirect('/');
			}
		});

can some one please help me out?

EDIT: i have also hosted it - https://cryptic-basin-20507.herokuapp.com/
github - https://github.com/shameekagarwal/fccadvancednode

Hello there,

Your live app appears to be down. So, I suggest you head over to the logs to see why.

Otherwise, something you can try:

  1. Navigate to the freecodecamp/fcctesting.js file
  2. Scroll to the first app.use statement
  3. Replace it with this:
app.use(function (req, res, next) {
      const origin = req.get('origin');
      if (allowedOriginsMatcher.test(origin)) {
            res.setHeader('Access-Control-Allow-Origin', 'https://www.freecodecamp.org');
      }
      res.setHeader('Access-Control-Allow-Credentials', true);
      next();
  });

Something else: Add this option to your session middleware

cookie: { secure: false }