Issue with 'How to use Passport strategies' in Advanced Node and Express

I’m having an issue with this challenge how to use passport strategies, as my test fails on second requirement. It says that I need to redirect to ‘/’ after login request. As far as I can see, I’m redirecting it, but the test still fails.
And here is 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 would appreciate any help…

2 Likes

Try changing the title on your homepage to “Home page”. It should work.

7 Likes

thanks, it worked! :smiley:

1 Like

Cheers, was stuck here for a while… even went to other courses to try and figure out what I was doing wrong.

Same for me, thanks for the “Home Page” - hint!

To be honest, the whole section needs a cleanup, both the code and the descriptions. It is very obvious that no proofreading happened there :smiley:

4 Likes

Hi everybody!
I have some problems passing this challenges. I read everything on the forum and tried it out, everything fails for me. When I almost gave up, I tried to clone someone else’s repo in Glitch to make sure it’s identical and mine doesn’t pass the test, his passes the test. Can someone explain to me what I’m doing wrong?
Here is my clone repository: Glitch doesn’t pass the test
Here is the repo that passes the test: Glitch passes the test

This is the screenshot of the error I get:


Thank you in advance for your help.
Linda

2 Likes

When I click on the link to your clone repository, glitch says: “No project found at lkovacs-advnodeexpress7-fcc, perhaps a typo?”
so I can’t see your code.
However, your link to repo that passes the test helped me a lot. It has: app.set( ‘views’, ‘./views/pug’ ); that my own didn’t. I added this line and passed the challenge. Thank you so much.

1 Like

@danbauguitar Thank you for your thoughts. I observed myself that my glitch project doesn’t work anymore. Now I’m concentrating on building my FreeCodeCamp projects and I will turn back to this part of the Curriculum after I will be done with everything else and I will repass the practice part to refresh my memory. I’m glad that this could help you figure out your project issue and could fix it. I wish you good luck!

2 Likes

what title ? where ?

1 Like

app.route("/").get((req, res) => {
res.render(process.cwd() + “/views/pug/index”, {
title: “Home Page”,
message: “Please login”,
showLogin: true
});
});

2 Likes

Thanks for posting this. I totally missed the requirement for “Home Page.”

1 Like