1 00:00:00,780 --> 00:00:05,580 In the last section we created a new user instance and saved it to our database. 2 00:00:05,580 --> 00:00:11,730 Any time a user came back to our application from the Google flow now we tested this out and we were 3 00:00:11,760 --> 00:00:12,750 able to verify. 4 00:00:12,780 --> 00:00:16,860 Yes we did it correctly save a user to our database. 5 00:00:16,890 --> 00:00:19,340 However there's just one little issue right now. 6 00:00:19,620 --> 00:00:26,610 If I open up a new tab again and go to localhost Colin 5000 slash off slash Google again. 7 00:00:26,850 --> 00:00:28,020 Yeah the browser still hangs. 8 00:00:28,020 --> 00:00:28,920 That's totally fine. 9 00:00:29,070 --> 00:00:34,860 But if I go over to my inland collection again and refresh the page you will now notice that there are 10 00:00:34,890 --> 00:00:41,190 two user records inside of my user user collection with identical Google IDs. 11 00:00:41,190 --> 00:00:45,090 Now this really is not ideal for any user who signs into application. 12 00:00:45,090 --> 00:00:51,430 We want to have just exactly one user account for them or one user record. 13 00:00:51,450 --> 00:00:56,910 So I think that we need to modify that call back where we are currently creating a new user an ad just 14 00:00:56,970 --> 00:00:59,300 a little bit more logic to it. 15 00:00:59,310 --> 00:01:04,470 So here's what we're going to do when the user comes back to us from the Google flow and we get access 16 00:01:04,470 --> 00:01:08,300 to their profile before we create a new user. 17 00:01:08,310 --> 00:01:15,180 We will check to see if anyone inside of our users collection already has this given profile I.D. or 18 00:01:15,180 --> 00:01:18,620 the profile ID that we just got back from the Google flow. 19 00:01:19,080 --> 00:01:23,520 Now if someone does have that profile ID will then fantastic. 20 00:01:23,520 --> 00:01:29,560 We do not need to create a new record so do not create a new model instance and do not save it. 21 00:01:29,760 --> 00:01:32,710 But if no one has used that profile ID before then. 22 00:01:32,730 --> 00:01:33,140 OK. 23 00:01:33,270 --> 00:01:34,450 This must be a new person. 24 00:01:34,470 --> 00:01:38,370 We need to create a new record and save it to that collection. 25 00:01:38,370 --> 00:01:44,550 So the linchpin here is clearly we have to make sure that we somehow query our existing collection we 26 00:01:44,550 --> 00:01:49,710 have to do a search and there we have to check to see if anyone has used this profile ID. 27 00:01:50,040 --> 00:01:54,340 So lets figure out how we do queries using mongoose. 28 00:01:54,390 --> 00:02:00,270 The first thing I want you to remember is that we currently have access to a model class which represents 29 00:02:00,270 --> 00:02:05,420 the entire underlying collection of records that exist inside of our database. 30 00:02:05,490 --> 00:02:11,810 We make use of that model class to search over all the records that exist inside the collection. 31 00:02:12,720 --> 00:02:18,540 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 32 00:02:18,540 --> 00:02:24,450 our motto class which is the capital you use or right here to initiate a query or initiate a search 33 00:02:24,510 --> 00:02:28,420 over all the records inside the collection to do so. 34 00:02:28,440 --> 00:02:32,460 We will write user find one. 35 00:02:32,460 --> 00:02:36,760 And then we're going to pass some search criteria inside of an object. 36 00:02:36,840 --> 00:02:43,890 We're going to say attempt to find one record inside the user's collection where there are Google ID 37 00:02:44,490 --> 00:02:52,950 is equal to profile dot Id like so now again triple check make sure you've got a lowercase d here for 38 00:02:52,950 --> 00:02:55,020 ID no capitals. 39 00:02:55,350 --> 00:03:01,440 So again look through the users collection find the first record inside that collection with a google 40 00:03:01,440 --> 00:03:04,660 id of profile ID. 41 00:03:04,710 --> 00:03:08,850 So this line of code right here will essentially initiate that query for us. 42 00:03:09,090 --> 00:03:13,230 But before we try to make use of this query in practice there's one last little thing we have to do 43 00:03:14,030 --> 00:03:21,510 any time we reach out to our Mongo database in any way shape or form be it to search through a collection 44 00:03:21,780 --> 00:03:26,630 or to save a new record to it or to edit an existing record or to delete one. 45 00:03:26,670 --> 00:03:27,380 It doesn't matter. 46 00:03:27,390 --> 00:03:28,740 No matter what it is. 47 00:03:28,820 --> 00:03:34,050 Any time we reach out to our database we are initiating an asynchronous action. 48 00:03:34,230 --> 00:03:38,440 In other words we can't just say like oh yeah this returns our user right here. 49 00:03:38,580 --> 00:03:42,450 This line of code right here does not return the user directly. 50 00:03:42,450 --> 00:03:46,940 We can't just put down Konst user and attempt to get access to the results of the query. 51 00:03:47,310 --> 00:03:55,230 Instead the query returns a promise a promise is a tool that we use with javascript for handling asynchronous 52 00:03:55,230 --> 00:03:56,610 code. 53 00:03:56,610 --> 00:04:01,830 Now in this course we are not going to be using promises very heavily because we're going to use an 54 00:04:01,830 --> 00:04:08,820 advanced feature of javascript that was included in E.S. 2017 for kind of making promises look a little 55 00:04:08,820 --> 00:04:09,880 bit nicer. 56 00:04:09,930 --> 00:04:13,650 So I'm going to tell you right now that if you're not familiar with promises that is OK. 57 00:04:13,830 --> 00:04:18,330 But for this very first query that we're going to do we will use a promise and then later on we will 58 00:04:18,330 --> 00:04:20,430 come back and refactor it a little bit. 59 00:04:20,670 --> 00:04:23,560 So if you're not familiar with promises toy fine. 60 00:04:23,640 --> 00:04:28,320 Just follow along for one second and then we'll come back later and refactor this stuff a little bit 61 00:04:28,320 --> 00:04:29,240 more. 62 00:04:29,620 --> 00:04:36,780 OK so find one returns a promise to get some indication or some signal of where when the query has completed 63 00:04:37,100 --> 00:04:44,400 we will chain on a dot then statement inside of there we will add an arrow function and the arrow function 64 00:04:44,400 --> 00:04:47,110 will be called with whatever the user was found. 65 00:04:47,160 --> 00:04:51,480 If one exists we're going to call that the existing user. 66 00:04:51,840 --> 00:04:58,710 So this will be a model instance in model instance that represents a user who was found. 67 00:04:58,740 --> 00:05:05,370 Now if a user has a google id of profile ID then the argument existing user right here will be equal 68 00:05:05,370 --> 00:05:06,600 to null. 69 00:05:06,600 --> 00:05:11,430 So to figure out whether or not we found a user you want to figure out whether or not existing user 70 00:05:11,430 --> 00:05:18,510 exists or not we can ask a very simple question will say if existing user exists like so. 71 00:05:18,510 --> 00:05:28,190 So if that existing user exists that means we already have a record with the given profile ID 72 00:05:32,370 --> 00:05:38,030 and that means that we do not need to create and save a new record else we want to create a new user 73 00:05:38,570 --> 00:05:46,600 in which case we don't have a user record with this Id make a new record. 74 00:05:46,600 --> 00:05:51,620 And so this case down here would be the case in which we had previously created the new model instance 75 00:05:51,680 --> 00:05:53,320 and saved it to our database. 76 00:05:53,540 --> 00:06:00,540 So going to take this new user and save statement and then add it down here OK. 77 00:06:01,330 --> 00:06:03,480 So this gets us at least one step further. 78 00:06:03,490 --> 00:06:08,740 We are now making sure that we do not attempt to create a new user any time that one already exists 79 00:06:08,800 --> 00:06:10,700 with a given profile ID. 80 00:06:10,870 --> 00:06:15,370 Now really quick let's test it inside the browser and then we will move on to the next section and add 81 00:06:15,370 --> 00:06:19,360 a little bit better logic to this statement right here. 82 00:06:19,720 --> 00:06:27,700 So inside my browser I'm going to open up a new tab I will navigate to localhost 5000 slash Auth. slash 83 00:06:27,700 --> 00:06:28,610 Google. 84 00:06:28,630 --> 00:06:30,170 Now of course it's still going to hang. 85 00:06:30,310 --> 00:06:37,050 But if we flip back over to in lab and refresh the collection we should still only see two records. 86 00:06:37,210 --> 00:06:39,130 And there's Arti right there. 87 00:06:39,190 --> 00:06:44,170 Now of course we still don't even want to records for our test database here so I'm going to delete 88 00:06:44,170 --> 00:06:48,480 one of these records by clicking the little trash can on the right hand side. 89 00:06:49,240 --> 00:06:53,070 So now we can go through our flow as many times as we want. 90 00:06:53,350 --> 00:06:55,130 So I'm going to do it again. 91 00:06:56,200 --> 00:07:01,630 And no matter how many times we do it we are definitely guaranteed that we will only ever create exactly 92 00:07:01,720 --> 00:07:06,140 one record inside of our users collection to represent this given user. 93 00:07:06,540 --> 00:07:06,910 OK. 94 00:07:07,000 --> 00:07:08,100 So that's pretty much it. 95 00:07:08,170 --> 00:07:09,500 That gets us one step further. 96 00:07:09,670 --> 00:07:13,800 But we still have to kind of you know look at the browser. 97 00:07:13,800 --> 00:07:17,290 I don't mean to tell you what we have to do everything is still kind of just hanging in the browser. 98 00:07:17,290 --> 00:07:21,070 Clearly we are really not done with this callback function right here. 99 00:07:21,160 --> 00:07:26,050 So let's take a quick break come back in the next section and add a little bit more logic to kind of 100 00:07:26,050 --> 00:07:30,720 nudge passport along after we figure out whether or not we need to create a user. 101 00:07:30,790 --> 00:07:32,450 So I'll see you in the next section.