Update serialization-of-a-user-object.english.md (#39518)

This commit is contained in:
Ezekiel Oladejo
2020-09-04 07:53:31 +01:00
committed by GitHub
parent a3856a5fd7
commit 4165135380

View File

@ -8,14 +8,15 @@ forumTopicId: 301563
## Description
<section id='description'>
As a reminder, this project is being built upon the following starter project on <a href="https://repl.it/github/freeCodeCamp/boilerplate-advancednode">Repl.it</a>, or cloned from <a href='https://github.com/freeCodeCamp/boilerplate-advancednode/'>GitHub</a>.
Serialization and deserialization are important concepts in regards to authentication. To serialize an object means to convert its contents into a small <em>key</em> essentially that can then be deserialized into the original object. This is what allows us to know whos communicated with the server without having to send the authentication data like username and password at each request for a new page.
Serialization and deserialization are important concepts in regards to authentication. To serialize an object means to convert its contents into a small <em>key</em> essentially that can then be deserialized into the original object. This is what allows us to know who has communicated with the server without having to send the authentication data like username and password at each request for a new page.
To set this up properly, we need to have a serialize function and a deserialize function. In passport we create these with <code>passport.serializeUser( OURFUNCTION )</code> and <code>passport.deserializeUser( OURFUNCTION )</code>
The serializeUser is called with 2 arguments, the full user object and a callback used by passport. Returned in the callback should be a unique key to identify that user- the easiest one to use being the users _id in the object as it should be unique as it generated by MongoDb. Similarly deserializeUser is called with that key and a callback function for passport as well, but this time we have to take that key and return the users full object to the callback. To make a query search for a Mongo _id you will have to create <code>const ObjectID = require('mongodb').ObjectID;</code>, and then to use it you call <code>new ObjectID(THE_ID)</code>. Be sure to add MongoDB as a dependency. You can see this in the examples below:
The <code>serializeUser</code> is called with 2 arguments, the full user object and a callback used by passport. Returned in the callback should be a unique key to identify that user - the easiest one to use being the user's <code>_id</code> in the object as it should be unique as it generated by MongoDB. Similarly, <code>deserializeUser</code> is called with that key and a callback function for passport as well, but, this time, we have to take that key and return the full user object to the callback. To make a query search for a Mongo <code>_id</code>, you will have to create <code>const ObjectID = require('mongodb').ObjectID;</code>, and then to use it you call <code>new ObjectID(THE_ID)</code>. Be sure to add MongoDB as a dependency. You can see this in the examples below:
```js
passport.serializeUser((user, done) => {
done(null, user._id);
});
passport.deserializeUser((id, done) => {
db.collection('users').findOne(
{_id: new ObjectID(id)},
@ -26,7 +27,7 @@ passport.deserializeUser((id, done) => {
});
```
NOTE: This deserializeUser will throw an error until we set up the DB in the next step so comment out the whole block and just call <code>done(null, null)</code> in the function deserializeUser.
NOTE: This <code>deserializeUser</code> will throw an error until we set up the DB in the next step so comment out the whole block and just call <code>done(null, null)</code> in the function <code>deserializeUser</code>.
Submit your page when you think you've got it right.
</section>