Implement the Serialization of a Passport User (Database connection should be present (Cannot find what i did Wrong))

Tell us what’s happening:

Your project link(s)

solution: https://triangular-dent-rugby.glitch.me

'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').ObjectID;

const app = express();
app.set('view engine', 'pug');

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(session({
  secret: process.env.SESSION_SECRET,
  resave: true,
  saveUninitialized: true,
  cookie: { secure: false }
}));

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

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

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

  // Serialization and deserialization here...
  passport.serializeUser((user, done) => {
    done(null, user._id);
  });
  passport.deserializeUser((id, done) => {
    myDataBase.findOne({ _id: new ObjectID(id) }, (err, doc) => {
      done(null, doc);
    });
  });
  // Be sure to add this...
}).catch((e) => {
  app.route('/').get((req, res) => {
    res.render('pug', { title: e, message: 'Unable to login' });
  });
});
// app.listen out here...

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




Your browser information:

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

Challenge: Implement the Serialization of a Passport User

Link to the challenge:

Hello there,

The important part in this kind of issue is to look at the console (logs in Glitch), and see if there are any errors. More often than not, there will be an error about the database connection string, or it will explain that there is an issue somewhere else in the app.

Remember: The console is your friend, during development.

Hope this helps

/*
*
*
*
*
*
*
*
*
*
*
*
*       DO NOT EDIT THIS FILE
*       For FCC testing purposes!
*
*
*
*
*
*
*
*
*
*
*
*/

/*
*  THIS FILE IS FOR freeCodeCamp TO BE ABLE TO TEST YOUR CODE PROPERLY
*
*  ~DO NOT EDIT!~
*
*/


'use strict';

const fs = require('fs');

const allowedOrigins = [/^https?:\/\/([\w-]+\.)*freecodecamp.org/, /^http:\/\/localhost:\d+/];

module.exports = function (app) {

  app.use(function (req, res, next) {
    const origin = req.get('origin');
    if (allowedOrigins.some(regex => regex.test(origin))) {
      res.setHeader('Access-Control-Allow-Origin', origin);
      console.log(origin);
    }

    res.setHeader('Access-Control-Allow-Credentials', true);
    next();
  });

  app.route('/_api/server.js')
    .get(function (req, res, next) {
      console.log('requested');
      fs.readFile(process.cwd() + '/server.js', function (err, data) {
        if (err) return next(err);
        res.send(data.toString());
      });
    });

  app.route('/_api/routes.js')
    .get(function (req, res, next) {
      console.log('requested');
      fs.readFile(process.cwd() + '/routes.js', function (err, data) {
        if (err) return next(err);
        res.send(data.toString());
      })
    })

  app.route('/_api/auth.js')
    .get(function (req, res, next) {
      console.log('requested');
      fs.readFile(process.cwd() + '/auth.js', function (err, data) {
        if (err) return next(err);
        res.send(data.toString());
      })
    })

  app.route('/_api/package.json')
    .get(function (req, res, next) {
      console.log('requested');
      fs.readFile(process.cwd() + '/package.json', function (err, data) {
        if (err) return next(err);
        res.type('txt').send(data.toString());
      });
    });

  app.get('/_api/app-info', function (req, res) {
    var hs = Object.keys(res._headers)
      .filter(h => !h.match(/^access-control-\w+/));
    var hObj = {};
    hs.forEach(h => { hObj[h] = res._headers[h] });
    delete res._headers['strict-transport-security'];
    res.json({ headers: hObj });
  });

};

Hi Sky020,

this is the console log, i dont know what to fix in it.

Sincerely,
Henry Chang.

That is the server.js file. That is not the logs from the console. In Glitch the logs look like this:

can we go over this step by step because i am a little overwhelmed about this

That error suggests:
a) Your password/username (in the URI string) is incorrect
b) You have not set up your Atlas account with the correct permissions.

I cannot confirm either of these things for you.

This is turning into a nightmare for me. I do have atlas and i cannot connect the dots. I did this all in the previous steps and it worked. how is it not working for this part.

Do not worry. As long as you are learning something, you are accomplishing the main goal of fCC.

Take it as slowly as needed.

Have you whitelisted all IP addresses? (Last I remember this involves setting the allowed IP addresses to 0.0.0.0)

This is my IP Address

Just looking at your code:
image

I suspect using the fancy quotation marks could cause problems. Especially, if you include them in your DB URI.

I suggest you use normal quotation marks:

  • Normal: '
  • Yours:

Hope this helps

Glitch :・゚✧. this is my code and i do not see that

Ah, ok. I clicked on the link in your original post.

The IP Address is 0.0.0.0

Thank you, for letting me join the Glitch project.

Are you aware your password is not supposed to include the <> (lt/gt) symbols? Try removing them.

I have removed the image showing your database URI. This is a personal access URI for you to use. Anyone with access to it can read and write to your database, and, if you forget to one day, someone could end up charging money to your database account, by abusing it.

I have commented on your project a typo in your app.get('/'..).

It passed and I am going to leave this here because it is a perfect example of someone who falls in this trap later on.

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