1 00:00:00,840 --> 00:00:03,660 -: In the last section we did a little bit of refactoring 2 00:00:03,660 --> 00:00:05,550 where we pulled out some request logic 3 00:00:05,550 --> 00:00:08,280 to on an authentication controller 4 00:00:08,280 --> 00:00:10,950 over an authentication js. 5 00:00:10,950 --> 00:00:14,940 Our goal now is to read in a user if one is passed. 6 00:00:14,940 --> 00:00:17,040 So like an email and a password. 7 00:00:17,040 --> 00:00:19,020 We're gonna check to see if a 8 00:00:19,020 --> 00:00:21,750 user account already exists with that given email 9 00:00:21,750 --> 00:00:24,150 cause we don't wanna make any duplicate records. 10 00:00:24,150 --> 00:00:25,650 If one does not exist, 11 00:00:25,650 --> 00:00:27,300 we're gonna go ahead and save the record 12 00:00:27,300 --> 00:00:29,227 and then respond to the request and say, 13 00:00:29,227 --> 00:00:31,587 "Okay, user has been successfully created." 14 00:00:32,430 --> 00:00:34,740 So, that's a lot of steps to put in all at once, 15 00:00:34,740 --> 00:00:35,880 so, let's put in some comments 16 00:00:35,880 --> 00:00:37,983 to help guide us through this process. 17 00:00:38,820 --> 00:00:40,080 First thing we need to do 18 00:00:40,080 --> 00:00:44,790 is see if a user with a given email exists. 19 00:00:44,790 --> 00:00:46,410 So again, we're going to be given 20 00:00:46,410 --> 00:00:49,320 an email and a password as strings. 21 00:00:49,320 --> 00:00:51,900 So we'll see if a user with a given email exists 22 00:00:51,900 --> 00:00:53,943 because we don't want any duplicates. 23 00:00:54,870 --> 00:00:58,563 If a user with email does exist, 24 00:00:59,580 --> 00:01:00,993 return an error. 25 00:01:02,430 --> 00:01:06,360 And then if a user without 26 00:01:06,360 --> 00:01:09,630 or excuse me, with email does not exist. 27 00:01:09,630 --> 00:01:11,220 So in other words, this is like a fresh email, 28 00:01:11,220 --> 00:01:12,723 it's never been used before. 29 00:01:14,070 --> 00:01:15,480 Create 30 00:01:15,480 --> 00:01:18,660 and save user record 31 00:01:18,660 --> 00:01:22,353 and then we will respond to request, 32 00:01:25,230 --> 00:01:28,530 indicating the user was created. 33 00:01:28,530 --> 00:01:30,600 It's kind of a four step process here 34 00:01:30,600 --> 00:01:35,600 but I'm kind of glossing over one very critical step here. 35 00:01:35,730 --> 00:01:37,087 The first step I've listed says, 36 00:01:37,087 --> 00:01:39,720 "See if a user with a given email exists". 37 00:01:39,720 --> 00:01:42,210 And so I'm kind of assuming at this first step right here 38 00:01:42,210 --> 00:01:44,580 that we already have access to that email, right? 39 00:01:44,580 --> 00:01:46,170 But we haven't really discussed 40 00:01:46,170 --> 00:01:49,410 how we pull any information out of this request object. 41 00:01:49,410 --> 00:01:50,670 Remember the request object 42 00:01:50,670 --> 00:01:53,640 is what kind of summarizes the incoming request 43 00:01:53,640 --> 00:01:54,630 or it symbolizes it. 44 00:01:54,630 --> 00:01:57,930 It contains a lot of data about that incoming request. 45 00:01:57,930 --> 00:01:58,800 So we need to figure out 46 00:01:58,800 --> 00:02:02,193 how to pull some data out of this request object. 47 00:02:03,270 --> 00:02:06,120 To pull data outta request, request object 48 00:02:06,120 --> 00:02:07,890 when it is a post request 49 00:02:07,890 --> 00:02:12,240 we can make reference to the req.body property. 50 00:02:12,240 --> 00:02:17,240 So, .body means anything contained within this post request. 51 00:02:17,790 --> 00:02:19,920 Let's see what it looks like in practice. 52 00:02:19,920 --> 00:02:23,460 I'm gonna put a console login just to console log the body, 53 00:02:23,460 --> 00:02:25,010 that's all we're gonna do here. 54 00:02:27,030 --> 00:02:28,780 I'm gonna flip back over to Postman 55 00:02:30,780 --> 00:02:31,950 and I'm gonna change the body, 56 00:02:31,950 --> 00:02:33,360 I've already got some test data in here, 57 00:02:33,360 --> 00:02:35,160 so I'm gonna take it out. 58 00:02:35,160 --> 00:02:37,330 So right now if I send a post request 59 00:02:38,220 --> 00:02:39,630 and I'm not gonna get a response right now 60 00:02:39,630 --> 00:02:41,150 because we aren't responding to the request 61 00:02:41,150 --> 00:02:42,450 in the route handler, 62 00:02:42,450 --> 00:02:44,370 but I should be able to check my terminal 63 00:02:44,370 --> 00:02:46,680 and see, okay, here's my console log right here. 64 00:02:46,680 --> 00:02:48,270 So this is the body object. 65 00:02:48,270 --> 00:02:50,280 I didn't pass any data with my request 66 00:02:50,280 --> 00:02:51,750 in Postman when I made it, 67 00:02:51,750 --> 00:02:54,510 so, right now it's just an empty object. 68 00:02:54,510 --> 00:02:58,680 Let's see what happens, I'm going to cancel the request. 69 00:02:58,680 --> 00:03:01,833 Let's see what happens when we post some data along. 70 00:03:03,030 --> 00:03:05,040 So on the body tab in postman 71 00:03:05,040 --> 00:03:08,280 I'm gonna select the raw option over here 72 00:03:08,280 --> 00:03:09,120 and I'm gonna make sure 73 00:03:09,120 --> 00:03:14,043 that JSON application/JSON is selected as my request type. 74 00:03:15,390 --> 00:03:17,700 Then I'm going to add some JSON in here 75 00:03:17,700 --> 00:03:22,700 and I'm gonna give it an email of "test@example.com" 76 00:03:24,450 --> 00:03:29,220 and a password of how about just "123". 77 00:03:29,220 --> 00:03:31,230 Keep in mind that this is actual JSON 78 00:03:31,230 --> 00:03:32,520 that is expected right here. 79 00:03:32,520 --> 00:03:34,500 So we have to use double quotes 80 00:03:34,500 --> 00:03:37,050 around both the keys and the values, 81 00:03:37,050 --> 00:03:38,220 that is, this is not optional. 82 00:03:38,220 --> 00:03:39,900 We have to have double quotes 83 00:03:39,900 --> 00:03:41,610 around all these different values in here. 84 00:03:41,610 --> 00:03:43,410 Otherwise you're gonna get an error 85 00:03:43,410 --> 00:03:45,543 when the server tries to parse this JSON. 86 00:03:46,470 --> 00:03:47,520 One other thing I wanna say is 87 00:03:47,520 --> 00:03:49,170 I'm sorry I can't really make this any larger, 88 00:03:49,170 --> 00:03:51,120 I know the text is really small here. 89 00:03:51,120 --> 00:03:54,120 Unfortunately, I can't really zoom with postman so, 90 00:03:54,120 --> 00:03:57,270 you guys can, I'm sure you can follow along regardless. 91 00:03:57,270 --> 00:04:01,320 Anyways, we've now gotten email and a password 92 00:04:01,320 --> 00:04:05,310 being passed in the body of our post request. 93 00:04:05,310 --> 00:04:06,390 Let's send this along 94 00:04:06,390 --> 00:04:09,040 and see what we get console logged on the other side. 95 00:04:10,890 --> 00:04:12,990 So now I'll go back over to our server 96 00:04:12,990 --> 00:04:17,820 and I've got req.body is email 97 00:04:17,820 --> 00:04:19,740 and it has the password as well. 98 00:04:19,740 --> 00:04:22,170 So this is how we're gonna get access to our data 99 00:04:22,170 --> 00:04:23,250 in our post request. 100 00:04:23,250 --> 00:04:25,350 This is how we're going to get access to this email 101 00:04:25,350 --> 00:04:27,150 that we referenced right here. 102 00:04:27,150 --> 00:04:28,680 To get access to our email, 103 00:04:28,680 --> 00:04:33,680 you can say const email is req.body.email, 104 00:04:34,470 --> 00:04:39,470 and const password is req.body.password. 105 00:04:40,020 --> 00:04:42,320 And I'm gonna take off the console log up top. 106 00:04:43,380 --> 00:04:44,910 Okay, so that's how we get our 107 00:04:44,910 --> 00:04:46,920 kind of relevant data off this request. 108 00:04:46,920 --> 00:04:48,930 This is the data that we're going to use to figure out 109 00:04:48,930 --> 00:04:51,130 whether or not we should make this new user. 110 00:04:52,860 --> 00:04:56,580 Now, let's go back to that was basically step 0.1. 111 00:04:56,580 --> 00:04:58,590 Let's go back to step one 112 00:04:58,590 --> 00:05:03,090 which is we need to see if a user with a given email exists. 113 00:05:03,090 --> 00:05:04,650 So this means we need to go through 114 00:05:04,650 --> 00:05:06,590 all of our existing database records. 115 00:05:06,590 --> 00:05:08,850 We have to search through every last one 116 00:05:08,850 --> 00:05:12,720 and we have to see if an existing user has already created 117 00:05:12,720 --> 00:05:15,180 or if an existing user already exists 118 00:05:15,180 --> 00:05:18,330 with this exact email right here. 119 00:05:18,330 --> 00:05:21,300 If one does exist, then we wanna throw 120 00:05:21,300 --> 00:05:24,360 or return an error on the request. 121 00:05:24,360 --> 00:05:26,370 So that means that we need some ability 122 00:05:26,370 --> 00:05:28,290 to search through all of our records. 123 00:05:28,290 --> 00:05:29,910 We need some ability to look through 124 00:05:29,910 --> 00:05:31,923 our entire database at this point. 125 00:05:32,940 --> 00:05:34,500 So to look through all of our records, 126 00:05:34,500 --> 00:05:36,210 to search through all of our users, 127 00:05:36,210 --> 00:05:38,190 we're going to use the user model 128 00:05:38,190 --> 00:05:40,473 that we already created using Mongoose. 129 00:05:41,490 --> 00:05:42,810 Let's see what it looks like. 130 00:05:42,810 --> 00:05:45,340 At the top, I'm going to import 131 00:05:47,190 --> 00:05:48,573 our user model. 132 00:05:50,730 --> 00:05:53,970 And then the user model has a method. 133 00:05:53,970 --> 00:05:55,290 This is, remember at this point in time, 134 00:05:55,290 --> 00:05:56,670 we have the user class right here. 135 00:05:56,670 --> 00:05:59,040 So this is the class of users, it's not an instance, 136 00:05:59,040 --> 00:06:02,520 it doesn't represent a single user, it represents all users. 137 00:06:02,520 --> 00:06:04,710 My entire collection of records 138 00:06:04,710 --> 00:06:07,830 of all users that are saved in the database. 139 00:06:07,830 --> 00:06:10,260 So we can search through this collection of users 140 00:06:10,260 --> 00:06:12,663 by using the method, findOne. 141 00:06:14,919 --> 00:06:17,430 FindOne is a method whose first argument 142 00:06:17,430 --> 00:06:20,550 is kind of the search criteria that we want to use. 143 00:06:20,550 --> 00:06:23,730 So, we're going to say find one user 144 00:06:23,730 --> 00:06:26,520 who has an email of email, 145 00:06:26,520 --> 00:06:27,353 email being 146 00:06:27,353 --> 00:06:30,250 the email coming at directly off of the request right here. 147 00:06:31,680 --> 00:06:33,750 After this search is completed 148 00:06:33,750 --> 00:06:35,640 it's going to invoke a callback. 149 00:06:35,640 --> 00:06:37,140 Remember, just about everything in Node 150 00:06:37,140 --> 00:06:38,340 happens with callbacks. 151 00:06:38,340 --> 00:06:41,130 So searching through our list of users, 152 00:06:41,130 --> 00:06:42,660 you know all the users in our database, 153 00:06:42,660 --> 00:06:45,990 it might take some, you know, unknown amount of time. 154 00:06:45,990 --> 00:06:49,590 So, once the search is complete 155 00:06:49,590 --> 00:06:53,106 it's gonna call a callback that we pass. 156 00:06:53,106 --> 00:06:55,720 And the arguments to this callback 157 00:06:55,720 --> 00:06:57,180 are going to be an error object 158 00:06:57,180 --> 00:06:59,130 if there was an error during the search 159 00:07:01,170 --> 00:07:03,780 and what we're going to call an existing user. 160 00:07:03,780 --> 00:07:07,800 Where existing user is basically a user who already exists 161 00:07:07,800 --> 00:07:09,573 with this email right here. 162 00:07:11,310 --> 00:07:12,390 So this is how we're gonna see 163 00:07:12,390 --> 00:07:15,300 if a user with that given email exists. 164 00:07:15,300 --> 00:07:17,310 We just search through all of our users, 165 00:07:17,310 --> 00:07:20,580 we see if someone already has this existing email, 166 00:07:20,580 --> 00:07:23,040 if they do, this argument right here 167 00:07:23,040 --> 00:07:26,100 will be populated with that found user, 168 00:07:26,100 --> 00:07:29,670 the one with that kind of already existing email address. 169 00:07:29,670 --> 00:07:32,760 If they do not exist, if this is a completely fresh account 170 00:07:32,760 --> 00:07:35,130 this value right here will be null instead. 171 00:07:35,130 --> 00:07:36,270 So that's how we're going to tell 172 00:07:36,270 --> 00:07:38,470 whether or not this email is already in use. 173 00:07:39,570 --> 00:07:40,800 Okay, so this is, 174 00:07:40,800 --> 00:07:43,230 I think a good place to take a quick break. 175 00:07:43,230 --> 00:07:44,970 We've got step one complete here. 176 00:07:44,970 --> 00:07:47,700 We can see if a user already has a give email. 177 00:07:47,700 --> 00:07:49,680 We've also got the ability to pull off 178 00:07:49,680 --> 00:07:51,870 an email and password from the request. 179 00:07:51,870 --> 00:07:52,703 In the next section, 180 00:07:52,703 --> 00:07:55,710 let's continue by implementing the next three steps. 181 00:07:55,710 --> 00:07:56,660 I'll see you there.