In the last section we created a new user instance and saved it to our database.
Any time a user came back to our application from the Google flow now we tested this out and we were
able to verify.
Yes we did it correctly save a user to our database.
However there's just one little issue right now.
If I open up a new tab again and go to localhost Colin 5000 slash off slash Google again.
Yeah the browser still hangs.
That's totally fine.
But if I go over to my inland collection again and refresh the page you will now notice that there are
two user records inside of my user user collection with identical Google IDs.
Now this really is not ideal for any user who signs into application.
We want to have just exactly one user account for them or one user record.
So I think that we need to modify that call back where we are currently creating a new user an ad just
a little bit more logic to it.
So here's what we're going to do when the user comes back to us from the Google flow and we get access
to their profile before we create a new user.
We will check to see if anyone inside of our users collection already has this given profile I.D. or
the profile ID that we just got back from the Google flow.
Now if someone does have that profile ID will then fantastic.
We do not need to create a new record so do not create a new model instance and do not save it.
But if no one has used that profile ID before then.
OK.
This must be a new person.
We need to create a new record and save it to that collection.
So the linchpin here is clearly we have to make sure that we somehow query our existing collection we
have to do a search and there we have to check to see if anyone has used this profile ID.
So lets figure out how we do queries using mongoose.
The first thing I want you to remember is that we currently have access to a model class which represents
the entire underlying collection of records that exist inside of our database.
We make use of that model class to search over all the records that exist inside the collection.
So let's flip back over to our code editor and we're going to add a line of code that's going to use
our motto class which is the capital you use or right here to initiate a query or initiate a search
over all the records inside the collection to do so.
We will write user find one.
And then we're going to pass some search criteria inside of an object.
We're going to say attempt to find one record inside the user's collection where there are Google ID
is equal to profile dot Id like so now again triple check make sure you've got a lowercase d here for
ID no capitals.
So again look through the users collection find the first record inside that collection with a google
id of profile ID.
So this line of code right here will essentially initiate that query for us.
But before we try to make use of this query in practice there's one last little thing we have to do
any time we reach out to our Mongo database in any way shape or form be it to search through a collection
or to save a new record to it or to edit an existing record or to delete one.
It doesn't matter.
No matter what it is.
Any time we reach out to our database we are initiating an asynchronous action.
In other words we can't just say like oh yeah this returns our user right here.
This line of code right here does not return the user directly.
We can't just put down Konst user and attempt to get access to the results of the query.
Instead the query returns a promise a promise is a tool that we use with javascript for handling asynchronous
code.
Now in this course we are not going to be using promises very heavily because we're going to use an
advanced feature of javascript that was included in E.S. 2017 for kind of making promises look a little
bit nicer.
So I'm going to tell you right now that if you're not familiar with promises that is OK.
But for this very first query that we're going to do we will use a promise and then later on we will
come back and refactor it a little bit.
So if you're not familiar with promises toy fine.
Just follow along for one second and then we'll come back later and refactor this stuff a little bit
more.
OK so find one returns a promise to get some indication or some signal of where when the query has completed
we will chain on a dot then statement inside of there we will add an arrow function and the arrow function
will be called with whatever the user was found.
If one exists we're going to call that the existing user.
So this will be a model instance in model instance that represents a user who was found.
Now if a user has a google id of profile ID then the argument existing user right here will be equal
to null.
So to figure out whether or not we found a user you want to figure out whether or not existing user
exists or not we can ask a very simple question will say if existing user exists like so.
So if that existing user exists that means we already have a record with the given profile ID
and that means that we do not need to create and save a new record else we want to create a new user
in which case we don't have a user record with this Id make a new record.
And so this case down here would be the case in which we had previously created the new model instance
and saved it to our database.
So going to take this new user and save statement and then add it down here OK.
So this gets us at least one step further.
We are now making sure that we do not attempt to create a new user any time that one already exists
with a given profile ID.
Now really quick let's test it inside the browser and then we will move on to the next section and add
a little bit better logic to this statement right here.
So inside my browser I'm going to open up a new tab I will navigate to localhost 5000 slash Auth. slash
Google.
Now of course it's still going to hang.
But if we flip back over to in lab and refresh the collection we should still only see two records.
And there's Arti right there.
Now of course we still don't even want to records for our test database here so I'm going to delete
one of these records by clicking the little trash can on the right hand side.
So now we can go through our flow as many times as we want.
So I'm going to do it again.
And no matter how many times we do it we are definitely guaranteed that we will only ever create exactly
one record inside of our users collection to represent this given user.
OK.
So that's pretty much it.
That gets us one step further.
But we still have to kind of you know look at the browser.
I don't mean to tell you what we have to do everything is still kind of just hanging in the browser.
Clearly we are really not done with this callback function right here.
So let's take a quick break come back in the next section and add a little bit more logic to kind of
nudge passport along after we figure out whether or not we need to create a user.
So I'll see you in the next section.