In the less sexually defined serialise user and I don't think I need to really review what's going on
there because I just repeated myself like three times in a row probably.
So the next thing we have to do is define a very similar function called De serialise user in which
we are going to take the ID that we had previously stuffed in the cookie and turn it back into an actual
user model.
So very similar way of defining the second function we're going to say at passport dot de serialise
user.
And then we're going to pass an arrow function into there as well.
Now I want to tell you very quickly please make sure that you check your spelling on serialise user
and serialise user.
Very easy to make typos on both these.
If you see some error in your server eventually that says like DC relies user is not a function that
means you almost definitely made a typo on one of these.
So do you serialize user takes a function.
The first argument is the exact token that we had previously stuffed into a cookie and for us that is
the user's ID.
So we had previously stuff this into the cookie when DC realized user is called We get whatever had
been in the cookie.
And for us that is going to be the ID the user's ID.
So the first argument is the ID.
The second argument is the done function which we have to call after we have successfully turned the
ID back into a user.
OK.
So remember user up here was a user model instance it was a mongoose model.
We turned it into an ID and now we're doing the exact opposite.
We are going to turn an ID into a mongoose model instance.
So in other words we want to search over our big old collection of all of our different users that exist
inside of our database.
And after we find a very particular user We will then return a rule called done with that user so that
looks like in practice.
First off because we want to do a search or a query over our whole collection of users remember weve
done this previously and for that we use our model class which is capital-T you user will say user Daut
find by ID.
So we had previously issued a query by using the Find the one function down here.
So find one allowed us to pass in some search criteria that return the very first record that match
that criteria another query that we can use is find by ID we pass in the ID of the record that we want
to find to this function like the.
Now remember any time we access our Mongo database no matter what it is always an asynchronous action
and to deal with this we have to assume that it returns a promise that will be resolved after a user
with the given ID is found inside of here will change on it then which will be called with the user
model.
So whatever we just pulled out of the database right there and then we will call done with that user
that we just fetched out the database.
So I will say done and remember done always takes an air object if one exists.
So it will say you know everything probably worked OK.
No.
And then as the second argument we pass in the user that we just pulled out okay.
So thats pretty much it.
So we now have the ability to take our user model and put some identifying piece of information into
the cookie and then pull it back out and turn it back into a user at some point in the future.
Now I had said that all this cookie stuff was the last step and it really is.
So to really close this stuff out we cant just define serialise user and serialize user we have to also
instruct the passport that we want it to manage all of our authentication by using a cookie.
So remember a passport is overall is just very general very basic set of helpers for handling authentication.
It has many different ways of keeping track of a user one of which is making use of cookies.
So the last thing we have to do is make sure that passport is aware that it needs to make use of cookies
to keep track of the currently signed in user.
So last we have to do we're going to take care of that in the next section and we will be able to test
out our entire authentication flow.
So quick pause and I'll see you in the next section.