1 00:00:00,510 --> 00:00:02,880 -: So this section that we're gonna start right now 2 00:00:02,880 --> 00:00:05,130 we're gonna start talking about some of the tricky stuff. 3 00:00:05,130 --> 00:00:05,963 So this is where some 4 00:00:05,963 --> 00:00:07,770 of the more challenging stuff is gonna start to come 5 00:00:07,770 --> 00:00:10,920 into play with regards to authentication. 6 00:00:10,920 --> 00:00:13,230 Let's take a look at a diagram to kind of look 7 00:00:13,230 --> 00:00:15,180 at where we are and where we're headed. 8 00:00:16,627 --> 00:00:17,820 So this is a diagram 9 00:00:17,820 --> 00:00:20,070 of all the different libraries that we're going to 10 00:00:20,070 --> 00:00:22,260 eventually use inside this application. 11 00:00:22,260 --> 00:00:26,400 And so far we've basically used everything on the left half. 12 00:00:26,400 --> 00:00:28,501 So we've got our http module 13 00:00:28,501 --> 00:00:32,051 from Node that's doing the low level request handling. 14 00:00:32,051 --> 00:00:34,980 We've got express for handling responses 15 00:00:34,980 --> 00:00:38,520 or for handling routing and whatnot. 16 00:00:38,520 --> 00:00:40,530 We've got Morgan for logging. 17 00:00:40,530 --> 00:00:41,880 We've got our body parser 18 00:00:41,880 --> 00:00:45,543 for parsing http requests to get J s O out them. 19 00:00:46,443 --> 00:00:48,660 And now we're gonna start moving onto the right hand side. 20 00:00:48,660 --> 00:00:49,920 So the next thing we're gonna be working 21 00:00:49,920 --> 00:00:53,790 on is our connection to our database 22 00:00:53,790 --> 00:00:55,533 which is gonna be MongoDB. 23 00:00:56,820 --> 00:00:59,100 MongoDB is not something that we're gonna work 24 00:00:59,100 --> 00:01:01,830 with just vanilla or just by itself, you know 25 00:01:01,830 --> 00:01:03,123 hands on bare metal. 26 00:01:03,123 --> 00:01:04,440 We're gonna be working 27 00:01:04,440 --> 00:01:06,003 with a library that's gonna be sitting 28 00:01:06,003 --> 00:01:10,140 in between us and MongoDB called Mongoose. 29 00:01:10,140 --> 00:01:12,571 So Mongoose is what's called an ORM 30 00:01:12,571 --> 00:01:15,990 and is a library that is used to interface 31 00:01:15,990 --> 00:01:17,910 with a database in some fashion. 32 00:01:17,910 --> 00:01:18,840 So we don't have to deal 33 00:01:18,840 --> 00:01:22,380 with the very direct manipulating the database and whatnot. 34 00:01:22,380 --> 00:01:24,150 All that behavior is gonna be abstracted 35 00:01:24,150 --> 00:01:26,013 out to Mongoose for us. 36 00:01:27,840 --> 00:01:30,240 We already installed Mongoose in an earlier section 37 00:01:30,240 --> 00:01:33,120 so we've already got Mongoose installed in our project. 38 00:01:33,120 --> 00:01:36,420 All we need to do now is actually make use of it. 39 00:01:36,420 --> 00:01:38,170 So to make use of Mongoose, the first thing we're 40 00:01:38,170 --> 00:01:42,390 gonna do is we're gonna create what's called a user model. 41 00:01:42,390 --> 00:01:44,190 So this is a model 42 00:01:44,190 --> 00:01:48,781 like a data model that represents a user most users 43 00:01:48,781 --> 00:01:50,550 and is certainly the one 44 00:01:50,550 --> 00:01:53,310 in our application is going to have two attributes 45 00:01:53,310 --> 00:01:56,883 or two properties and email and a password. 46 00:01:58,440 --> 00:02:00,990 We need to actually tell Mongoose about these two 47 00:02:00,990 --> 00:02:04,380 properties that we expect this user model to have. 48 00:02:04,380 --> 00:02:07,701 So in this section we're going to create a new user model. 49 00:02:07,701 --> 00:02:10,320 We're going to feed it to Mongoose 50 00:02:10,320 --> 00:02:11,250 and we're going to make sure 51 00:02:11,250 --> 00:02:15,990 that this model has a email field and a password. 52 00:02:15,990 --> 00:02:16,920 Okay, So let's go ahead 53 00:02:16,920 --> 00:02:17,970 and give this a shot. 54 00:02:20,760 --> 00:02:22,500 Back over in our project directory 55 00:02:22,500 --> 00:02:27,360 I'm gonna create a new folder called models. 56 00:02:27,360 --> 00:02:29,280 Right now we're only gonna have the user model 57 00:02:29,280 --> 00:02:31,620 but if I was expanding this project out in the future 58 00:02:31,620 --> 00:02:33,210 I would probably add in, you know 59 00:02:33,210 --> 00:02:36,213 additional files in here for the other models as well. 60 00:02:36,213 --> 00:02:39,060 So like we mentioned, we only have one model right now. 61 00:02:39,060 --> 00:02:41,160 It's gonna be the user model. 62 00:02:41,160 --> 00:02:42,630 And so I'm gonna create a new file 63 00:02:42,630 --> 00:02:46,773 inside the models directory called user dot js. 64 00:02:48,390 --> 00:02:51,690 So inside of this file is gonna be our local definition 65 00:02:51,690 --> 00:02:54,093 of exactly what a user is, and we're creating this 66 00:02:54,093 --> 00:02:57,540 so we can tell mongoose, hey, here's what a model is. 67 00:02:57,540 --> 00:03:00,570 It has an email, it has a a password, you know 68 00:03:00,570 --> 00:03:02,460 handle this for me, handle this. 69 00:03:02,460 --> 00:03:05,070 This is where we get to set up some level instructions 70 00:03:05,070 --> 00:03:07,721 for mongoose that describes our application. 71 00:03:07,721 --> 00:03:10,110 So as you might imagine, we're going to need to 72 00:03:10,110 --> 00:03:12,873 require mongoose up at the top of this file. 73 00:03:13,710 --> 00:03:18,580 We'll write cons to mongoose, require mongoose 74 00:03:21,450 --> 00:03:23,010 And then we're gonna pull one property 75 00:03:23,010 --> 00:03:27,600 off of Mongoose called Schema with a capital S. 76 00:03:27,600 --> 00:03:30,033 So right mongoose dot schema. 77 00:03:31,500 --> 00:03:32,550 So here's already, you know 78 00:03:32,550 --> 00:03:34,170 things are starting to get a little bit interesting 79 00:03:34,170 --> 00:03:35,850 right? Well, I said we needed Mongoose 80 00:03:35,850 --> 00:03:38,310 but what is this schema thing? 81 00:03:38,310 --> 00:03:41,010 So schema is what we use to tell mongoose 82 00:03:41,010 --> 00:03:43,170 about the very particular fields 83 00:03:43,170 --> 00:03:45,420 that our model is going to have. 84 00:03:45,420 --> 00:03:46,440 So the first thing we're gonna do 85 00:03:46,440 --> 00:03:51,000 in this file is we're going to define our model. 86 00:03:51,000 --> 00:03:52,290 We're gonna say, Hey, here's the model. 87 00:03:52,290 --> 00:03:53,490 It's a user model. 88 00:03:53,490 --> 00:03:56,253 It has a property email, it has a property password. 89 00:03:58,830 --> 00:04:02,716 We're then going to create the model class 90 00:04:02,716 --> 00:04:06,660 and then we're going to export the model 91 00:04:06,660 --> 00:04:10,410 so other files inside of our application can make use of it. 92 00:04:10,410 --> 00:04:12,937 So let's get started with defining our model first. 93 00:04:12,937 --> 00:04:17,279 To create a model, we'll create first a schema. 94 00:04:17,279 --> 00:04:22,280 So we'll say constant user schema is a new schema 95 00:04:22,673 --> 00:04:25,578 and we're gonna pass it in object. 96 00:04:25,578 --> 00:04:28,440 And inside of this object right here, we're going to 97 00:04:28,440 --> 00:04:31,980 pass the properties that this model is going to have. 98 00:04:31,980 --> 00:04:33,480 So as we discussed previously 99 00:04:33,480 --> 00:04:36,180 this model is going to have an email 100 00:04:36,180 --> 00:04:37,730 and it's gonna have a password. 101 00:04:39,270 --> 00:04:40,680 Now the other thing that's really relevant 102 00:04:40,680 --> 00:04:44,370 for these fields is the type of data that they are 103 00:04:44,370 --> 00:04:47,550 you know, is it a string, is it a number, is it an object? 104 00:04:47,550 --> 00:04:49,560 What is it exactly? 105 00:04:49,560 --> 00:04:52,890 In both cases, both the email and the password are going to 106 00:04:52,890 --> 00:04:54,633 be of type string. 107 00:04:55,680 --> 00:05:00,680 So I'm gonna put email, string and password string. 108 00:05:02,580 --> 00:05:05,010 When I say string right here, this is literally a reference 109 00:05:05,010 --> 00:05:06,353 to the JavaScript string. 110 00:05:06,353 --> 00:05:09,360 So this is not a variable that we have to import in 111 00:05:09,360 --> 00:05:11,400 or something like this or something like that. 112 00:05:11,400 --> 00:05:13,773 It is literally JavaScript string. 113 00:05:15,690 --> 00:05:19,470 Now I've defined two properties on our schema. 114 00:05:19,470 --> 00:05:21,960 One is email, one is password, and they're both 115 00:05:21,960 --> 00:05:23,160 of type string. 116 00:05:23,160 --> 00:05:25,470 But I wanna like, I wanna, before we go on, I wanna think 117 00:05:25,470 --> 00:05:26,303 about this for a little bit 118 00:05:26,303 --> 00:05:28,380 right? I wanna think about our application 119 00:05:30,090 --> 00:05:32,667 In just about, I'm not gonna say just about 120 00:05:32,667 --> 00:05:35,187 I'm gonna say literally every application I've ever used 121 00:05:35,187 --> 00:05:37,860 whatever is used as kind of like the username 122 00:05:37,860 --> 00:05:41,490 or the identifying piece of information of like, you know 123 00:05:41,490 --> 00:05:42,323 who am I? 124 00:05:42,323 --> 00:05:45,900 Basically this email property or that identifying piece 125 00:05:45,900 --> 00:05:48,360 of information always has to be unique. 126 00:05:48,360 --> 00:05:51,450 So in other words, if I create one username 127 00:05:51,450 --> 00:05:56,190 or one user account with an email of steven@gmail.com 128 00:05:56,190 --> 00:05:59,430 I can't log out and then create immediately another email 129 00:05:59,430 --> 00:06:03,889 another account using email, steven at email at gmail.com. 130 00:06:03,889 --> 00:06:08,889 In other words, we want to enforce uniqueness on this email. 131 00:06:09,150 --> 00:06:12,289 Whenever a user is created with a particular email 132 00:06:12,289 --> 00:06:16,050 no other E, no other user should ever be able to 133 00:06:16,050 --> 00:06:19,410 use that email again, basically for any account 134 00:06:19,410 --> 00:06:23,718 one email cannot be reused again inside of our application. 135 00:06:23,718 --> 00:06:27,480 So to enforce this uniqueness, we're going to pass 136 00:06:27,480 --> 00:06:31,980 an additional property to this email field right here. 137 00:06:31,980 --> 00:06:34,140 Now, to pass an additional property, instead of 138 00:06:34,140 --> 00:06:37,440 passing just string as this value, we're going 139 00:06:37,440 --> 00:06:39,573 to assign it an object instead. 140 00:06:40,710 --> 00:06:43,380 So I'm gonna wrap this in an object and I'm gonna 141 00:06:43,380 --> 00:06:46,230 say that it is of type string. 142 00:06:46,230 --> 00:06:47,790 So at this point, nothing has changed. 143 00:06:47,790 --> 00:06:50,280 We're still passing some configuration for the 144 00:06:50,280 --> 00:06:53,403 email field and it is of type string. 145 00:06:55,304 --> 00:06:58,980 To make sure that this field is always gonna be unique. 146 00:06:58,980 --> 00:07:02,320 We just pass on an additional property of unique 147 00:07:03,960 --> 00:07:05,490 true. 148 00:07:05,490 --> 00:07:09,210 So now whenever an email or a user model is created 149 00:07:09,210 --> 00:07:12,998 and someone tries to save it, it is going to tell MongoDB 150 00:07:12,998 --> 00:07:14,850 Hey, here's a user model. 151 00:07:14,850 --> 00:07:16,140 They have an email. 152 00:07:16,140 --> 00:07:18,870 Before you save this model, before you save it 153 00:07:18,870 --> 00:07:22,230 to the database, make sure that the email is unique. 154 00:07:22,230 --> 00:07:25,260 Make sure that there are no other users already created 155 00:07:25,260 --> 00:07:26,820 with this same email. 156 00:07:26,820 --> 00:07:29,940 If there are throw and air, that's what this unique check 157 00:07:29,940 --> 00:07:31,143 right here does. 158 00:07:32,122 --> 00:07:34,650 Now, one interesting little side effect of this is 159 00:07:34,650 --> 00:07:39,390 that MongoDB does not enforce case in strings when 160 00:07:39,390 --> 00:07:40,920 it's doing this unique check. 161 00:07:40,920 --> 00:07:43,530 So in other words, if someone tries to register with 162 00:07:43,530 --> 00:07:46,110 like steven@gmail.com 163 00:07:46,110 --> 00:07:48,190 and then another person tried to use 164 00:07:49,950 --> 00:07:54,000 Stephen@gmail.com with all capitals 165 00:07:54,000 --> 00:07:57,600 Mongo would think that these are two unique emails. 166 00:07:57,600 --> 00:07:59,910 So to make sure that these always get saved 167 00:07:59,910 --> 00:08:03,240 with lowercase characters to avoid this kind 168 00:08:03,240 --> 00:08:04,997 of uniqueness complication right here 169 00:08:04,997 --> 00:08:07,253 we can add another requirement to it 170 00:08:07,253 --> 00:08:11,640 to this option that just says lowercase true. 171 00:08:11,640 --> 00:08:14,940 And that means whenever any string is saved to the database 172 00:08:14,940 --> 00:08:17,965 it will first be turned into lowercase 173 00:08:17,965 --> 00:08:22,110 in which case this would then fail the uniqueness check. 174 00:08:22,110 --> 00:08:23,760 Okay. 175 00:08:23,760 --> 00:08:26,360 So this is looking good again, we created a user model. 176 00:08:26,360 --> 00:08:29,220 It has two fields, email and password. 177 00:08:29,220 --> 00:08:31,080 They're both of type string 178 00:08:31,080 --> 00:08:34,784 and the email and forces some uniqueness on it. 179 00:08:34,784 --> 00:08:37,590 Let's go ahead and save this, and we're almost done here. 180 00:08:37,590 --> 00:08:38,970 So let's just go another minute 181 00:08:38,970 --> 00:08:39,803 or two. 182 00:08:41,220 --> 00:08:42,845 To create the model class. 183 00:08:42,845 --> 00:08:45,390 We need to actually make use of Mongoose. 184 00:08:45,390 --> 00:08:47,130 So, so far we've just created a schema 185 00:08:47,130 --> 00:08:49,565 and we assigned it to a local variable. 186 00:08:49,565 --> 00:08:52,230 Now to actually create the model class 187 00:08:52,230 --> 00:08:53,910 which is what we're going to use to actually 188 00:08:53,910 --> 00:08:58,713 create new users, we'll make a new variable model. 189 00:09:00,900 --> 00:09:05,900 And then we'll say Mongoose model user 190 00:09:06,300 --> 00:09:07,473 and user schema. 191 00:09:08,610 --> 00:09:09,443 Like so. 192 00:09:12,024 --> 00:09:14,550 This basically loads the schema into mongoose 193 00:09:14,550 --> 00:09:17,130 and it tells mongoose, Hey, there's a new schema here, it's 194 00:09:17,130 --> 00:09:22,130 about a user and it corresponds to a collection named user. 195 00:09:25,110 --> 00:09:27,660 So we add the model as schema 196 00:09:27,660 --> 00:09:30,900 to mongoose and we get back our model right here. 197 00:09:30,900 --> 00:09:31,733 Now I'm gonna, you know 198 00:09:31,733 --> 00:09:33,420 how about let's rename this variable right here. 199 00:09:33,420 --> 00:09:36,240 I'm gonna rename it to model class 200 00:09:36,240 --> 00:09:38,280 just to really indicate what it is. 201 00:09:38,280 --> 00:09:41,340 This is a model class, which means it represents kind of 202 00:09:41,340 --> 00:09:44,280 like all users, not a particular user. 203 00:09:44,280 --> 00:09:46,992 It is a class of users. 204 00:09:46,992 --> 00:09:50,070 Now the last thing we have to do is export the model. 205 00:09:50,070 --> 00:09:51,990 And this part is a little bit more straightforward. 206 00:09:51,990 --> 00:09:53,400 Again, in Node. 207 00:09:53,400 --> 00:09:56,532 We are not using the export keyword here yet 208 00:09:56,532 --> 00:09:58,500 cause we don't have support for it. 209 00:09:58,500 --> 00:10:01,720 So instead we'll just use module dot exports 210 00:10:03,270 --> 00:10:05,490 model class. 211 00:10:05,490 --> 00:10:06,930 Like so. 212 00:10:06,930 --> 00:10:08,550 Cool. So this looks great right here. 213 00:10:08,550 --> 00:10:10,732 We've defined our user schema. 214 00:10:10,732 --> 00:10:13,350 We created a model class out of it 215 00:10:13,350 --> 00:10:16,830 and then we exported it for use in other files. 216 00:10:16,830 --> 00:10:17,663 This looks good. 217 00:10:17,663 --> 00:10:18,750 Let's give a shot 218 00:10:18,750 --> 00:10:21,243 at making use of it inside the next section.