Tests Failing for "Registration of New Users" Quality Assurance Challenge

Tell us what’s happening:
Despite being able to complete each requirement manually, and watch the process work as intended through console logs, I am unable to pass the test suite for this challenge, with the following errors:

In the console log for the FCC page, I also have this recurring CORS error:

image

Adding const cors = require('cors'); and app.use(cors()); to both fcctesting.js and server.js doesn’t resolve this error either.

Your project link(s)

solution: https://boilerplate-advancednode.phr0nesis.repl.co

source: (server.js)

'use strict';
require('dotenv').config({ path: 'sample.env' });
const express = require('express');
const myDB = require('./connection');
const fccTesting = require('./freeCodeCamp/fcctesting.js');
const session = require('express-session');
const passport = require('passport');
const ObjectID = require('mongodb').ObjectID;
const LocalStrategy = require('passport-local');
const cors = require('cors');

const app = express();

app.use(session({
  secret: process.env.SESSION_SECRET,
  resave: true,
  saveUninitialized: true,
  cookie: { secure: false }
}));

app.use(passport.initialize());
app.use(passport.session());

fccTesting(app); //For FCC testing purposes
app.use('/public', express.static(process.cwd() + '/public'));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors());
app.set('view engine', 'pug');

myDB(async (client) => {
  const myDataBase = await client.db('database').collection('users');

  const PORT = process.env.PORT;
  app.listen(PORT, () => {
    console.log('Listening on port ' + PORT);
  });

  // MAIN
  app.route('/').get((req, res) => {
    res.render('pug', { title: 'Connected to database', message: 'Please login', showLogin: true, showRegistration: true });
  });

  // LOGIN
  // Handle login by authenticating user, redirecting to their profile on success, to / on fail
  app.route('/login').post(passport.authenticate('local', { failureRedirect: '/' }), (req, res) => {
    res.redirect('/profile');
  });

  // Assuming the user is authenticated, render the profile page.
  app.route('/profile').get(ensureAuthenticated, (req, res) => {
    res.render(process.cwd() + '/views/pug/profile', { username: req.user.username });
  });

  // LOGOUT
  // Handle logout by unathenticating the user, and then redirecting them to '/'
  app.route('/logout').get((req, res) => {
    req.logout();
    res.redirect('/');
  });

  app.route('/register').post(
    (req, res, next) => {
      myDataBase.findOne({ username: req.body.username }, (err, user) => {
        if (err) {
          next(err);
        } else if (user) {
          console.log('User already exists in database');
          res.redirect('/');
        } else {
          myDataBase.insertOne({ username: req.body.username, password: req.body.password },
            (err, doc) => {
              if (err) {
                console.log(err);
                res.redirect('/');
              } else {
                console.log('User created, authenticating...');
                console.log(doc.ops[0]);
                next(null, doc.ops[0]);
              }
            });
        }
      });
    },
    passport.authenticate('local', { failureRedirect: '/' }), (req, res, next) => {
      res.redirect('/profile');
    }
  );

  // Handle missing pages with 404
  app.use((req, res, next) => {
    res.status(404).type('text').send('Not Found');
  });

  passport.serializeUser((user, done) => {
    done(null, user._id);
  });
  passport.deserializeUser((id, done) => {
    myDataBase.findOne({ _id: new ObjectID(id) }, (err, doc) => {
      done(null, doc);
    });
  });

  // Configuration of local authentication strategy for passport
  passport.use(new LocalStrategy(
    (username, password, done) => {
      myDataBase.findOne({ username: username }, (err, user) => {
        console.log('User ' + username + ' attempted to log in.');
        if (err) {
          console.log('Error finding user.');
          return done(err);
        }
        if (!user) {
          console.log('No user found.');
          return done(null, false);
        }
        if (password !== user.password) {
          console.log('User has incorrect password.');
          return done(null, false);
        }
        return done(null, user);
      });
    })
  );

}).catch(e => {
  app.route('/').get((req, res) => {
    res.render('pug', { title: e, message: 'Unable to login' });
  });
});

// Check to see if the user is authenticated
const ensureAuthenticated = (req, res, next) => {
  if (req.isAuthenticated()) {
    return next();
  }
  res.redirect('/');
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36

Challenge: Registration of New Users

Link to the challenge:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.