Files
freeCodeCamp/curriculum/challenges/english/06-information-security-and-quality-assurance/advanced-node-and-express/implement-the-serialization-of-a-passport-user.english.md
Randell Dawson 9cd57105af fix(curriculum): changed test text to use should for Information Security and Quality Assurance (#37763)
* fix: changed test text to use should

* fix: corrected typo

Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com>

* fix: corrected typo

Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com>

* fix: remove unnecessary backslash

Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com>

* fix: simplified text

Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com>

* fix: added period

Co-Authored-By: Manish Giri <manish.giri.me@gmail.com>
2019-11-20 09:58:14 -05:00

3.3 KiB

id, title, challengeType, forumTopicId
id title challengeType forumTopicId
5895f70cf9fc0f352b528e67 Implement the Serialization of a Passport User 2 301556

Description

As a reminder, this project is being built upon the following starter project on Glitch, or cloned from GitHub. Right now we're not loading an actual user object since we haven't set up our database. This can be done many different ways, but for our project we will connect to the database once when we start the server and keep a persistent connection for the full life-cycle of the app. To do this, add MongoDB as a dependency and require it in your server. (const mongo = require('mongodb').MongoClient;) Now we want to the connect to our database then start listening for requests. The purpose of this is to not allow requests before our database is connected or if there is a database error. To accomplish you will want to encompass your serialization and your app listener in the following:
mongo.connect(process.env.DATABASE, (err, db) => {
  if(err) {
    console.log('Database error: ' + err);
  } else {
    console.log('Successful database connection');

    //serialization and app.listen
  }
});

You can now uncomment the block in deserializeUser and remove your done(null, null). Be sure to set DATABASE in your .env file to your database's connection string (for example: DATABASE=mongodb://admin:pass@mlab.com:12345/my-project). You can set up a free database on mLab. Congratulations- you've finished setting up serialization! Submit your page when you think you've got it right. If you're running into errors, you can check out the project completed up to this point here.

Instructions

Tests

tests:
  - text: Database connection should be present.
    testString: getUserInput => $.get(getUserInput('url')+ '/_api/server.js') .then(data => { assert.match(data, /mongo.connect/gi, 'You should have created a connection to your database'); assert.match(data, /mongo.connect[^]*app.listen[^]*}[^]*}/gi, 'You should have your app.listen nested at within your database connection at the bottom'); }, xhr => { throw new Error(xhr.statusText); })
  - text: Deserialization should now be correctly using the DB and <code>done(null, null)</code> should be erased.
    testString: getUserInput => $.get(getUserInput('url')+ '/_api/server.js') .then(data => { assert.notMatch(data, /null,( |)null/gi, 'The callback in deserializeUser of (null, null) should be completely removed for the db block uncommented out'); }, xhr => { throw new Error(xhr.statusText); })

Challenge Seed

Solution

/**
  Backend challenges don't need solutions, 
  because they would need to be tested against a full working project. 
  Please check our contributing guidelines to learn more.
*/