Advanced Node and Express - Logging a User Out Plz Help

What is the problem with my code please help me

'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');
const expressSession = require('cookie-session');
const app = express();

  
  
app.set('view engine', 'pug');
fccTesting(app); //For FCC testing purposes
app.use('/public', express.static(process.cwd() + '/public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
 app.use(session({
  secret: process.env.SESSION_SECRET,
  resave: true,
  saveUninitialized: true,
}));
app.use(passport.initialize());
app.use(passport.session());
mongo.connect(process.env.DATABASE, (err, db) => {
    if(err) {
        console.log('Database error: ' + err);
    } else {
        console.log('Successful database connection');

        //serialization and app.listen

}});
passport.serializeUser((user, done) => {
   done(null, user._id);
 });
passport.deserializeUser((id, done) => {
        mongo.collection('users').findOne(
            {_id: new ObjectID(id)},
            (err, doc) => {
                done(null, doc);
  
            }
       );
    });
// Authentication Strategies
passport.use(new LocalStrategy(
  function(username, password, done) {
    mongo.collection('users').findOne({ username: username }, function (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.pug',{title: 'Hello', message: 'Please login',showLogin: true});
  });

 app.route('/login')
          .post(passport.authenticate('local', { failureRedirect: '/' }),(req,res) => {
               res.redirect('/profile');
          });

function ensureAuthenticated(req,res, next){
if (req.isAuthenticated()){
return next();}
res.redirect('/');
}
  
        app.route('/profile')
          .get(ensureAuthenticated, (req,res) => {
               res.render(process.cwd() + '/views/pug/profile' , {username: req.user.username});
          });

app.route('/logout')
  .get((req, res) => {
      req.logout();
  req.session
      res.redirect('/');
  });
app.use((req, res, next) => {
  res.status(404)
    .type('text')
    .send('Not Found');
});
  
app.listen(process.env.PORT || 3000, () => {
  console.log("Listening on port " + process.env.PORT);
});
 

Currently your logout route looks like this:

app.route('/logout')
  .get((req, res) => {
      req.logout();
  req.session
      res.redirect('/');
  });

You should remove req.session from this as it is not required.

I removed req.session from my logout route but my test did not pass.

Actually, I get errors too on the FCC tests, but my code works fine when I try it manually on Glitch. Try out your code by clicking on ‘Show Live’ near the top of the screen in Glitch. Fill out the forms and submit. If they work as expected, then I would just move onto the next challenge. Any FCC bugs will be fixed in due course.

Look at the following variables passed to res.render in the routes to the .pug views compared to your variables. The test should pass when you pass your code with the same variables.

      app.route('/')
            .get((req, res) => {
              res.render(process.cwd() + '/views/pug/index', {title: 'Home Page', message: 'login', showLogin: true, showRegistration: true});
            });          
app.route('/profile')
          .get(ensureAuthenticated, (req,res) => {
               res.render(process.cwd() + '/views/pug/profile', {username: req.body.username, title: 'Profile Page'});
          });
1 Like