In the last section we made it clear he looked through our users collection and find exactly one record
where someone had a google id of the profile ID that was just returned by the process.
Now we made a query right here.
Anytime we make a query that reaches out to our Mongo database which means it's an asynchronous operation
and to deal with that we get a promise.
So we changed on then that was called within the existing user if one was found.
So existing user right here is either going to be a mongo mob it's a mongoose model instance I remember
an instance represents exactly one record or existing user will be NULL if the record was not found.
So we were then able to look at the existing user and say if something exists there like if that is
a user model instance then great.
We've already got a record.
Otherwise if the existing user is no well then obviously we need to create a new one and save it to
the database.
So now we've got all this logic put together we need to do one last thing inside of this callback function.
So I've taken the diagram that we're looking at just a second ago and I added that it added in one last
step to be clear the Google strategy that we are making use of gives us this callback explicitly so
that we can do exactly what we are doing right now which is to create a record or save a record for
a database that represents this new user.
So after we have finished with all this user creation or this user fetching stuff to tell passport or
to tell that strategy that we are all done doing our thing we have to inform it that we're finished
by calling the done callback or the done function.
This tells passport that we have now finished making this user we've finished all this kind of junk.
And it should now resume the authentication process.
And so this dumb thing right here we already have included inside the callback.
Look at the argument list to the callback and you'll notice that the fourth argument is called.
Done.
So this is a function when every call done it tells passport hey we're finished here.
We've made the user Here's the user.
This is it we're all finished.
Proceed with the authentication flow.
So let's make sure that after we have found an existing user or saved into the database we tell passport
that we are all complete by calling this done function.
So we'll take care of the first case first and which we found a record inside of our collection and
we will call.
Done.
Now it's not as easy as just calling done like so we have to provide two arguments to it.
The first argument will be an air object.
This object communicates back to a passport that maybe something went wrong or maybe something didn't
quite work the way we expected.
Now if we found a user inside of a user's collection that means everything went fine and totally according
to plan.
So we will pass in a first argument of all which indicates hey there's no air here.
Everything went completely fine.
Right we are totally happy.
Then the second argument will be the user record which is existing user.
So this tells Monck this tells passport hey we're all finished here.
Here is the user that we just found or the user that we just created.
Everything is done we are finished now.
This is going to work out just fine for the first case but for the second case we have to do something
a little bit more complex.
Remember any time we save a record to our Mongo database it is an asynchronous operation.
We do not want to call it done right here until we know for a fact that the user has been successfully
saved to the database.
So in order to get a notification or get some something to tell us that yes the user has been successfully
saved right here we have to chain on a dot then statement then statement will be called with the new
user who has just saved.
So we'll take the user was just saved.
That's provided as an argument to the function that we passed to then and then just like we did right
here we will take that new user who has just saved the database.
We would call done with the first argument of Noal.
And then as a second argument we will pass and the user who has just saved.
OK so just to kind of clarify something that I have found is sometimes confusing for people.
When we called new user right here remember that creates a Mungo's mongoose model instance a model instance
represents a single record inside of our collection.
So this creates a new model right here a new model instance we then save that instance and in the callback
we get another model instance.
So at this point we are really working with like two separate model instances.
We've got the one that we just created right here.
And then we have a second instance right here.
Now they both represent the exact same underlying model or the exact same record inside of our collection
but by convention we always make use of the one that's provided to us inside of the promised callback
because this one might have some additional changes to that was made to it while the user was being
saved.
So we kind of consider this initial user that we created right here to be like not quite as fresh not
quite as clean not as quite as well-maintained as the user that we just got back from the database so
that's pretty much it.
We're now ready to test this out inside the browser before we test it so I want to say really quickly.
This has been our first taste of mongoose and as we've been working with it we've also been kind of
like simultaneously juggling this passport stuff and so at this point a lot of the scene probably seems
kind of confusing.
And then on top of all of it we have also introduce some promises after we get everything on our authentication
flow working.
We're going to come back to this callback and refactor it to make it look a lot more legible than it
does right now.
So if this still looks confusing Don't sweat it in a little bit.
We'll come back.
We will refactor it to make it look much nicer.
And at that point we will have a second chance to understand exactly what is going on inside this function.
Ok so last thing we will test this out I'm going to make sure that my server is running and I do not
have any errors and yet looks like everything is OK.
I'm going to change back over to my browser.
I will access localhost 5000 slash off slash Google.
And before I go there just really quick I want to point out my users collection I've only got a single
record in there right now.
So I deleted the duplicate record that we already had.
So just one record at this point you can delete records in here by the way by clicking on a little trash
icon right here.
OK.
So we'll go here.
When I do so I can go refresh the collection.
I still only have one record in here.
Yup looks good.
Now it looks like nothing is still happening inside the browser.
So perhaps we saw a little bit of work to do on the passport side but we can at least rest easy knowing
that we have correctly progressed out of this callback function right here.
So clearly mug passport still expects us to do something to kind of Usher along the authentication process.
So let's take a quick break.
We'll come back in the next section and figure out exactly how we're going to kind of push passport
along to the next step in the authentication flow.
So I will see you in just a second.