Advanced Node and Express - Implement the Serialization of a Passport User

Tell us what’s happening:

The issue im running into is that mongodb is connected when i check on their website but for some reason its not passing these tests. heres my code

'use strict';
require('dotenv').config();
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');

const app = express();

app.set('view engine', 'pug');
app.set('views', './views/pug');

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 }));

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

  app.route('/').get((req, res) => {
    res.render('index', {
      title: 'Connected to Database',
      message: 'Please log in'
    });
  });

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

}).catch(e => {
  app.route('/').get((req, res) => {
    res.render('index', { title: e, message: 'Unable to connect to database' });
  });
});
  
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Listening on port ${PORT}`);
});

Your code so far

Your browser information:

Challenge Information:

Advanced Node and Express - Implement the Serialization of a Passport User

Hi, @Wrld_runs_on_Code,

You should be getting an assertion error, right? Or is it just another kind of error? Can you please share that error with us when running the test?

While testing, instead of null in the following:

done(null, doc);

Check if err can provide more information?

Your code is passing for me with my own DB. Also, it is exactly the same as the hint code.

What test isn’t passing?

What do you see in the browser console and browser network tab when you submit?

ik it is i took that after it said mine was working i was just frustrated it wasnt working for two hours and then i checked with theirs and it says not working im getting some time of mongodb is depreciated error


(node:377) [MONGODB DRIVER] Warning: useNewUrlParser is a deprecated option: useNewUrlParser has no effect since Node.js Driver version 4.0.0 and will be removed in the next major version
(Use `node --trace-warnings ...` to show where the warning was created)
(node:377) [MONGODB DRIVER] Warning: useUnifiedTopology is a deprecated option: useUnifiedTopology has no effect since Node.js Driver version 4.0.0 and will be removed in the next major version

the test is . Implement the Serialization of a Passport User, I even tried restarting the lesson and it still wouldnt work

Implement the Serialization of a Passport User

(node:377) [MONGODB DRIVER] Warning: useNewUrlParser is a deprecated option: useNewUrlParser has no effect since Node.js Driver version 4.0.0 and will be removed in the next major version
(Use `node --trace-warnings ...` to show where the warning was created)
(node:377) [MONGODB DRIVER] Warning: useUnifiedTopology is a deprecated option: useUnifiedTopology has no effect since Node.js Driver version 4.0.0 and will be removed in the next major version
Listening on port 8080

Deprecation warnings are unrelated to tests failing. They just inform you that in future versions something will change, so you can prepare for it.

The code you posted isn’t the issue. So it’s likely something related to your connection.

One thing to keep in mind is the port number 8080 that is used, are you submitting the localhost URL using the correct port number?

Future versions of Node.js will throw an error.
(Use `node --trace-deprecation ...` to show where the warning was created)
Successfully connected to MongoDB.

Still failed even though connected?

Hi, I found that you are still struggling with this issue. Sorry if we haven’t answered.

This are the two test:

    1. Database connection should be present.
  • Waiting:2. Deserialization should now be correctly using the DB and done(null, null) should be called with the doc.

I guess you are not passing even the first one, right? Can you please confirm that? Or are both tests failing? I believe that your connection is ok, @Wrld_runs_on_Code.

We could concentrate on the deserialization, right?

I don’t know if this is nowadays the case but in older versions it was important to put the call of the app.use to initialize passport and the passport.session after the routers and before the server. Maybe it won’t help but could you try that?

Another thing that I would advise is making tests with Postman? You will likely get more information about the success of the connection with that kind of tools. Have a look at your terminal when making those tests?

There are users having similar issues to yours in the forum. I am trying to find one that could help me to give you a tip. I will come back to you as soon as I find one.

same issue i am facing dont know why this is not passing ? even db connection is properly connected.

"use strict";
require("dotenv").config();
const express = require("express");
const session = require("express-session");
const passport = require("passport");

const { ObjectId } = require("mongodb");

const myDB = require("./connection");
const fccTesting = require("./freeCodeCamp/fcctesting.js");

const app = express();

fccTesting(app); //For FCC testing purposes
app.use("/public", express.static(process.cwd() + "/public"));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

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

passport.initialize();
passport.session();

app.set("view engine", "pug");
app.set("views", "./views/pug");

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

  // Be sure to change the title
  app.route("/").get((req, res) => {
    // Change the response to render the Pug template
    res.render("index", {
      title: "Connected to Database",
      message: "Please login",
    });
  });

  // Serialization and deserialization here...

  // passort serializeUser and deserializeUser method set-up
  passport.serializeUser((user, done) => {
    // first argument any error and second id of the user
    done(null, user._id);
  });

  // deserializeUser method
  passport.deserializeUser((_id, done) => {
    myDataBase.findOne({ _id: new ObjectId(_id) }, (err, doc) => {
      // first argument is the error and second argumnet is the full user object in case of deserializeUser.
      done(null, doc);
    });
  });

  // Be sure to add this...
}).catch((e) => {
  app.route("/").get((req, res) => {
    res.render("index", { title: e, message: "Unable to connect to database" });
  });
});

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

this works for me paste the localhost:3000 url in submit option i think it would work 

@ervishwjeetthakur Please open your own thread and provide a repo or Gitpod workspace snapshot.

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