Advanced Node and Express Challenges - missing stuff

Hi all,
for those working with glitch template, you need to add the following to your server.js

const cors=require('cors');
app.use(cors());

in order for your tests to pass.
And in the introduction of section " Authentication Strategies", the LocalStrategy should be
const LocalStrategy = require('passport-local').Strategy;
instead of
const LocalStrategy = require('passport-local');

Good luck,

8 Likes

Hi,
I have other problem with Strategies challenge - redirecting to the homepge is not accepted for some reason. Test shows an error, despite proper code.

Here’s the code: Glitch.

Hi,
I also have problem with that challenge.
The test fails on the second requirement. But I can see that page is redirected after POST request to /login.

and here’s my code

'use strict';

const express     = require('express');
const bodyParser  = require('body-parser');
const fccTesting  = require('./freeCodeCamp/fcctesting.js');
const passport = require('passport');
const session = require('express-session');
const ObjectID = require('mongodb').ObjectID;
const mongo = require('mongodb').MongoClient;
const LocalStrategy = require('passport-local').Strategy;
const cors = require('cors');

const app = express();

fccTesting(app); //For FCC testing purposes

app.use(cors());
app.use('/public', express.static(process.cwd() + '/public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));


// set up a template engine
app.set('view engine', 'pug');

app.use(session({
  secret: process.env.SESSION_SECRET,
  resave: true,
  saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());

// connect to database
mongo.connect(process.env.DATABASE, (err, db) => {
  if (err) {
    console.log('Database error: ' + err);
  } else {
    console.log('Successful database connection');
    passport.serializeUser((user, done) => {
      done(null, user._id);
    });
    
    passport.deserializeUser((id, done) => {
      db.collection('users').findOne({ _id: new ObjectID(id) }, (err, doc) => {
        done(null, doc);
      });
    });
    
    
    passport.use(new LocalStrategy((username, password, done) => {
      db.collection('users').findOne({ username: username }, (err, user) => {
        console.log('User ' + username + ' attempted to log in.');
        if (err) return done(err);
        if (!user) return done(null, false);
        if (password !== user.password) return done(null, false);
        return done(null, user);
      });
    })); 
    
    app.route('/')
      .get((req, res) => {
      res.render(process.cwd() + '/views/pug/index', { title: 'Hello', message: 'Please login', showLogin: true });
    });    
    
    app.route('/login')
      .post(passport.authenticate('local', { failureRedirect: '/' }), (req, res) => {
        res.redirect('/profile');
      });
    
    app.route('/profile')
      .get((req, res) => {
        res.render(process.cwd() + '/views/pug/profile');
      });
    
    app.listen(process.env.PORT || 3000, () => {
      console.log("Listening on port " + process.env.PORT);
    });
  }
});

i’ve changed title to Home page and it works now… maybe you should try that too…

1 Like

Works… weird :slight_smile:

yeah, maybe it has something to do with how tests are written… who knows… :confused:

Yeah, that’s how I found out. I checked the source code on GitHub to see what exactly the test was checking for to pass, since I had the same problem. I couldn’t understand why I couldn’t pass the test, even though my code worked perfectly. When I checked the test’s implementation, I saw that it used the regex /Home page/gi to find any variation of “Home page” on the page, so I changed my title to “Home page” and it worked. What I don’t like is that nowhere in the lessons did it say to add that phrase to the page to pass. I assume the test wasn’t meant to be set up that way. Oh, well. At least we passed it.

2 Likes

There are more bugs in the testing environment. I have passed some test just putting ‘correct’ code in the comments. For example ive noticed there is a typo in test for socket.io challenge: instead of checking passport.socketio it checks for passportsocketio :slight_smile:

There is somewhere also a typo - i found passportsockerio.

Not sure, but if remember well also it looks for '\/auth\/github' insted of '/auth/github'.

Hope it will be fixed soon :slight_smile:

This test nearly drove me nuts but found out how to pass it. The test in question is
Advanced Node and Express - Authentication with Socket.IO.

Ok so to pass this you in your package.json you need to add the following,

“passport.socketio”: “^3.7.0”,
“passportSocketIo”: “0.0.0”

And then in your server add the following,

// passportSockerIo = require(‘passportSocketIo’
const passportSocketIo = require(“passport.socketio”);

Hopefully those who need the green ticks (like me) will find this useful until its fixed.