All language subtitles for [English (auto-generated)] Express JS Full Course [DownSub.com]

af Afrikaans
sq Albanian
am Amharic
ar Arabic Download
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:00,760 --> 00:00:03,399 hello everyone my name is Anon and in 2 00:00:03,399 --> 00:00:05,440 this tutorial I will teach you how to 3 00:00:05,440 --> 00:00:09,000 build a web API using the expressjs 4 00:00:09,000 --> 00:00:12,080 framework Express is the most popular 5 00:00:12,080 --> 00:00:14,360 server sided web framework in the 6 00:00:14,360 --> 00:00:15,279 node.js 7 00:00:15,279 --> 00:00:18,439 ecosystem it is widely used by many 8 00:00:18,439 --> 00:00:20,400 developers it's in over 20 million 9 00:00:20,400 --> 00:00:23,000 projects according to GitHub it has over 10 00:00:23,000 --> 00:00:25,400 27 million weekly downloads according to 11 00:00:25,400 --> 00:00:28,840 npm and it is used by a lot of companies 12 00:00:28,840 --> 00:00:31,840 ranging from startups all the way to 13 00:00:31,840 --> 00:00:35,120 Fortune 500 companies but why is it so 14 00:00:35,120 --> 00:00:37,480 popular though well mainly because 15 00:00:37,480 --> 00:00:40,399 expressjs is very easy to learn it makes 16 00:00:40,399 --> 00:00:43,719 it very easy for you to set up an API in 17 00:00:43,719 --> 00:00:45,840 less than 30 18 00:00:45,840 --> 00:00:48,760 seconds it's unated which means there's 19 00:00:48,760 --> 00:00:50,960 not much if not there's really not any 20 00:00:50,960 --> 00:00:52,960 overhead at all you don't have to worry 21 00:00:52,960 --> 00:00:54,800 about configuring a bunch of different 22 00:00:54,800 --> 00:00:57,359 properties in your application before 23 00:00:57,359 --> 00:00:59,280 you can actually use it all you have to 24 00:00:59,280 --> 00:01:01,519 do is just in install the package 25 00:01:01,519 --> 00:01:03,640 instantiate the express app and then 26 00:01:03,640 --> 00:01:05,640 listen to a port and then begin 27 00:01:05,640 --> 00:01:08,320 listening to requests that's it there's 28 00:01:08,320 --> 00:01:10,240 no right or wrong answer when it comes 29 00:01:10,240 --> 00:01:13,840 to building web apis using a framework 30 00:01:13,840 --> 00:01:15,880 that is unopinionated like 31 00:01:15,880 --> 00:01:18,320 expressjs and because of that that is 32 00:01:18,320 --> 00:01:21,680 why till this day over the past perhaps 33 00:01:21,680 --> 00:01:23,960 10 years now Express is still the 34 00:01:23,960 --> 00:01:25,560 dominant framework that many people 35 00:01:25,560 --> 00:01:27,560 choose to use whenever they want to 36 00:01:27,560 --> 00:01:29,119 build their next project now for those 37 00:01:29,119 --> 00:01:30,720 of you who are not really familiar with 38 00:01:30,720 --> 00:01:32,840 how web apis interact with other 39 00:01:32,840 --> 00:01:34,399 applications I have a simple diagram 40 00:01:34,399 --> 00:01:35,799 over here that we're going to go over 41 00:01:35,799 --> 00:01:38,479 right now expressjs remember is a serers 42 00:01:38,479 --> 00:01:40,439 sided application so pretend that right 43 00:01:40,439 --> 00:01:42,040 over here on the right hand side this 44 00:01:42,040 --> 00:01:44,159 server is our is where our express 45 00:01:44,159 --> 00:01:45,960 application is going to live and then we 46 00:01:45,960 --> 00:01:48,520 have our clients so these are typically 47 00:01:48,520 --> 00:01:50,439 just regular users that will use your 48 00:01:50,439 --> 00:01:52,920 application either on a mobile device 49 00:01:52,920 --> 00:01:55,159 such as a phone or a tablet or they'll 50 00:01:55,159 --> 00:01:57,759 use it on a computer either via the web 51 00:01:57,759 --> 00:01:59,600 browser or a desktop client now let's 52 00:01:59,600 --> 00:02:01,719 say for example we have an e-commerce 53 00:02:01,719 --> 00:02:03,560 website and that e-commerce website when 54 00:02:03,560 --> 00:02:05,600 you visit it it displays a list of 55 00:02:05,600 --> 00:02:08,199 products those products needs to come 56 00:02:08,199 --> 00:02:09,840 from somewhere they don't just randomly 57 00:02:09,840 --> 00:02:11,280 appear when you click on those products 58 00:02:11,280 --> 00:02:12,800 you can see more information about it 59 00:02:12,800 --> 00:02:14,120 but where does that information come 60 00:02:14,120 --> 00:02:15,879 from where does all the data come from 61 00:02:15,879 --> 00:02:18,560 well most of the time it comes from a 62 00:02:18,560 --> 00:02:21,360 server comes from a web API but the 63 00:02:21,360 --> 00:02:22,760 client doesn't just automatically 64 00:02:22,760 --> 00:02:24,440 receive that data from the server the 65 00:02:24,440 --> 00:02:27,080 client needs to make what is called an 66 00:02:27,080 --> 00:02:30,599 HTTP request HTTP stands for hyper text 67 00:02:30,599 --> 00:02:32,360 transfer protocol it pretty much just 68 00:02:32,360 --> 00:02:35,040 means hey this is how I want to exchange 69 00:02:35,040 --> 00:02:36,800 data with you okay there are many 70 00:02:36,800 --> 00:02:38,959 different types of protocols but HTTP is 71 00:02:38,959 --> 00:02:40,640 one of the most popular ones out there 72 00:02:40,640 --> 00:02:42,319 so let's go back to our example of the 73 00:02:42,319 --> 00:02:44,159 e-commerce app the moment that you visit 74 00:02:44,159 --> 00:02:46,120 the homepage of the e-commerce app what 75 00:02:46,120 --> 00:02:48,959 happens is the code on the client side 76 00:02:48,959 --> 00:02:52,720 will make an HTTP request to the server 77 00:02:52,720 --> 00:02:54,599 side application okay in our case it 78 00:02:54,599 --> 00:02:57,319 will be the express API the express API 79 00:02:57,319 --> 00:02:58,840 will receive that request and it will 80 00:02:58,840 --> 00:03:00,080 say okay I just received receive the 81 00:03:00,080 --> 00:03:03,120 request to send back a list of products 82 00:03:03,120 --> 00:03:05,040 for the client I don't care how the 83 00:03:05,040 --> 00:03:06,400 client uses it I don't know what they're 84 00:03:06,400 --> 00:03:07,640 going to do with it but my 85 00:03:07,640 --> 00:03:09,760 responsibility is just to get that list 86 00:03:09,760 --> 00:03:11,239 of products and send it back to the 87 00:03:11,239 --> 00:03:12,879 client so what the server will do is 88 00:03:12,879 --> 00:03:15,319 it'll perform some business logic and 89 00:03:15,319 --> 00:03:17,599 then it will produce an output okay in 90 00:03:17,599 --> 00:03:19,879 this case the output is retrieving the 91 00:03:19,879 --> 00:03:22,599 list of products and the client itself 92 00:03:22,599 --> 00:03:24,400 never actually sees this operation going 93 00:03:24,400 --> 00:03:25,920 on so think of it like this let's say 94 00:03:25,920 --> 00:03:27,680 you're at a restaurant and you sit down 95 00:03:27,680 --> 00:03:29,480 you're at a table the waiter comes to 96 00:03:29,480 --> 00:03:31,159 you the waiter in this case is the 97 00:03:31,159 --> 00:03:33,159 server the waiter asks you what you want 98 00:03:33,159 --> 00:03:35,480 you request the waiter hey this is what 99 00:03:35,480 --> 00:03:37,879 I want to eat the waiter will send that 100 00:03:37,879 --> 00:03:40,000 request back to the kitchen back to 101 00:03:40,000 --> 00:03:41,239 where all the chefs are working so you 102 00:03:41,239 --> 00:03:43,239 can think of the kitchen like the server 103 00:03:43,239 --> 00:03:45,239 you never actually see what's going on 104 00:03:45,239 --> 00:03:47,000 in the kitchen all you know is that 105 00:03:47,000 --> 00:03:48,799 after a certain amount of time the 106 00:03:48,799 --> 00:03:51,280 waiter will come back with a response in 107 00:03:51,280 --> 00:03:52,519 this case that response is going to be 108 00:03:52,519 --> 00:03:55,480 your food in our case for our e-commerce 109 00:03:55,480 --> 00:03:57,400 application the response that we are 110 00:03:57,400 --> 00:03:59,720 getting from the server is going to be a 111 00:03:59,720 --> 00:04:02,480 list of products hopefully that makes 112 00:04:02,480 --> 00:04:04,920 sense and now we are finally ready to 113 00:04:04,920 --> 00:04:07,760 dive into setting up our expressjs 114 00:04:07,760 --> 00:04:10,680 project and writing some code so let's 115 00:04:10,680 --> 00:04:13,040 go ahead and get started so right inside 116 00:04:13,040 --> 00:04:14,400 my windows Powershell I'm going to go 117 00:04:14,400 --> 00:04:16,918 ahead and create a new directory and I'm 118 00:04:16,918 --> 00:04:18,320 going to call this 119 00:04:18,320 --> 00:04:21,120 expressjs 120 00:04:21,120 --> 00:04:23,199 tutorial I'm going to go ahead and CD 121 00:04:23,199 --> 00:04:25,680 into that 122 00:04:25,680 --> 00:04:28,039 directory and let me just clear up my 123 00:04:28,039 --> 00:04:29,800 console I'm currently using using 124 00:04:29,800 --> 00:04:31,919 node.js version 125 00:04:31,919 --> 00:04:34,720 2.4.0 as of right now the time of 126 00:04:34,720 --> 00:04:36,400 recording this video however there 127 00:04:36,400 --> 00:04:38,880 hasn't really been any major breaking 128 00:04:38,880 --> 00:04:41,400 changes with expressjs between different 129 00:04:41,400 --> 00:04:43,479 node.js versions so even if you're using 130 00:04:43,479 --> 00:04:45,639 an earlier version or a later version 131 00:04:45,639 --> 00:04:47,199 you really won't run into any issues at 132 00:04:47,199 --> 00:04:49,039 all so don't worry about that so let's 133 00:04:49,039 --> 00:04:52,479 go ahead and type npm init hyphen y to 134 00:04:52,479 --> 00:04:55,080 initialize this folder as an npm 135 00:04:55,080 --> 00:04:56,880 repository and this will give us a 136 00:04:56,880 --> 00:04:58,919 package.json file that's generated for 137 00:04:58,919 --> 00:05:01,000 us and and let's go ahead and open up 138 00:05:01,000 --> 00:05:02,840 visual studio code or whatever text 139 00:05:02,840 --> 00:05:05,800 editor you prefer to use and let's just 140 00:05:05,800 --> 00:05:08,320 take a look at this package Json file 141 00:05:08,320 --> 00:05:11,120 and there's nothing in here uh for the 142 00:05:11,120 --> 00:05:13,919 dependencies just yet we need to install 143 00:05:13,919 --> 00:05:17,680 it so let's go ahead and install Express 144 00:05:17,680 --> 00:05:22,199 so I'm going to type npm I or install I 145 00:05:22,199 --> 00:05:24,639 is short for install and then 146 00:05:24,639 --> 00:05:28,199 Express and just hit enter and now this 147 00:05:28,199 --> 00:05:31,160 will install Express for you and that's 148 00:05:31,160 --> 00:05:32,880 it that's the only package that we need 149 00:05:32,880 --> 00:05:35,479 to install it's that 150 00:05:35,479 --> 00:05:38,199 simple let's go ahead 151 00:05:38,199 --> 00:05:41,280 and install actually one more tool for 152 00:05:41,280 --> 00:05:45,520 development I'm going to install 153 00:05:45,520 --> 00:05:48,199 nodemon and what nodemon allows you to 154 00:05:48,199 --> 00:05:51,639 do is run your application in watch mode 155 00:05:51,639 --> 00:05:54,680 so as you're saving changes to your 156 00:05:54,680 --> 00:05:57,360 source code the process will 157 00:05:57,360 --> 00:05:59,759 automatically restart based on file 158 00:05:59,759 --> 00:06:01,919 changes so you don't have to manually 159 00:06:01,919 --> 00:06:03,840 exit out of the process and restart it 160 00:06:03,840 --> 00:06:06,400 again so I'm going to install nodon as a 161 00:06:06,400 --> 00:06:08,319 Dev dependency so I'm going to use the 162 00:06:08,319 --> 00:06:10,120 hyphen D flag as you can see right down 163 00:06:10,120 --> 00:06:12,360 over here let me zoom in a little bit 164 00:06:12,360 --> 00:06:15,240 more and I'll type 165 00:06:15,240 --> 00:06:18,479 nodemon okay and what I'm going to do is 166 00:06:18,479 --> 00:06:21,120 I'm going to set up uh a script so right 167 00:06:21,120 --> 00:06:24,199 inside the scripts object I'm going to 168 00:06:24,199 --> 00:06:26,840 set up a start colon Dev script and this 169 00:06:26,840 --> 00:06:30,400 will use nodemon to run our our main 170 00:06:30,400 --> 00:06:33,479 Javascript file so that file doesn't 171 00:06:33,479 --> 00:06:35,160 exist yet we need to create it but I'm 172 00:06:35,160 --> 00:06:37,199 going to go ahead and create a folder 173 00:06:37,199 --> 00:06:39,440 called Source in just a bit and the main 174 00:06:39,440 --> 00:06:41,720 file will be called index.js so this 175 00:06:41,720 --> 00:06:43,560 will be the entry point to our 176 00:06:43,560 --> 00:06:45,560 application I'll create one more script 177 00:06:45,560 --> 00:06:47,080 called start and this will just be a 178 00:06:47,080 --> 00:06:49,800 simple script to just use a regular node 179 00:06:49,800 --> 00:06:51,800 command to run our application so not in 180 00:06:51,800 --> 00:06:54,120 watch mode so this will typically be for 181 00:06:54,120 --> 00:06:55,919 production when you're ready to deploy 182 00:06:55,919 --> 00:06:58,039 the API now there's one more thing that 183 00:06:58,039 --> 00:07:00,039 I want to do inside this p package.json 184 00:07:00,039 --> 00:07:02,280 file I'm going to go ahead and set this 185 00:07:02,280 --> 00:07:04,919 type property and you can see that 186 00:07:04,919 --> 00:07:07,879 there's two values uh commonjs or module 187 00:07:07,879 --> 00:07:09,360 I'm going to set it to module and what 188 00:07:09,360 --> 00:07:12,039 this will allow me to do is use esm as 189 00:07:12,039 --> 00:07:14,840 my module system so that way I can use 190 00:07:14,840 --> 00:07:17,120 the modern import export statements 191 00:07:17,120 --> 00:07:18,919 instead of having to use require to 192 00:07:18,919 --> 00:07:21,120 import modules and module that exports 193 00:07:21,120 --> 00:07:23,400 to export stuff because I'm using uh esm 194 00:07:23,400 --> 00:07:25,080 modules I need to actually change the 195 00:07:25,080 --> 00:07:28,080 file extension to MJS in order for this 196 00:07:28,080 --> 00:07:31,120 to work so let's go ahead and do that 197 00:07:31,120 --> 00:07:32,520 and don't worry everything will still 198 00:07:32,520 --> 00:07:34,759 work fine it really doesn't make much of 199 00:07:34,759 --> 00:07:36,840 a difference except for you have the 200 00:07:36,840 --> 00:07:39,599 latest um modern versions of importing 201 00:07:39,599 --> 00:07:41,120 and exporting modules that's really all 202 00:07:41,120 --> 00:07:43,680 it is for let's go ahead and continue 203 00:07:43,680 --> 00:07:45,360 I'm going to go ahead and create a new 204 00:07:45,360 --> 00:07:49,000 folder called Source SRC and I'll create 205 00:07:49,000 --> 00:07:52,360 a new file called index. 206 00:07:52,360 --> 00:07:54,440 MJS and now what we're going to do is 207 00:07:54,440 --> 00:07:56,960 we're going to import 208 00:07:56,960 --> 00:08:00,840 Express from Express just like that so 209 00:08:00,840 --> 00:08:02,199 I'm basically importing the entire 210 00:08:02,199 --> 00:08:05,400 Express module from this Express package 211 00:08:05,400 --> 00:08:08,759 now the imported value of this Express 212 00:08:08,759 --> 00:08:11,400 name is actually a top level function 213 00:08:11,400 --> 00:08:13,039 and we need to call this function in 214 00:08:13,039 --> 00:08:15,560 order to create an express application 215 00:08:15,560 --> 00:08:16,840 so what I'm going to do is I'm going to 216 00:08:16,840 --> 00:08:18,479 first declare a variable let me zoom in 217 00:08:18,479 --> 00:08:21,120 a little bit more I'll call it app and 218 00:08:21,120 --> 00:08:22,520 then I'm just simply going to reference 219 00:08:22,520 --> 00:08:25,319 Express and then invoke that function by 220 00:08:25,319 --> 00:08:27,199 using parentheses and that's all and 221 00:08:27,199 --> 00:08:28,639 that's all you have to do now that we 222 00:08:28,639 --> 00:08:31,159 have our Express app I can reference the 223 00:08:31,159 --> 00:08:33,679 app variable and whenever I use the dot 224 00:08:33,679 --> 00:08:35,279 operator you can see that there are a 225 00:08:35,279 --> 00:08:36,719 bunch of different methods and 226 00:08:36,719 --> 00:08:38,519 properties that I can reference now it 227 00:08:38,519 --> 00:08:39,679 might be a little bit overwhelming at 228 00:08:39,679 --> 00:08:41,479 first but don't worry the method that we 229 00:08:41,479 --> 00:08:44,600 need to call is the listen method and 230 00:08:44,600 --> 00:08:47,560 this pretty much allows you to listen to 231 00:08:47,560 --> 00:08:50,120 a port for incoming requests this is 232 00:08:50,120 --> 00:08:51,760 actually what starts up the express 233 00:08:51,760 --> 00:08:54,320 server on a specific port and then you 234 00:08:54,320 --> 00:08:57,240 can begin receiving incoming HTTP 235 00:08:57,240 --> 00:08:59,040 requests so let's go ahead and set a 236 00:08:59,040 --> 00:09:00,720 port Port uh you can pass in really any 237 00:09:00,720 --> 00:09:02,040 port you want I'm going to go ahe and 238 00:09:02,040 --> 00:09:04,120 pass in Port 3000 but instead of just 239 00:09:04,120 --> 00:09:06,320 passing in a hardcoded number for the 240 00:09:06,320 --> 00:09:09,079 port for best practice it's best to 241 00:09:09,079 --> 00:09:11,600 assign your port to a variable called 242 00:09:11,600 --> 00:09:13,920 port in this case and then you can 243 00:09:13,920 --> 00:09:16,320 reference process which is a global in 244 00:09:16,320 --> 00:09:18,600 node.js and then process has an object 245 00:09:18,600 --> 00:09:20,839 called EnV and from here you can access 246 00:09:20,839 --> 00:09:23,920 your environment variables so we would 247 00:09:23,920 --> 00:09:25,120 assume that there is going to be an 248 00:09:25,120 --> 00:09:27,560 environment variable for ports but if 249 00:09:27,560 --> 00:09:29,640 the environment variable for Port is 250 00:09:29,640 --> 00:09:32,839 undefined then we can have it assign 251 00:09:32,839 --> 00:09:35,800 this leftand value with our logical or 252 00:09:35,800 --> 00:09:37,640 operator right over here let's go ahead 253 00:09:37,640 --> 00:09:40,680 and reference app now and call. listen 254 00:09:40,680 --> 00:09:42,079 and pass in Port and then I'm going to 255 00:09:42,079 --> 00:09:44,000 go ahead and pass in a callback function 256 00:09:44,000 --> 00:09:45,959 so you can use this to perform 257 00:09:45,959 --> 00:09:47,760 post-processing operations once your 258 00:09:47,760 --> 00:09:49,959 server has uh started up so maybe if you 259 00:09:49,959 --> 00:09:51,839 want to send an event to some 260 00:09:51,839 --> 00:09:54,440 centralized logging system so that way 261 00:09:54,440 --> 00:09:56,120 they know that the server was just 262 00:09:56,120 --> 00:09:58,120 started up at this time you can do that 263 00:09:58,120 --> 00:10:01,120 inside this callback function 264 00:10:01,360 --> 00:10:03,720 however I'll simply write a console log 265 00:10:03,720 --> 00:10:05,920 and I'll use string interpolation um and 266 00:10:05,920 --> 00:10:08,120 then I'll go ahead and write running on 267 00:10:08,120 --> 00:10:10,440 Port and then I'll log the port okay so 268 00:10:10,440 --> 00:10:11,959 let's go ahead and start up our 269 00:10:11,959 --> 00:10:13,760 application and make sure that it works 270 00:10:13,760 --> 00:10:15,600 so I'll go into the terminal now and I'm 271 00:10:15,600 --> 00:10:18,640 going to run that start Dev script so 272 00:10:18,640 --> 00:10:22,200 let's type npm Run start colon Dev and 273 00:10:22,200 --> 00:10:23,640 now you're going to see that nodemon 274 00:10:23,640 --> 00:10:25,640 will start up this application and you 275 00:10:25,640 --> 00:10:28,399 can see that now it says running on Port 276 00:10:28,399 --> 00:10:29,560 3000 277 00:10:29,560 --> 00:10:31,000 now if you want to test this out you can 278 00:10:31,000 --> 00:10:33,040 simply just go to your web browser and 279 00:10:33,040 --> 00:10:35,959 just type Local Host colon and then the 280 00:10:35,959 --> 00:10:37,680 port number that you are listening to 281 00:10:37,680 --> 00:10:39,680 requests on since I'm listening to 282 00:10:39,680 --> 00:10:41,800 requests on Port 3000 I would type Local 283 00:10:41,800 --> 00:10:44,920 Host colon Port 3000 right now you can 284 00:10:44,920 --> 00:10:47,200 see that it says cannot get and this is 285 00:10:47,200 --> 00:10:49,120 because we don't have anything 286 00:10:49,120 --> 00:10:51,399 registered just yet okay we need to 287 00:10:51,399 --> 00:10:53,399 actually register what's called a route 288 00:10:53,399 --> 00:10:55,959 in order for us to start requesting 289 00:10:55,959 --> 00:10:57,279 something from the server and then 290 00:10:57,279 --> 00:10:59,560 receiving a response so hope hopefully 291 00:10:59,560 --> 00:11:02,839 this all makes 292 00:11:05,040 --> 00:11:07,800 sense so now that you finally know how 293 00:11:07,800 --> 00:11:09,480 to set up a simple web server using 294 00:11:09,480 --> 00:11:11,360 expressjs I'm going to show you how we 295 00:11:11,360 --> 00:11:13,519 can Define routes and access those 296 00:11:13,519 --> 00:11:16,240 routes to receive different responses 297 00:11:16,240 --> 00:11:18,440 but first of all what exactly is a route 298 00:11:18,440 --> 00:11:20,600 well think of it like this currently we 299 00:11:20,600 --> 00:11:23,959 were trying to access Local Host Port 300 00:11:23,959 --> 00:11:27,440 3000 okay and this was the base route 301 00:11:27,440 --> 00:11:28,839 and we actually don't have anything set 302 00:11:28,839 --> 00:11:31,560 up up to be returned from the base route 303 00:11:31,560 --> 00:11:33,360 so whenever we try to access it that's 304 00:11:33,360 --> 00:11:35,399 the reason why it said cannot get slash 305 00:11:35,399 --> 00:11:37,639 because we didn't have any resolver to 306 00:11:37,639 --> 00:11:39,720 map a response back to that route but a 307 00:11:39,720 --> 00:11:42,000 route in general is think of it like a 308 00:11:42,000 --> 00:11:45,279 path in your express application so 309 00:11:45,279 --> 00:11:47,120 determining which path you want to take 310 00:11:47,120 --> 00:11:49,800 gives you different outputs so for 311 00:11:49,800 --> 00:11:52,279 example if you go 312 00:11:52,279 --> 00:11:56,040 to let's say the users route let's say 313 00:11:56,040 --> 00:11:58,560 if I have a users route defined on my 314 00:11:58,560 --> 00:11:59,639 server 315 00:11:59,639 --> 00:12:02,720 this will give me a list of users if I 316 00:12:02,720 --> 00:12:06,760 wanted to get a list of products I would 317 00:12:06,760 --> 00:12:09,079 access the products route and all you do 318 00:12:09,079 --> 00:12:11,480 is just you just add this forward slash 319 00:12:11,480 --> 00:12:12,839 and then the name of the route at the 320 00:12:12,839 --> 00:12:17,440 end of the host name and the port in 321 00:12:17,440 --> 00:12:19,519 actual real uh applications that are 322 00:12:19,519 --> 00:12:21,040 deployed you typically don't have the 323 00:12:21,040 --> 00:12:23,320 port uh exposed like this so it would 324 00:12:23,320 --> 00:12:25,000 just be after the host name like 325 00:12:25,000 --> 00:12:27,279 something like Local Host 326 00:12:27,279 --> 00:12:30,760 test.com products okay so you define 327 00:12:30,760 --> 00:12:33,160 these routes on your Express server and 328 00:12:33,160 --> 00:12:34,800 then you allow your clients to make 329 00:12:34,800 --> 00:12:37,199 requests to those routes now remember 330 00:12:37,199 --> 00:12:39,160 how in the introduction I mentioned in 331 00:12:39,160 --> 00:12:42,440 order to request data from the client to 332 00:12:42,440 --> 00:12:44,240 the backend server you need to make an 333 00:12:44,240 --> 00:12:46,959 HTTP request well there are actually 334 00:12:46,959 --> 00:12:50,040 different types of HTTP requests and 335 00:12:50,040 --> 00:12:53,079 these are known as HTTP verbs so these 336 00:12:53,079 --> 00:12:55,720 verbs pretty much are ways on how you 337 00:12:55,720 --> 00:12:58,240 can tell the server to perform some 338 00:12:58,240 --> 00:13:00,519 operation so for example you don't 339 00:13:00,519 --> 00:13:02,839 always want to just get data sometimes 340 00:13:02,839 --> 00:13:05,760 you might want to create data by saving 341 00:13:05,760 --> 00:13:07,440 it to the database once it's reach the 342 00:13:07,440 --> 00:13:09,959 server sometimes you want to update data 343 00:13:09,959 --> 00:13:11,360 sometimes you want to delete data there 344 00:13:11,360 --> 00:13:13,480 are different types of request methods 345 00:13:13,480 --> 00:13:16,000 that we use to handle these operations 346 00:13:16,000 --> 00:13:17,760 and you'll learn that later on but first 347 00:13:17,760 --> 00:13:19,480 let's go ahead and set up a simple get 348 00:13:19,480 --> 00:13:22,800 request so let's go ahead and reference 349 00:13:22,800 --> 00:13:25,000 the app and we're going to go ahead and 350 00:13:25,000 --> 00:13:27,600 call this get method right over 351 00:13:27,600 --> 00:13:29,800 here and and it's going to take in an 352 00:13:29,800 --> 00:13:31,800 argument which is going to be a string 353 00:13:31,800 --> 00:13:33,360 as the first argument and right over 354 00:13:33,360 --> 00:13:35,800 here is where you can specify what route 355 00:13:35,800 --> 00:13:38,639 you want to register in your Express app 356 00:13:38,639 --> 00:13:41,040 So currently I don't have any route 357 00:13:41,040 --> 00:13:43,800 handling the base 358 00:13:43,800 --> 00:13:46,639 SL route so I'm going to go ahead and 359 00:13:46,639 --> 00:13:49,000 configure a route for that so whenever 360 00:13:49,000 --> 00:13:51,160 the user visits this route They will 361 00:13:51,160 --> 00:13:54,360 receive a response but we actually need 362 00:13:54,360 --> 00:13:56,399 another piece in order for this whole 363 00:13:56,399 --> 00:13:59,600 thing to work we need what is called a 364 00:13:59,600 --> 00:14:02,560 request Handler and that is actually the 365 00:14:02,560 --> 00:14:06,079 second argument to 366 00:14:06,079 --> 00:14:10,680 app.get so the request Handler is just a 367 00:14:10,680 --> 00:14:12,360 function but in this case it's a 368 00:14:12,360 --> 00:14:14,720 callback function so it would look like 369 00:14:14,720 --> 00:14:17,279 this so I'll pass a simple arrow 370 00:14:17,279 --> 00:14:20,680 function and this callback function has 371 00:14:20,680 --> 00:14:24,880 two arguments okay it has a request 372 00:14:24,880 --> 00:14:27,759 argument which is the request object 373 00:14:27,759 --> 00:14:29,480 itself this contain contains everything 374 00:14:29,480 --> 00:14:32,880 related to the incoming HTTP request so 375 00:14:32,880 --> 00:14:35,519 for example if you passed in HTTP 376 00:14:35,519 --> 00:14:37,480 headers from the client side to the 377 00:14:37,480 --> 00:14:39,639 server side that would be inside the 378 00:14:39,639 --> 00:14:42,199 header property in the request object if 379 00:14:42,199 --> 00:14:44,040 you were to send data in the request 380 00:14:44,040 --> 00:14:46,480 body that would be accessed by grabbing 381 00:14:46,480 --> 00:14:49,440 it from the request body property if you 382 00:14:49,440 --> 00:14:51,759 wanted to access cookies if you wanted 383 00:14:51,759 --> 00:14:54,360 to access the IP address all of this 384 00:14:54,360 --> 00:14:58,160 stuff comes from the request object okay 385 00:14:58,160 --> 00:15:01,320 now the second argument is the response 386 00:15:01,320 --> 00:15:04,399 object the response object is what you 387 00:15:04,399 --> 00:15:07,720 can use to modify the response and send 388 00:15:07,720 --> 00:15:10,399 it back to the user so you can set the 389 00:15:10,399 --> 00:15:12,800 status code as an example you can send 390 00:15:12,800 --> 00:15:15,399 back uh data you can send back text you 391 00:15:15,399 --> 00:15:17,519 can send send back 392 00:15:17,519 --> 00:15:20,680 HTML you can send back text you can send 393 00:15:20,680 --> 00:15:23,120 back HTML you can send back a Json 394 00:15:23,120 --> 00:15:26,000 object whatever it is that you want so 395 00:15:26,000 --> 00:15:27,279 let's go ahead and reference the 396 00:15:27,279 --> 00:15:29,519 response object to to send back a 397 00:15:29,519 --> 00:15:32,399 response so I can reference response and 398 00:15:32,399 --> 00:15:34,279 call the send method and I'll just send 399 00:15:34,279 --> 00:15:36,079 back a simple hello world string just 400 00:15:36,079 --> 00:15:40,000 simple plain text so now if I visit the 401 00:15:40,000 --> 00:15:43,160 Local Host Port 3000 and if I go to just 402 00:15:43,160 --> 00:15:45,120 the base route you can see it says hello 403 00:15:45,120 --> 00:15:47,160 world okay pretty 404 00:15:47,160 --> 00:15:49,680 simple I can go ahead and 405 00:15:49,680 --> 00:15:54,480 also send back a Json object I'll say 406 00:15:54,480 --> 00:15:57,800 hello I refresh I now see it is parsed 407 00:15:57,800 --> 00:16:00,959 in this Json format right over here uh I 408 00:16:00,959 --> 00:16:03,480 can also set the status code as well so 409 00:16:03,480 --> 00:16:05,279 I can do that very easily by referencing 410 00:16:05,279 --> 00:16:08,680 response. status and this is a method so 411 00:16:08,680 --> 00:16:10,639 you can just pass in whatever status 412 00:16:10,639 --> 00:16:13,839 code you want I'll set it to just for a 413 00:16:13,839 --> 00:16:15,360 demonstration purposes I'll set it to 414 00:16:15,360 --> 00:16:17,959 2011 2011 is actually used for post 415 00:16:17,959 --> 00:16:19,720 requests whenever you create a resource 416 00:16:19,720 --> 00:16:21,639 but I just want to show you that this is 417 00:16:21,639 --> 00:16:23,199 what the status code is because by 418 00:16:23,199 --> 00:16:26,040 default the status code whenever it is 419 00:16:26,040 --> 00:16:29,279 successful is a 200 status code 420 00:16:29,279 --> 00:16:31,040 okay and after you set the status code 421 00:16:31,040 --> 00:16:32,560 you can actually chain these methods 422 00:16:32,560 --> 00:16:35,800 together so after I call status I can 423 00:16:35,800 --> 00:16:38,759 also just call do send and then just 424 00:16:38,759 --> 00:16:40,920 pass 425 00:16:41,000 --> 00:16:45,319 in a requ a response body so now if I 426 00:16:45,319 --> 00:16:48,600 refresh and let me open up 427 00:16:48,600 --> 00:16:51,560 the uh let's see the network tab right 428 00:16:51,560 --> 00:16:54,160 over here you can see that now the 429 00:16:54,160 --> 00:16:57,240 status code says 430 00:16:57,240 --> 00:16:59,519 2011 let's let's go ahead and Define a 431 00:16:59,519 --> 00:17:01,319 few more routes so that way you all get 432 00:17:01,319 --> 00:17:03,680 the hang of this so I'll go ahead and 433 00:17:03,680 --> 00:17:05,000 Define a 434 00:17:05,000 --> 00:17:09,119 route uh called slash users now whenever 435 00:17:09,119 --> 00:17:11,359 you are building apis you typically want 436 00:17:11,359 --> 00:17:15,079 to prefix all of your endpoints with a 437 00:17:15,079 --> 00:17:18,240 slash API prefix and this is industry 438 00:17:18,240 --> 00:17:20,640 standard a lot of companies that have 439 00:17:20,640 --> 00:17:23,679 apis do this it's just good practice so 440 00:17:23,679 --> 00:17:25,240 I would highly recommend you all to 441 00:17:25,240 --> 00:17:29,440 follow this approach okay so/ API SL 442 00:17:29,440 --> 00:17:31,919 users so this is now our route so 443 00:17:31,919 --> 00:17:34,919 whenever we access this in our browser 444 00:17:34,919 --> 00:17:37,520 we don't visit SL users we're visiting 445 00:17:37,520 --> 00:17:39,400 SL API 446 00:17:39,400 --> 00:17:43,160 users okay so as a second argument we 447 00:17:43,160 --> 00:17:45,799 need our request Handler of course so 448 00:17:45,799 --> 00:17:48,640 let's pass in the request and response 449 00:17:48,640 --> 00:17:51,480 object and what I'll do is I'll simply 450 00:17:51,480 --> 00:17:52,480 just send 451 00:17:52,480 --> 00:17:56,480 back an array of fake users so I'm just 452 00:17:56,480 --> 00:17:59,039 going to pass in an array in the 453 00:17:59,039 --> 00:18:01,799 send method as an argument and in my 454 00:18:01,799 --> 00:18:03,880 array I'll just provide some users so 455 00:18:03,880 --> 00:18:08,000 I'll set the ID to one username 456 00:18:08,000 --> 00:18:10,120 ansen display 457 00:18:10,120 --> 00:18:14,120 name Anson we'll keep it simple I will 458 00:18:14,120 --> 00:18:15,760 just copy and paste this a few more 459 00:18:15,760 --> 00:18:18,600 times and just change up the 460 00:18:18,600 --> 00:18:22,280 values so let's do 461 00:18:23,280 --> 00:18:26,320 Jack and then 462 00:18:26,320 --> 00:18:29,480 Adam now let's save okay and now 463 00:18:29,480 --> 00:18:32,360 whenever I go to the browser and let me 464 00:18:32,360 --> 00:18:33,919 just kind of like move this over to the 465 00:18:33,919 --> 00:18:37,720 to the side a bit when I go to slash API 466 00:18:37,720 --> 00:18:40,480 users this is the route or the endpoint 467 00:18:40,480 --> 00:18:41,919 I'm going to use those terms 468 00:18:41,919 --> 00:18:44,919 synonymously route and endpoint this is 469 00:18:44,919 --> 00:18:47,360 the route that I am going to be making a 470 00:18:47,360 --> 00:18:49,880 request to from the browser in this case 471 00:18:49,880 --> 00:18:52,039 the browser is our client okay we're 472 00:18:52,039 --> 00:18:55,760 making a request to SL API users and 473 00:18:55,760 --> 00:18:58,840 then when I hit enter you can see as a 474 00:18:58,840 --> 00:19:01,159 response this is what I get back I get 475 00:19:01,159 --> 00:19:05,120 back this array and this array has three 476 00:19:05,120 --> 00:19:07,320 users okay and if you were getting this 477 00:19:07,320 --> 00:19:10,240 data let's say on your react code you 478 00:19:10,240 --> 00:19:14,760 would render this out to the client so 479 00:19:14,760 --> 00:19:16,159 they can actually see all the 480 00:19:16,159 --> 00:19:18,960 users okay let's go ahead and create one 481 00:19:18,960 --> 00:19:24,640 more Let's do app.get let's do uh SL API 482 00:19:24,640 --> 00:19:27,240 SL uh 483 00:19:27,240 --> 00:19:30,919 products request and response so now you 484 00:19:30,919 --> 00:19:33,120 should get the hang of doing all of this 485 00:19:33,120 --> 00:19:34,799 and the whole reason why I'm showing you 486 00:19:34,799 --> 00:19:36,679 multiple examples is that way so you are 487 00:19:36,679 --> 00:19:39,600 familiar with this so once again we have 488 00:19:39,600 --> 00:19:42,559 our endpoint our route name defined 489 00:19:42,559 --> 00:19:45,600 right over here/ API products and then 490 00:19:45,600 --> 00:19:49,640 we also have our request Handler and I'm 491 00:19:49,640 --> 00:19:51,600 going to go ahead and send back a 492 00:19:51,600 --> 00:19:53,200 response now so I will reference the 493 00:19:53,200 --> 00:19:55,440 response object and I'm going to go 494 00:19:55,440 --> 00:19:58,559 ahead and call do send and then what 495 00:19:58,559 --> 00:20:01,200 I'll do is I'll just send back an OB uh 496 00:20:01,200 --> 00:20:03,320 a Json object which is this in this case 497 00:20:03,320 --> 00:20:05,360 is going to be an array and I'll do the 498 00:20:05,360 --> 00:20:09,440 same thing ID of let's do one two 3 499 00:20:09,440 --> 00:20:12,280 username or what am I doing not username 500 00:20:12,280 --> 00:20:15,120 uh let's do 501 00:20:15,120 --> 00:20:18,760 name let's do chicken 502 00:20:18,760 --> 00:20:23,440 breast and then price let's 503 00:20:23,440 --> 00:20:25,919 do 504 00:20:25,919 --> 00:20:28,200 $12.99 okay and now if I go to the 505 00:20:28,200 --> 00:20:31,840 browser and if I visit SL API product 506 00:20:31,840 --> 00:20:34,640 I'm making a request to this route it's 507 00:20:34,640 --> 00:20:37,960 going to give me that array of products 508 00:20:37,960 --> 00:20:41,400 and I can see this stuff right over 509 00:20:44,960 --> 00:20:47,360 here so now what I will do is I'll show 510 00:20:47,360 --> 00:20:50,280 you how we can use route parameters to 511 00:20:50,280 --> 00:20:53,440 be able to dynamically pass data to the 512 00:20:53,440 --> 00:20:56,480 server in the route and this can 513 00:20:56,480 --> 00:20:58,320 actually make it so that we we can 514 00:20:58,320 --> 00:21:00,480 receive Dynamic data based on whatever 515 00:21:00,480 --> 00:21:03,640 the value of that parameter is so I'll 516 00:21:03,640 --> 00:21:06,559 give you an example right now we are 517 00:21:06,559 --> 00:21:09,280 only able to receive all of the users 518 00:21:09,280 --> 00:21:11,600 let's just pretend that this users array 519 00:21:11,600 --> 00:21:14,679 comes from a database we are receiving 520 00:21:14,679 --> 00:21:17,960 all of the users in an array but what if 521 00:21:17,960 --> 00:21:20,640 I wanted to actually receive only one 522 00:21:20,640 --> 00:21:23,559 user based on some unique identifier 523 00:21:23,559 --> 00:21:25,919 such as the username or the ID how would 524 00:21:25,919 --> 00:21:27,600 I do that well this is where route 525 00:21:27,600 --> 00:21:29,679 parameters come into play you can use 526 00:21:29,679 --> 00:21:32,120 route parameters to pass in a dynamic 527 00:21:32,120 --> 00:21:34,880 value in the route path and then the 528 00:21:34,880 --> 00:21:36,720 server would receive that request it 529 00:21:36,720 --> 00:21:38,480 would check what the route parameter is 530 00:21:38,480 --> 00:21:39,720 and then since we know that we're going 531 00:21:39,720 --> 00:21:41,120 to be dealing with users because we 532 00:21:41,120 --> 00:21:44,200 would be visiting SL API users and then 533 00:21:44,200 --> 00:21:45,840 the route parameter would be placed 534 00:21:45,840 --> 00:21:47,840 after that we would go ahead and grab 535 00:21:47,840 --> 00:21:50,400 the correct user from our database in 536 00:21:50,400 --> 00:21:51,559 this case we're going to grab it from 537 00:21:51,559 --> 00:21:54,000 our array so the way that you define a 538 00:21:54,000 --> 00:21:56,400 route parameter is like this so right 539 00:21:56,400 --> 00:22:00,559 underneath uh my AP API SL users route 540 00:22:00,559 --> 00:22:03,279 I'll go ahead and set up another one but 541 00:22:03,279 --> 00:22:04,559 this time we will be using a route 542 00:22:04,559 --> 00:22:07,400 parameter so I'll call app.get and then 543 00:22:07,400 --> 00:22:11,240 SL API SL users and then slash and then 544 00:22:11,240 --> 00:22:13,799 here's where I want to Define my route 545 00:22:13,799 --> 00:22:15,960 parameter what I can do is I can use the 546 00:22:15,960 --> 00:22:19,440 Colin symbol and then give my route 547 00:22:19,440 --> 00:22:21,279 parameter a name I'm going to go ahead 548 00:22:21,279 --> 00:22:24,159 and give the name ID and then we're 549 00:22:24,159 --> 00:22:27,640 going to pass in a request Handler so 550 00:22:27,640 --> 00:22:29,880 the same that we've been doing so 551 00:22:29,880 --> 00:22:34,559 far and now whenever I visit SL API SL 552 00:22:34,559 --> 00:22:38,120 users and then slash and then the ID 553 00:22:38,120 --> 00:22:40,000 whatever I pass in it's going to go 554 00:22:40,000 --> 00:22:42,400 ahead and hit this endpoint so I have 555 00:22:42,400 --> 00:22:44,600 one endpoint or one route defined to 556 00:22:44,600 --> 00:22:46,840 give me all the users and then I have 557 00:22:46,840 --> 00:22:49,360 another route that has a route parameter 558 00:22:49,360 --> 00:22:51,600 that gives me a single user record based 559 00:22:51,600 --> 00:22:55,720 on the route parameter ID let's go ahead 560 00:22:55,720 --> 00:22:57,960 and do this I'm going to show you first 561 00:22:57,960 --> 00:23:00,640 first how I can grab that route 562 00:23:00,640 --> 00:23:03,240 parameter and we do that by referencing 563 00:23:03,240 --> 00:23:06,240 it from the request object so I can go 564 00:23:06,240 --> 00:23:08,840 ahead and console log this right now if 565 00:23:08,840 --> 00:23:10,000 I reference 566 00:23:10,000 --> 00:23:13,480 request. prams this is an object that 567 00:23:13,480 --> 00:23:16,679 gives you all of the route parameters 568 00:23:16,679 --> 00:23:18,520 because you can have multiple you don't 569 00:23:18,520 --> 00:23:20,520 need to only necessarily have one you 570 00:23:20,520 --> 00:23:22,880 might have more than one you might have 571 00:23:22,880 --> 00:23:26,960 an ID maybe you might have a username 572 00:23:26,960 --> 00:23:28,640 but typically in our in this situation 573 00:23:28,640 --> 00:23:31,240 we we really only need one so what I'll 574 00:23:31,240 --> 00:23:33,760 do is I'll console log this and I'll go 575 00:23:33,760 --> 00:23:35,960 back to my browser and I'll show you 576 00:23:35,960 --> 00:23:39,120 what happens when I visit SL API 577 00:23:39,120 --> 00:23:41,240 users and then 578 00:23:41,240 --> 00:23:43,480 slash1 and then let me show you the 579 00:23:43,480 --> 00:23:45,720 console you can see that right over here 580 00:23:45,720 --> 00:23:48,400 the console logged an object and that 581 00:23:48,400 --> 00:23:51,919 object contained that route parameter as 582 00:23:51,919 --> 00:23:55,440 a field ID and then it mapped to this 583 00:23:55,440 --> 00:23:58,240 value of one so hopefully that makes 584 00:23:58,240 --> 00:23:59,919 sense so I again like I said I can pass 585 00:23:59,919 --> 00:24:02,320 in literally any value I want I can pass 586 00:24:02,320 --> 00:24:04,760 in 500 and then whenever the server 587 00:24:04,760 --> 00:24:06,679 receives that request it will log uh 588 00:24:06,679 --> 00:24:10,600 this object ID as a field of 500 so what 589 00:24:10,600 --> 00:24:13,360 I'll do is I'm going to go ahead and 590 00:24:13,360 --> 00:24:17,200 grab the user from the array by its ID 591 00:24:17,200 --> 00:24:19,360 so let me just First Take This array and 592 00:24:19,360 --> 00:24:21,640 I'm going to move it up top over here so 593 00:24:21,640 --> 00:24:23,120 that way I can reference it all 594 00:24:23,120 --> 00:24:27,240 throughout my code const uh mock users 595 00:24:27,240 --> 00:24:29,720 and I'll sign it to this array and then 596 00:24:29,720 --> 00:24:32,120 let me just send back that mock users 597 00:24:32,120 --> 00:24:34,960 array and then what I'll do is a couple 598 00:24:34,960 --> 00:24:37,440 things one notice how if you looked at 599 00:24:37,440 --> 00:24:42,559 the logs the value of ID is actually a 600 00:24:42,559 --> 00:24:46,720 string but in our case our users have a 601 00:24:46,720 --> 00:24:50,440 numeric ID so we want to convert that 602 00:24:50,440 --> 00:24:54,520 into an actual number and this is kind 603 00:24:54,520 --> 00:24:56,279 of like a brief little intro to how you 604 00:24:56,279 --> 00:24:58,760 can perform valid ation for your 605 00:24:58,760 --> 00:25:02,039 incoming get requests okay so what I'll 606 00:25:02,039 --> 00:25:05,200 do is I will create a variable called 607 00:25:05,200 --> 00:25:09,880 par ID and I'm going to use the parse in 608 00:25:09,880 --> 00:25:11,760 method and I'm just simply going to pass 609 00:25:11,760 --> 00:25:15,880 in request. prrams do ID but here's the 610 00:25:15,880 --> 00:25:19,799 other problem though we don't know if uh 611 00:25:19,799 --> 00:25:23,080 the user even provided an ID at all well 612 00:25:23,080 --> 00:25:25,360 if they didn't then it would go to the 613 00:25:25,360 --> 00:25:28,120 API users route like for example if I 614 00:25:28,120 --> 00:25:31,279 didn't pass in an ID it would just go 615 00:25:31,279 --> 00:25:34,960 to/ API users so in because this is the 616 00:25:34,960 --> 00:25:38,120 case we don't really need to check if ID 617 00:25:38,120 --> 00:25:39,559 is defined because we know that it's 618 00:25:39,559 --> 00:25:40,880 going to be there but we do need to make 619 00:25:40,880 --> 00:25:45,039 sure that the value is valid in our case 620 00:25:45,039 --> 00:25:46,399 we want to make sure it's a valid 621 00:25:46,399 --> 00:25:50,760 numeric value so what I can do is I can 622 00:25:50,760 --> 00:25:55,520 parse this uh pam. ID value and if it is 623 00:25:55,520 --> 00:25:58,159 a valid numeric string then it'll 624 00:25:58,159 --> 00:26:00,440 convert it over to the actual integer 625 00:26:00,440 --> 00:26:03,440 itself if it's some regular non-numeric 626 00:26:03,440 --> 00:26:06,440 string then it will be not a number so 627 00:26:06,440 --> 00:26:10,080 if I were to conso log par 628 00:26:10,080 --> 00:26:13,440 ID and then if I go to the browser and 629 00:26:13,440 --> 00:26:14,600 if I 630 00:26:14,600 --> 00:26:17,919 refresh and I just pass in some invalid 631 00:26:17,919 --> 00:26:21,640 numeric or some non-numeric ID and the 632 00:26:21,640 --> 00:26:24,520 console it will log not a number Nan 633 00:26:24,520 --> 00:26:26,440 which stands for not a number for the 634 00:26:26,440 --> 00:26:28,120 par ID value 635 00:26:28,120 --> 00:26:31,600 so we can use uh an if condition and we 636 00:26:31,600 --> 00:26:34,679 can use this is not a number 637 00:26:34,679 --> 00:26:37,840 function and then I can just pass in par 638 00:26:37,840 --> 00:26:40,120 ID okay and in this case since we're 639 00:26:40,120 --> 00:26:44,159 passing in an invalid ID is Nan would 640 00:26:44,159 --> 00:26:47,520 return true so if it is not a number 641 00:26:47,520 --> 00:26:49,279 then what I want to do is I want to 642 00:26:49,279 --> 00:26:52,000 return perhaps a status code that 643 00:26:52,000 --> 00:26:53,799 indicates that this is an 644 00:26:53,799 --> 00:26:56,120 invalid response or this is an invalid 645 00:26:56,120 --> 00:26:59,240 request so what I'll I'll do is I will 646 00:26:59,240 --> 00:27:00,760 return 647 00:27:00,760 --> 00:27:02,600 response and I'm going to go ahead and 648 00:27:02,600 --> 00:27:05,840 call the status method and I'll pass in 649 00:27:05,840 --> 00:27:08,640 for the code the number 400 which means 650 00:27:08,640 --> 00:27:09,480 bad 651 00:27:09,480 --> 00:27:12,799 request and then I'll just simply call 652 00:27:12,799 --> 00:27:15,679 send and then maybe an error 653 00:27:15,679 --> 00:27:17,640 message bad 654 00:27:17,640 --> 00:27:21,880 request okay and I can even add 655 00:27:21,880 --> 00:27:25,799 additional additional notes invalid 656 00:27:25,799 --> 00:27:30,720 ID all right so now if it is in fact a 657 00:27:30,720 --> 00:27:33,320 valid number then we can perform some 658 00:27:33,320 --> 00:27:36,480 operations we can interact with our mock 659 00:27:36,480 --> 00:27:39,120 users array so I'm going to go ahead and 660 00:27:39,120 --> 00:27:41,799 now write the logic to find a user so 661 00:27:41,799 --> 00:27:45,880 const user or find user equals mock 662 00:27:45,880 --> 00:27:47,559 users and I'll just simply use the find 663 00:27:47,559 --> 00:27:49,840 method and I'll pass my predicate so 664 00:27:49,840 --> 00:27:51,919 we're going to search based on the 665 00:27:51,919 --> 00:27:54,799 ID so we're going to pass in this 666 00:27:54,799 --> 00:27:57,200 callback function or predicate function 667 00:27:57,200 --> 00:27:59,360 and we we're going to have access to the 668 00:27:59,360 --> 00:28:02,480 user object that is currently in the 669 00:28:02,480 --> 00:28:06,320 array and we'll check to see if user. ID 670 00:28:06,320 --> 00:28:10,360 matches so triple equals pars oh whoops 671 00:28:10,360 --> 00:28:12,640 did I oh you know I forgot this is 672 00:28:12,640 --> 00:28:14,360 actually par ID sorry about this it's 673 00:28:14,360 --> 00:28:18,720 pars ID not parse ID so pars 674 00:28:18,720 --> 00:28:24,240 ID okay and now uh if the user does 675 00:28:24,240 --> 00:28:26,600 exist well let's do if the user does not 676 00:28:26,600 --> 00:28:30,120 exist then I'll just 677 00:28:30,120 --> 00:28:32,559 return response so we're going to do the 678 00:28:32,559 --> 00:28:34,440 same thing that we did above on line 29 679 00:28:34,440 --> 00:28:36,840 right over here so this is the point 680 00:28:36,840 --> 00:28:39,240 where you have different paths that your 681 00:28:39,240 --> 00:28:40,720 controllers or that your request 682 00:28:40,720 --> 00:28:43,600 handlers can take so in our case we have 683 00:28:43,600 --> 00:28:45,840 three different outputs and later on 684 00:28:45,840 --> 00:28:47,320 around the end of this tutorial you'll 685 00:28:47,320 --> 00:28:50,320 learn how to unit test these these uh 686 00:28:50,320 --> 00:28:51,960 these functions but you can see right 687 00:28:51,960 --> 00:28:55,640 over here that currently I have three 688 00:28:55,640 --> 00:28:58,200 different outputs whether the ID that I 689 00:28:58,200 --> 00:29:00,919 passed was invalid so it sends back a 690 00:29:00,919 --> 00:29:03,440 400 or if the user is not found then we 691 00:29:03,440 --> 00:29:05,360 want to send back a 404 because that 692 00:29:05,360 --> 00:29:07,720 indicates not found so I'll return 693 00:29:07,720 --> 00:29:10,919 response. status and pass in 404 or you 694 00:29:10,919 --> 00:29:13,720 know there's also the uh I think there's 695 00:29:13,720 --> 00:29:16,519 a send status method so that way I can 696 00:29:16,519 --> 00:29:18,399 just send the status and I don't have to 697 00:29:18,399 --> 00:29:21,080 send I don't have to call do send 698 00:29:21,080 --> 00:29:24,559 again at the end um and then if the user 699 00:29:24,559 --> 00:29:27,679 is found then we'll just return response 700 00:29:27,679 --> 00:29:32,600 dot send find user just like this so we 701 00:29:32,600 --> 00:29:34,159 have three different possibilities for 702 00:29:34,159 --> 00:29:35,000 this 703 00:29:35,000 --> 00:29:37,640 endpoint okay so hopefully that makes 704 00:29:37,640 --> 00:29:40,840 sense now let's go ahead back into our 705 00:29:40,840 --> 00:29:42,640 app if I refresh you can see it says bad 706 00:29:42,640 --> 00:29:46,399 request invalid ID if I pass in let's 707 00:29:46,399 --> 00:29:48,760 say an ID of a user that does not exist 708 00:29:48,760 --> 00:29:50,720 it says not found and you can see on the 709 00:29:50,720 --> 00:29:54,000 console it gives us that 44 if I pass in 710 00:29:54,000 --> 00:29:56,720 an ID of one it finds the user and I can 711 00:29:56,720 --> 00:29:58,600 see it right over over here and same 712 00:29:58,600 --> 00:30:01,159 thing if I pass in two and three I get 713 00:30:01,159 --> 00:30:04,320 all of the users that are in that array 714 00:30:04,320 --> 00:30:06,640 so hopefully that shows you how to use 715 00:30:06,640 --> 00:30:08,960 route 716 00:30:12,799 --> 00:30:14,840 parameters so now I'm going to go ahead 717 00:30:14,840 --> 00:30:17,240 and talk about query strings and query 718 00:30:17,240 --> 00:30:19,760 parameters and how they are used in 719 00:30:19,760 --> 00:30:21,080 backend development and now we can 720 00:30:21,080 --> 00:30:23,200 actually use them ourselves so many of 721 00:30:23,200 --> 00:30:26,919 you may have seen something at the end 722 00:30:26,919 --> 00:30:31,039 of the website address so in the browser 723 00:30:31,039 --> 00:30:33,679 address URL you might see this question 724 00:30:33,679 --> 00:30:35,799 mark and then you might see something 725 00:30:35,799 --> 00:30:39,200 like key equals value and then you might 726 00:30:39,200 --> 00:30:41,320 see an ENT symbol and you might see 727 00:30:41,320 --> 00:30:45,159 another key equals value this is known 728 00:30:45,159 --> 00:30:47,840 as a query string so right over here 729 00:30:47,840 --> 00:30:50,480 this question mark symbol denotes that 730 00:30:50,480 --> 00:30:52,080 we have a query string and they go at 731 00:30:52,080 --> 00:30:55,720 the end of our uh defined route over 732 00:30:55,720 --> 00:30:58,840 here okay so you have the domain and 733 00:30:58,840 --> 00:31:01,760 then the route the path and then the 734 00:31:01,760 --> 00:31:04,120 query string at the end and then after 735 00:31:04,120 --> 00:31:05,760 the question mark you basically just 736 00:31:05,760 --> 00:31:08,240 pass in whatever key value pairs you 737 00:31:08,240 --> 00:31:11,360 want so for example I have uh a key 738 00:31:11,360 --> 00:31:15,039 called key and I use the equals operator 739 00:31:15,039 --> 00:31:17,159 to assign a value to it so it's kind of 740 00:31:17,159 --> 00:31:19,440 like assigning a value to a variable but 741 00:31:19,440 --> 00:31:21,399 only we're doing it in the address bar 742 00:31:21,399 --> 00:31:24,399 so key equals value and if I wanted more 743 00:31:24,399 --> 00:31:26,679 query parameters in the query string I 744 00:31:26,679 --> 00:31:28,919 can just simply use this ersan as a 745 00:31:28,919 --> 00:31:32,200 delim so ersan and then the next key 746 00:31:32,200 --> 00:31:35,320 value pair so key 2 equals value 2 I can 747 00:31:35,320 --> 00:31:38,000 have as many query parameters as I want 748 00:31:38,000 --> 00:31:39,240 now there are different ways that you 749 00:31:39,240 --> 00:31:41,320 can use Query parameters in web 750 00:31:41,320 --> 00:31:44,080 development you can send query 751 00:31:44,080 --> 00:31:46,120 parameters from a page to another page 752 00:31:46,120 --> 00:31:48,480 on the client side so that way you can 753 00:31:48,480 --> 00:31:51,919 send data uh across different pages so 754 00:31:51,919 --> 00:31:54,000 let's say if one page needs data from 755 00:31:54,000 --> 00:31:56,399 another page when you're navigating then 756 00:31:56,399 --> 00:31:58,519 you can grab the the values from the 757 00:31:58,519 --> 00:32:00,840 query query string if you're sending it 758 00:32:00,840 --> 00:32:02,840 from the client side to the serice side 759 00:32:02,840 --> 00:32:04,799 typically you would send a query string 760 00:32:04,799 --> 00:32:08,480 to uh add additional data to the request 761 00:32:08,480 --> 00:32:10,399 that you normally wouldn't add in a 762 00:32:10,399 --> 00:32:12,760 request body we haven't gotone to post 763 00:32:12,760 --> 00:32:15,200 requests just yet but I'll stick to a 764 00:32:15,200 --> 00:32:17,519 get request as an example so when you 765 00:32:17,519 --> 00:32:19,559 make a get request remember that you are 766 00:32:19,559 --> 00:32:23,000 performing a request an HTTP request to 767 00:32:23,000 --> 00:32:25,279 get data in readon format you're not 768 00:32:25,279 --> 00:32:27,519 manipulating any data at all on the 769 00:32:27,519 --> 00:32:30,559 server side so sometimes you might need 770 00:32:30,559 --> 00:32:32,880 to retrieve the data but you also want 771 00:32:32,880 --> 00:32:35,679 to have that data already um manipulated 772 00:32:35,679 --> 00:32:38,200 in a certain way on the server side so 773 00:32:38,200 --> 00:32:40,919 for example up top over here I added a 774 00:32:40,919 --> 00:32:43,519 couple more user objects so let's say I 775 00:32:43,519 --> 00:32:45,440 have this users array and let's pretend 776 00:32:45,440 --> 00:32:47,679 it's from the database and let's say I 777 00:32:47,679 --> 00:32:49,799 want all of these users returned back 778 00:32:49,799 --> 00:32:52,360 but I wanted it sorted in alphabetical 779 00:32:52,360 --> 00:32:54,760 order based on the username or you can 780 00:32:54,760 --> 00:32:56,360 also have it sorted based on the display 781 00:32:56,360 --> 00:32:58,760 name maybe you might also want it sorted 782 00:32:58,760 --> 00:33:01,279 in um from least to greatest based on 783 00:33:01,279 --> 00:33:03,399 the ID value since these IDs are 784 00:33:03,399 --> 00:33:06,320 integers so you would use a query string 785 00:33:06,320 --> 00:33:09,080 to do that let's say if you also want to 786 00:33:09,080 --> 00:33:12,760 filter out some results from the users 787 00:33:12,760 --> 00:33:14,720 itself maybe you don't want to get every 788 00:33:14,720 --> 00:33:17,120 single user from the database you only 789 00:33:17,120 --> 00:33:20,080 want to get only specific users that 790 00:33:20,080 --> 00:33:22,559 match whether their user matches a 791 00:33:22,559 --> 00:33:25,799 substring so maybe I only want to get 792 00:33:25,799 --> 00:33:29,480 all the users that have an A in their 793 00:33:29,480 --> 00:33:32,200 username field so hopefully that makes 794 00:33:32,200 --> 00:33:33,799 sense with query parameters and how they 795 00:33:33,799 --> 00:33:37,360 can be used so let's go ahead and see 796 00:33:37,360 --> 00:33:40,320 how we can actually send query strings 797 00:33:40,320 --> 00:33:43,679 and query parameters to our server so 798 00:33:43,679 --> 00:33:48,240 inside my/ API users route inside the 799 00:33:48,240 --> 00:33:50,519 request Handler function I'm going to go 800 00:33:50,519 --> 00:33:53,559 ahead and consol log this request. query 801 00:33:53,559 --> 00:33:55,480 object remember how I said earlier the 802 00:33:55,480 --> 00:33:57,559 request object has everything that you 803 00:33:57,559 --> 00:34:00,000 can possibly get in regards to the 804 00:34:00,000 --> 00:34:03,320 request itself so earlier we referenced 805 00:34:03,320 --> 00:34:05,480 request. prams to get the route 806 00:34:05,480 --> 00:34:08,280 parameter so to get the query parameters 807 00:34:08,280 --> 00:34:10,719 from the query string we just reference 808 00:34:10,719 --> 00:34:12,918 request. query so let's go ahead and 809 00:34:12,918 --> 00:34:15,320 send a query string when we are making 810 00:34:15,320 --> 00:34:18,599 this request to the SL users end point 811 00:34:18,599 --> 00:34:21,199 so I'm going to use the question mark 812 00:34:21,199 --> 00:34:23,320 symbol and then provide some key value 813 00:34:23,320 --> 00:34:25,839 pairs I can literally pass any key value 814 00:34:25,839 --> 00:34:27,520 pair I want so let's do something like 815 00:34:27,520 --> 00:34:29,560 filter and we'll assume the filter is 816 00:34:29,560 --> 00:34:31,480 going to be based on username um so I'll 817 00:34:31,480 --> 00:34:33,520 do filter uh 818 00:34:33,520 --> 00:34:37,000 Anson and I'll go into my console uh let 819 00:34:37,000 --> 00:34:39,760 me actually just rerun the request so 820 00:34:39,760 --> 00:34:42,560 you see how whenever I send a request a 821 00:34:42,560 --> 00:34:45,280 get request to that endpoint in the 822 00:34:45,280 --> 00:34:47,480 console it logs that request query 823 00:34:47,480 --> 00:34:50,040 object and it has the filter which is 824 00:34:50,040 --> 00:34:52,280 the key that I passed in the query 825 00:34:52,280 --> 00:34:55,040 parameter filter it is showing up as a 826 00:34:55,040 --> 00:34:58,520 field in that object that query object 827 00:34:58,520 --> 00:35:01,320 and we have the uh string Anon as the 828 00:35:01,320 --> 00:35:03,960 value so the query string gets parsed 829 00:35:03,960 --> 00:35:06,880 into a Json object by Express so we can 830 00:35:06,880 --> 00:35:09,040 very easily grab the values let's go 831 00:35:09,040 --> 00:35:10,480 ahead and actually do something 832 00:35:10,480 --> 00:35:12,119 realistic with the filtering so what 833 00:35:12,119 --> 00:35:14,400 I'll do is this I want to make it so 834 00:35:14,400 --> 00:35:16,520 that I can filter based on sub some 835 00:35:16,520 --> 00:35:18,520 substring so I want to go ahead and also 836 00:35:18,520 --> 00:35:20,960 make it so that I can also set which 837 00:35:20,960 --> 00:35:23,480 field in this mock users array in in 838 00:35:23,480 --> 00:35:24,960 these objects I want to make sure I can 839 00:35:24,960 --> 00:35:26,880 set which field I want to filter on so 840 00:35:26,880 --> 00:35:28,040 maybe I want to alternate between 841 00:35:28,040 --> 00:35:31,839 filtering by username or display name so 842 00:35:31,839 --> 00:35:34,599 for the filter value we can expect it to 843 00:35:34,599 --> 00:35:37,240 only be two possible values 844 00:35:37,240 --> 00:35:40,359 username or display name and this will 845 00:35:40,359 --> 00:35:44,400 tell us what uh key in the user object 846 00:35:44,400 --> 00:35:46,920 what field to filter by or yeah what 847 00:35:46,920 --> 00:35:48,920 what field to filter and then we will 848 00:35:48,920 --> 00:35:50,880 add an additional query parameter can 849 00:35:50,880 --> 00:35:52,359 call it whatever you want but I guess we 850 00:35:52,359 --> 00:35:55,520 can call it a value just to keep things 851 00:35:55,520 --> 00:35:57,839 simple and then this this value will 852 00:35:57,839 --> 00:36:00,839 basically be the uh the text the 853 00:36:00,839 --> 00:36:02,880 substring that you want to have that 854 00:36:02,880 --> 00:36:05,400 username contain so if I want to filter 855 00:36:05,400 --> 00:36:07,560 everything where it contains the an 856 00:36:07,560 --> 00:36:10,200 substring so a an substring then we 857 00:36:10,200 --> 00:36:12,520 would have to search for that okay so 858 00:36:12,520 --> 00:36:15,599 I'm going to send these two query 859 00:36:15,599 --> 00:36:19,240 parameters to the server okay so now I'm 860 00:36:19,240 --> 00:36:20,640 going to go 861 00:36:20,640 --> 00:36:24,000 into go back to the request Handler for 862 00:36:24,000 --> 00:36:26,319 the users endpoint and what I'll do is 863 00:36:26,319 --> 00:36:27,880 this I'm going to 864 00:36:27,880 --> 00:36:30,760 destructure that query object from the 865 00:36:30,760 --> 00:36:32,760 request object and then I also want to 866 00:36:32,760 --> 00:36:35,480 destructure from the query 867 00:36:35,480 --> 00:36:39,319 object the two query parameters filter 868 00:36:39,319 --> 00:36:42,440 and value and I can do that all in one 869 00:36:42,440 --> 00:36:45,760 go like this so I can use so after query 870 00:36:45,760 --> 00:36:47,599 I can additionally destructure 871 00:36:47,599 --> 00:36:50,960 properties from query so let's do filter 872 00:36:50,960 --> 00:36:54,640 and value and so what I want to do is I 873 00:36:54,640 --> 00:36:56,240 want to make sure that both of these 874 00:36:56,240 --> 00:36:58,760 query parameters exist because of course 875 00:36:58,760 --> 00:37:01,200 if they don't exist then we're not going 876 00:37:01,200 --> 00:37:03,880 to do any filtering at all so the 877 00:37:03,880 --> 00:37:05,920 easiest case that we can handle is we 878 00:37:05,920 --> 00:37:08,160 check to see if both of these values are 879 00:37:08,160 --> 00:37:09,839 undefined because if they are then we 880 00:37:09,839 --> 00:37:12,000 don't need to do any filtering we just 881 00:37:12,000 --> 00:37:15,800 return mock users as is so we'll you 882 00:37:15,800 --> 00:37:18,800 write an if case if there's no filter 883 00:37:18,800 --> 00:37:22,160 and there's no value then we will just 884 00:37:22,160 --> 00:37:25,720 simply return response. send and then 885 00:37:25,720 --> 00:37:28,680 call or not call uh pass and mock users 886 00:37:28,680 --> 00:37:31,520 in this do send method call okay that's 887 00:37:31,520 --> 00:37:32,880 the easiest case I'll write a simple 888 00:37:32,880 --> 00:37:39,520 comment when filter and and value are 889 00:37:39,520 --> 00:37:41,400 undefined 890 00:37:41,400 --> 00:37:44,440 okay and we always want to make sure 891 00:37:44,440 --> 00:37:47,160 that both of these query parameters are 892 00:37:47,160 --> 00:37:49,160 defined because you need both of them of 893 00:37:49,160 --> 00:37:51,040 course you can't have a value and not 894 00:37:51,040 --> 00:37:53,480 know what field in the user object you 895 00:37:53,480 --> 00:37:55,760 want to filter by and if you have the 896 00:37:55,760 --> 00:37:58,160 filter query parameter defined you need 897 00:37:58,160 --> 00:38:00,000 to make sure you have an actual text 898 00:38:00,000 --> 00:38:02,400 that you want to filter uh that you want 899 00:38:02,400 --> 00:38:05,160 to filter based on so we need to make 900 00:38:05,160 --> 00:38:07,160 sure that both of these are defined so 901 00:38:07,160 --> 00:38:10,960 we'll do if filter and 902 00:38:10,960 --> 00:38:16,520 value if filter and value we will 903 00:38:16,560 --> 00:38:19,960 return and we'll call response. send and 904 00:38:19,960 --> 00:38:23,119 from here I should just be able to write 905 00:38:23,119 --> 00:38:26,720 a simple filter function so mock users I 906 00:38:26,720 --> 00:38:29,920 can use the filter function on the array 907 00:38:29,920 --> 00:38:32,640 and pass in a predicate so what we're 908 00:38:32,640 --> 00:38:33,720 going to do is we're going to pass in 909 00:38:33,720 --> 00:38:35,160 this callback function also known as a 910 00:38:35,160 --> 00:38:38,280 predicate function and this will this 911 00:38:38,280 --> 00:38:41,400 call function has uh the user as an 912 00:38:41,400 --> 00:38:44,599 argument and then what we want to do is 913 00:38:44,599 --> 00:38:47,520 we want to filter out all of the we want 914 00:38:47,520 --> 00:38:49,480 to filter all the user objects that 915 00:38:49,480 --> 00:38:51,839 match that have that value as a 916 00:38:51,839 --> 00:38:54,240 substring so it's pretty easy we can do 917 00:38:54,240 --> 00:38:56,680 user. username because remember we're 918 00:38:56,680 --> 00:38:58,920 filtering by by the 919 00:38:58,920 --> 00:39:02,440 username or actually it would be user 920 00:39:02,440 --> 00:39:05,440 square brackets filter okay and this is 921 00:39:05,440 --> 00:39:07,440 assuming that filter would either be 922 00:39:07,440 --> 00:39:09,240 display name or 923 00:39:09,240 --> 00:39:12,760 username so user filter so this would 924 00:39:12,760 --> 00:39:15,440 grab the correct field and then we would 925 00:39:15,440 --> 00:39:17,119 want to so this is a this is going to be 926 00:39:17,119 --> 00:39:20,119 a string so we would want to make sure 927 00:39:20,119 --> 00:39:22,280 we check to see if the string 928 00:39:22,280 --> 00:39:25,319 contains that substring so we actually 929 00:39:25,319 --> 00:39:28,599 have this um 930 00:39:28,599 --> 00:39:31,480 includes and this method returns true if 931 00:39:31,480 --> 00:39:33,200 search string appears as a substring of 932 00:39:33,200 --> 00:39:35,319 the result of converting this object to 933 00:39:35,319 --> 00:39:39,640 a string okay so I can pretty much call 934 00:39:39,640 --> 00:39:41,200 do 935 00:39:41,200 --> 00:39:43,839 includes and I'll pass in the 936 00:39:43,839 --> 00:39:47,079 value so this will filter all of this 937 00:39:47,079 --> 00:39:49,079 will basically grab all of the user 938 00:39:49,079 --> 00:39:52,200 objects that pass this predicate so if 939 00:39:52,200 --> 00:39:54,280 the user and whether we are filtering by 940 00:39:54,280 --> 00:39:56,720 username or display name if let's say 941 00:39:56,720 --> 00:39:58,440 for example let's stick with username if 942 00:39:58,440 --> 00:40:00,800 the username includes the value that 943 00:40:00,800 --> 00:40:03,760 we're trying to filter then it's going 944 00:40:03,760 --> 00:40:07,480 to return that into a new array and then 945 00:40:07,480 --> 00:40:09,000 once all of the filtering is done we're 946 00:40:09,000 --> 00:40:10,960 going to send the entire array back so 947 00:40:10,960 --> 00:40:13,079 let's go ahead and test this out so 948 00:40:13,079 --> 00:40:15,520 right now if I let's do this if I don't 949 00:40:15,520 --> 00:40:18,839 have any of the query parameters at all 950 00:40:18,839 --> 00:40:20,960 you can see that it will just return the 951 00:40:20,960 --> 00:40:23,720 array as is okay it doesn't uh it 952 00:40:23,720 --> 00:40:25,880 doesn't do anything we have everything 953 00:40:25,880 --> 00:40:27,880 sorted we have everything the way it is 954 00:40:27,880 --> 00:40:30,560 nothing is sorted nothing is done let's 955 00:40:30,560 --> 00:40:33,960 go ahead and add a filter so filter 956 00:40:33,960 --> 00:40:35,800 let's filter by 957 00:40:35,800 --> 00:40:40,160 username and now notice how if I were to 958 00:40:40,160 --> 00:40:44,640 only have the filter but no value you'll 959 00:40:44,640 --> 00:40:47,000 see how it doesn't return anything yet 960 00:40:47,000 --> 00:40:48,520 the request is still pending that's 961 00:40:48,520 --> 00:40:51,880 because we need both filter and value 962 00:40:51,880 --> 00:40:55,040 okay we'll handle these cases as well so 963 00:40:55,040 --> 00:40:56,920 let's go ahead and handle a case where 964 00:40:56,920 --> 00:40:59,760 we have both filter and value as a query 965 00:40:59,760 --> 00:41:01,880 parameter so for the value query 966 00:41:01,880 --> 00:41:06,119 parameter I will set this to be a n and 967 00:41:06,119 --> 00:41:08,920 now you'll see this will grab me all of 968 00:41:08,920 --> 00:41:12,440 the user objects where the username has 969 00:41:12,440 --> 00:41:15,960 a n as a substring and if you look right 970 00:41:15,960 --> 00:41:19,079 over here it seems to be filtering 971 00:41:19,079 --> 00:41:21,000 correctly uh I can go ahead and do 972 00:41:21,000 --> 00:41:24,040 another simple case where let's 973 00:41:24,040 --> 00:41:27,119 filter the username where it includes e 974 00:41:27,119 --> 00:41:29,200 as a substring and you can see that 975 00:41:29,200 --> 00:41:32,839 seems like the only username that I have 976 00:41:32,839 --> 00:41:35,119 that has an e as a substring is 977 00:41:35,119 --> 00:41:38,720 Henry okay uh let's see what else let's 978 00:41:38,720 --> 00:41:43,200 try um let's try 979 00:41:43,200 --> 00:41:48,319 a I have 1 two 3 4 five so it's missing 980 00:41:48,319 --> 00:41:50,680 uh this object Henry so our filtering is 981 00:41:50,680 --> 00:41:53,800 working great okay so let's just finish 982 00:41:53,800 --> 00:41:57,200 this out um so let's make sure 983 00:41:57,200 --> 00:41:59,359 we handle all the other cases where if 984 00:41:59,359 --> 00:42:02,160 we don't have both of 985 00:42:02,160 --> 00:42:06,720 these um both of these defined then we 986 00:42:06,720 --> 00:42:10,560 return the same mock users that is in 987 00:42:10,560 --> 00:42:12,319 memory so we don't do any filtering at 988 00:42:12,319 --> 00:42:14,800 all so I think the easy thing to do is 989 00:42:14,800 --> 00:42:17,160 actually this instead of uh doing this 990 00:42:17,160 --> 00:42:19,160 if check right up here where we check 991 00:42:19,160 --> 00:42:21,400 both a filter and where we check if 992 00:42:21,400 --> 00:42:23,359 there's no filter and there's no value 993 00:42:23,359 --> 00:42:25,960 what we'll do is we'll check if there's 994 00:42:25,960 --> 00:42:29,520 fil filter and if there's a value and if 995 00:42:29,520 --> 00:42:32,000 this condition fails that means it only 996 00:42:32,000 --> 00:42:35,880 has one or the other defined or both are 997 00:42:35,880 --> 00:42:38,000 undefined so then we'll just return 998 00:42:38,000 --> 00:42:41,079 response. send mock users so I just 999 00:42:41,079 --> 00:42:42,800 realized that I'm going to fix that real 1000 00:42:42,800 --> 00:42:44,920 quick and now when I go back to the 1001 00:42:44,920 --> 00:42:48,599 browser if I only have one query 1002 00:42:48,599 --> 00:42:51,040 parameter it won't do any filtering at 1003 00:42:51,040 --> 00:42:55,160 all okay hopefully this makes 1004 00:42:55,160 --> 00:42:58,160 sense 1005 00:42:59,280 --> 00:43:01,200 now that you all know how to retrieve 1006 00:43:01,200 --> 00:43:03,520 data from the express API using get 1007 00:43:03,520 --> 00:43:05,760 requests I'm going to show you how you 1008 00:43:05,760 --> 00:43:08,640 can create data using what is called a 1009 00:43:08,640 --> 00:43:09,559 post 1010 00:43:09,559 --> 00:43:11,960 request now let's say for example you 1011 00:43:11,960 --> 00:43:15,599 want to create a resource on the backend 1012 00:43:15,599 --> 00:43:16,920 and that backend will save it to a 1013 00:43:16,920 --> 00:43:19,800 database or save it to a file or save it 1014 00:43:19,800 --> 00:43:21,760 just somewhere doesn't matter where it 1015 00:43:21,760 --> 00:43:24,079 is you want to create let's say a user 1016 00:43:24,079 --> 00:43:25,680 so your client your front-end 1017 00:43:25,680 --> 00:43:27,800 application will have a user form they 1018 00:43:27,800 --> 00:43:29,800 fill out their username password email 1019 00:43:29,800 --> 00:43:31,920 and other additional Fields once they 1020 00:43:31,920 --> 00:43:34,359 are ready they will click that signup 1021 00:43:34,359 --> 00:43:36,359 button when you click that sign up 1022 00:43:36,359 --> 00:43:39,559 button it will make an API request to 1023 00:43:39,559 --> 00:43:41,599 the backend doesn't have to be 1024 00:43:41,599 --> 00:43:43,559 necessarily an Express API server it can 1025 00:43:43,559 --> 00:43:45,920 be really any API server that's running 1026 00:43:45,920 --> 00:43:48,520 that handles that post request okay so 1027 00:43:48,520 --> 00:43:50,599 the front end the client side would make 1028 00:43:50,599 --> 00:43:54,920 an HTTP request a post request to the 1029 00:43:54,920 --> 00:43:58,200 server okay okay once the server 1030 00:43:58,200 --> 00:44:00,480 receives that request the server needs 1031 00:44:00,480 --> 00:44:01,960 to obviously be able to grab the data 1032 00:44:01,960 --> 00:44:03,119 that we're trying to send from the 1033 00:44:03,119 --> 00:44:06,720 client side to the backend and that data 1034 00:44:06,720 --> 00:44:08,839 that you're sending is known as a 1035 00:44:08,839 --> 00:44:12,040 request body so whenever you make post 1036 00:44:12,040 --> 00:44:14,079 requests the data that you want to send 1037 00:44:14,079 --> 00:44:17,240 to the backend server you send it via a 1038 00:44:17,240 --> 00:44:20,160 payload or a request body you use those 1039 00:44:20,160 --> 00:44:22,680 terms synonymously so payload request 1040 00:44:22,680 --> 00:44:24,760 body are interchangeable terms the 1041 00:44:24,760 --> 00:44:27,920 backend will then take that data and it 1042 00:44:27,920 --> 00:44:30,040 will perform the necessary operations in 1043 00:44:30,040 --> 00:44:32,240 need so typically validation if it needs 1044 00:44:32,240 --> 00:44:34,160 to do additional parsing if it needs to 1045 00:44:34,160 --> 00:44:36,359 make sure that it has the proper Fields 1046 00:44:36,359 --> 00:44:39,119 it will do all that stuff before it can 1047 00:44:39,119 --> 00:44:41,079 proceed with either saving it to a 1048 00:44:41,079 --> 00:44:43,319 database or saving it to some external 1049 00:44:43,319 --> 00:44:45,800 API Source whatever it is I needs to do 1050 00:44:45,800 --> 00:44:49,040 once it's done saving that record to the 1051 00:44:49,040 --> 00:44:51,960 database or somewhere it will return a 1052 00:44:51,960 --> 00:44:55,440 2011 response which or 2011 status code 1053 00:44:55,440 --> 00:44:57,240 which typically just means that the 1054 00:44:57,240 --> 00:44:59,359 resource was created sometimes it might 1055 00:44:59,359 --> 00:45:01,640 also return the new record that was 1056 00:45:01,640 --> 00:45:03,559 created so that way if you need to use 1057 00:45:03,559 --> 00:45:05,280 it on the client side for whatever 1058 00:45:05,280 --> 00:45:07,720 reason you can do so now before we 1059 00:45:07,720 --> 00:45:10,559 actually can make any post requests we 1060 00:45:10,559 --> 00:45:14,319 do need an htgp client to actually uh 1061 00:45:14,319 --> 00:45:16,040 make those requests and also be able to 1062 00:45:16,040 --> 00:45:19,720 send a request body to our Express API 1063 00:45:19,720 --> 00:45:20,960 because on the browser there's no 1064 00:45:20,960 --> 00:45:23,920 built-in tool that enables you to send 1065 00:45:23,920 --> 00:45:26,559 request bodies unless if to write the 1066 00:45:26,559 --> 00:45:28,200 code in the JavaScript console but we're 1067 00:45:28,200 --> 00:45:30,319 not going to do that um so there are 1068 00:45:30,319 --> 00:45:33,480 different uh clients that you can use to 1069 00:45:33,480 --> 00:45:35,280 interact with your API So currently 1070 00:45:35,280 --> 00:45:36,880 we've just been using the browser which 1071 00:45:36,880 --> 00:45:39,599 we limited to just making get requests 1072 00:45:39,599 --> 00:45:42,160 by simply typing in the address in the 1073 00:45:42,160 --> 00:45:44,319 address bar we want to be able to make 1074 00:45:44,319 --> 00:45:46,839 post requests where we can send actual 1075 00:45:46,839 --> 00:45:49,599 data okay so you can use tools like 1076 00:45:49,599 --> 00:45:52,040 Postman there's also Hopscotch which is 1077 00:45:52,040 --> 00:45:54,359 an alternative to postman for this 1078 00:45:54,359 --> 00:45:56,000 tutorial I'm going to keep things simple 1079 00:45:56,000 --> 00:45:57,520 I'm going to keep everything inside vs 1080 00:45:57,520 --> 00:45:59,520 code and we're going to install this 1081 00:45:59,520 --> 00:46:02,599 extension so on the left hand side or um 1082 00:46:02,599 --> 00:46:05,240 wherever you have this extensions icon 1083 00:46:05,240 --> 00:46:07,680 just click on extensions and you want to 1084 00:46:07,680 --> 00:46:11,359 search for an extension called Thunder 1085 00:46:11,359 --> 00:46:14,280 client and thunder client is a very 1086 00:46:14,280 --> 00:46:17,160 lightweight rest API tool for VSS code 1087 00:46:17,160 --> 00:46:18,440 it's integrated in there you just have 1088 00:46:18,440 --> 00:46:21,599 to install it and it allows you to make 1089 00:46:21,599 --> 00:46:25,119 API calls to your Express server so I'll 1090 00:46:25,119 --> 00:46:27,359 go ahead and click 1091 00:46:27,359 --> 00:46:29,440 install Okay and then I'm going to go 1092 00:46:29,440 --> 00:46:30,720 just close 1093 00:46:30,720 --> 00:46:34,040 this and then let's go ahead on the left 1094 00:46:34,040 --> 00:46:35,240 hand side you should see the Thunder 1095 00:46:35,240 --> 00:46:38,440 client appear right over here as an icon 1096 00:46:38,440 --> 00:46:40,559 that's the Thunder client and I'll click 1097 00:46:40,559 --> 00:46:43,079 on it and 1098 00:46:43,079 --> 00:46:46,599 now what I can do is I can create a new 1099 00:46:46,599 --> 00:46:49,839 request by clicking on the new request 1100 00:46:49,839 --> 00:46:52,960 button and you can see now it looks it 1101 00:46:52,960 --> 00:46:55,960 it looks kind of identical to post man 1102 00:46:55,960 --> 00:46:58,240 if you've used it before or really any 1103 00:46:58,240 --> 00:47:00,920 other rest client but we have an address 1104 00:47:00,920 --> 00:47:03,960 bar where we can uh type in the address 1105 00:47:03,960 --> 00:47:06,559 or the URL that we want to make requests 1106 00:47:06,559 --> 00:47:09,319 to so I'll type in Local Host Port 1107 00:47:09,319 --> 00:47:13,480 3000 API users I'll make a get request 1108 00:47:13,480 --> 00:47:15,000 so I can select this drop down and 1109 00:47:15,000 --> 00:47:17,000 select get and I'll click Send and you 1110 00:47:17,000 --> 00:47:19,400 can see that it gives us back the 1111 00:47:19,400 --> 00:47:22,640 data just like that okay and we're going 1112 00:47:22,640 --> 00:47:25,839 to use this client to switch between 1113 00:47:25,839 --> 00:47:28,720 different types of HTTP requests that we 1114 00:47:28,720 --> 00:47:30,800 want to make so we'll switch from get to 1115 00:47:30,800 --> 00:47:32,599 post and then in later videos when I 1116 00:47:32,599 --> 00:47:34,319 show you how to handle put requests or 1117 00:47:34,319 --> 00:47:37,119 delete requests we will switch to these 1118 00:47:37,119 --> 00:47:40,760 HTTP requests as well okay so let's 1119 00:47:40,760 --> 00:47:42,640 close this out just wanted to show you 1120 00:47:42,640 --> 00:47:45,480 all how to set up thunder client so now 1121 00:47:45,480 --> 00:47:47,200 what I'm going to do is set up our post 1122 00:47:47,200 --> 00:47:50,240 request to be able to create a brand new 1123 00:47:50,240 --> 00:47:52,880 user so what I'm going to do is right 1124 00:47:52,880 --> 00:47:57,440 underneath my uh API user users route 1125 00:47:57,440 --> 00:48:00,680 I'll go ahead and reference the app 1126 00:48:00,680 --> 00:48:02,960 variable and since I want to register a 1127 00:48:02,960 --> 00:48:05,000 post request I'm going to go ahead and 1128 00:48:05,000 --> 00:48:07,920 call the Post method so this method is 1129 00:48:07,920 --> 00:48:11,480 very similar to all the other HTTP verb 1130 00:48:11,480 --> 00:48:14,520 methods such as get put delete uh it 1131 00:48:14,520 --> 00:48:17,599 takes in a path so over here I'm going 1132 00:48:17,599 --> 00:48:21,480 to go and pass in/ API users now you're 1133 00:48:21,480 --> 00:48:23,440 probably wondering well should we be 1134 00:48:23,440 --> 00:48:25,960 able to reuse the path and the answer is 1135 00:48:25,960 --> 00:48:29,760 yes because you have a different HTTP 1136 00:48:29,760 --> 00:48:32,359 request type being used so this is for 1137 00:48:32,359 --> 00:48:34,960 post request and the one over here is 1138 00:48:34,960 --> 00:48:38,240 for get requests okay when your HTP 1139 00:48:38,240 --> 00:48:41,000 client is making requests the server 1140 00:48:41,000 --> 00:48:43,119 knows if it's making a get or a post 1141 00:48:43,119 --> 00:48:44,480 request so that way there's no 1142 00:48:44,480 --> 00:48:46,760 confliction between these two different 1143 00:48:46,760 --> 00:48:49,799 types of requests despite the route 1144 00:48:49,799 --> 00:48:54,440 being the same so we'll also pass in a 1145 00:48:54,440 --> 00:48:56,880 request and response 1146 00:48:56,880 --> 00:48:58,400 or we'll pass in the request Handler 1147 00:48:58,400 --> 00:49:00,319 function which will have these two 1148 00:49:00,319 --> 00:49:03,160 arguments request and response and then 1149 00:49:03,160 --> 00:49:06,280 for now I will just return a response. 1150 00:49:06,280 --> 00:49:10,720 send I'll just pass in a 200 status code 1151 00:49:10,720 --> 00:49:12,040 or I'll just pass in 1152 00:49:12,040 --> 00:49:15,440 200 and I'll just console log the 1153 00:49:15,440 --> 00:49:17,880 request body just so that we can see 1154 00:49:17,880 --> 00:49:19,599 what our data is looking like when we 1155 00:49:19,599 --> 00:49:21,760 send it from the client so let's go back 1156 00:49:21,760 --> 00:49:23,559 to thunder clients I'll click on the 1157 00:49:23,559 --> 00:49:26,160 Thunderbolt icon I'll click on new 1158 00:49:26,160 --> 00:49:29,119 request and up top where you see gets 1159 00:49:29,119 --> 00:49:32,200 just click on that drop down and select 1160 00:49:32,200 --> 00:49:35,000 post and we're going to change the url 1161 00:49:35,000 --> 00:49:37,040 and we're going to type in Local Host 1162 00:49:37,040 --> 00:49:41,160 Port 3000 API users okay so once again 1163 00:49:41,160 --> 00:49:44,640 we have uh we have two different types 1164 00:49:44,640 --> 00:49:45,599 of 1165 00:49:45,599 --> 00:49:49,280 HTTP uh methods but they both use the 1166 00:49:49,280 --> 00:49:54,400 same path okay so whenever I call a post 1167 00:49:54,400 --> 00:49:57,400 request to SL API users I don't even 1168 00:49:57,400 --> 00:49:59,280 need to send any data for now it's just 1169 00:49:59,280 --> 00:50:02,760 going to give me back a 200 status 1170 00:50:02,760 --> 00:50:06,200 code now I can go into the body tab in 1171 00:50:06,200 --> 00:50:09,520 my thunder client and I can select Json 1172 00:50:09,520 --> 00:50:12,480 if I want to send Json so if I try to 1173 00:50:12,480 --> 00:50:14,359 send a request body let's see what 1174 00:50:14,359 --> 00:50:17,119 happens let's click Send so in the 1175 00:50:17,119 --> 00:50:20,280 console log for our in our terminal you 1176 00:50:20,280 --> 00:50:21,599 can see that right now it's actually 1177 00:50:21,599 --> 00:50:22,640 logging 1178 00:50:22,640 --> 00:50:24,880 undefined when I try to send it again it 1179 00:50:24,880 --> 00:50:26,520 still logs and you're probably wondering 1180 00:50:26,520 --> 00:50:28,240 well what's going on with this why is it 1181 00:50:28,240 --> 00:50:29,839 undefined well the reason why it's 1182 00:50:29,839 --> 00:50:31,280 undefined is because right now by 1183 00:50:31,280 --> 00:50:34,359 default Express is not parsing those 1184 00:50:34,359 --> 00:50:37,040 request bodies that are coming in so 1185 00:50:37,040 --> 00:50:39,760 whenever I am sending Json to the 1186 00:50:39,760 --> 00:50:42,319 express server the headers set the 1187 00:50:42,319 --> 00:50:45,720 content type to application Json Express 1188 00:50:45,720 --> 00:50:47,960 doesn't parse those payloads by default 1189 00:50:47,960 --> 00:50:50,280 so we need to tell Express to do so now 1190 00:50:50,280 --> 00:50:51,760 this is going to require us to use a 1191 00:50:51,760 --> 00:50:53,119 middleware so this is kind of like a 1192 00:50:53,119 --> 00:50:55,040 brief little intro to middlewares but 1193 00:50:55,040 --> 00:50:56,760 don't worry so much about it once we 1194 00:50:56,760 --> 00:50:57,839 actually get into the topic of 1195 00:50:57,839 --> 00:50:59,760 middlewares you'll better understand how 1196 00:50:59,760 --> 00:51:02,040 they work but all middleware is is just 1197 00:51:02,040 --> 00:51:04,359 a function that is going to be invoked 1198 00:51:04,359 --> 00:51:08,079 before uh certain API requests are being 1199 00:51:08,079 --> 00:51:10,400 handled so in my case I want to make 1200 00:51:10,400 --> 00:51:13,119 sure that right before my post request 1201 00:51:13,119 --> 00:51:14,960 is being received I want to make sure 1202 00:51:14,960 --> 00:51:17,480 that that middleware that parses the 1203 00:51:17,480 --> 00:51:20,839 Json payload accordingly is being 1204 00:51:20,839 --> 00:51:22,960 invoked so you typically want to 1205 00:51:22,960 --> 00:51:24,520 register your middleware as early as 1206 00:51:24,520 --> 00:51:26,400 possible so the best way to do it is 1207 00:51:26,400 --> 00:51:29,480 doing it up top after you create your 1208 00:51:29,480 --> 00:51:32,040 Express app instance so I'm going to go 1209 00:51:32,040 --> 00:51:34,079 ahead and reference app and I'm going to 1210 00:51:34,079 --> 00:51:36,440 call the use method and this is the 1211 00:51:36,440 --> 00:51:38,799 method that you use to register 1212 00:51:38,799 --> 00:51:40,680 middleware and the middleware that we're 1213 00:51:40,680 --> 00:51:43,400 going to register is actually already 1214 00:51:43,400 --> 00:51:45,599 built into Express so I can just 1215 00:51:45,599 --> 00:51:48,680 reference Express and call this 1216 00:51:48,680 --> 00:51:50,400 Json 1217 00:51:50,400 --> 00:51:53,359 method okay so now you can even read 1218 00:51:53,359 --> 00:51:55,079 over here it looks at requests where the 1219 00:51:55,079 --> 00:51:57,400 cont type header matches the type option 1220 00:51:57,400 --> 00:52:00,000 so in this case this is express. Json 1221 00:52:00,000 --> 00:52:03,119 there's also other um uh there's also 1222 00:52:03,119 --> 00:52:05,240 other things that you can uh parse to 1223 00:52:05,240 --> 00:52:06,280 like let's say if you're trying to send 1224 00:52:06,280 --> 00:52:09,280 text or if you're trying to send uh URL 1225 00:52:09,280 --> 00:52:13,440 encoded or raw data so hopefully that 1226 00:52:13,440 --> 00:52:15,559 makes sense so in our case we'll keep it 1227 00:52:15,559 --> 00:52:16,480 as 1228 00:52:16,480 --> 00:52:18,440 Json and let's go ahead and see what 1229 00:52:18,440 --> 00:52:21,400 happens if I send the request 1230 00:52:21,400 --> 00:52:25,559 again so let's go back into our under 1231 00:52:25,559 --> 00:52:26,680 client I'll click 1232 00:52:26,680 --> 00:52:30,119 Send I'll just click it again and now 1233 00:52:30,119 --> 00:52:32,799 watch this you can see that in the 1234 00:52:32,799 --> 00:52:36,319 console it is logging that 1235 00:52:36,319 --> 00:52:38,680 request that that request body that I am 1236 00:52:38,680 --> 00:52:41,880 sending to the express server and I can 1237 00:52:41,880 --> 00:52:44,160 literally add as many fields as I want I 1238 00:52:44,160 --> 00:52:45,760 can add a display 1239 00:52:45,760 --> 00:52:48,880 name let's do Anon 1240 00:52:48,880 --> 00:52:53,119 the dev click Send again and you can see 1241 00:52:53,119 --> 00:52:54,799 that it is being logged right over here 1242 00:52:54,799 --> 00:52:57,280 okay perfect Perfect all right 1243 00:52:57,280 --> 00:53:00,119 cool now let's go ahead and actually do 1244 00:53:00,119 --> 00:53:02,359 something with the data so like I said 1245 00:53:02,359 --> 00:53:04,040 right now we don't have an actual 1246 00:53:04,040 --> 00:53:07,000 database so all I'm going to do is just 1247 00:53:07,000 --> 00:53:10,000 push this user to the array 1248 00:53:10,000 --> 00:53:12,839 so uh to do that what we'll do is we'll 1249 00:53:12,839 --> 00:53:15,359 assume that the request body is valid 1250 00:53:15,359 --> 00:53:17,119 but then in the next section I'm going 1251 00:53:17,119 --> 00:53:19,480 to show you how we can actually validate 1252 00:53:19,480 --> 00:53:21,520 the request body so I'm going to go 1253 00:53:21,520 --> 00:53:24,160 ahead and create a variable called new 1254 00:53:24,160 --> 00:53:28,400 user equals so we need to actually grab 1255 00:53:28,400 --> 00:53:30,440 all of the fields from the request body 1256 00:53:30,440 --> 00:53:34,119 but we also need to attach an ID to the 1257 00:53:34,119 --> 00:53:35,760 request body and once again since we 1258 00:53:35,760 --> 00:53:39,359 don't have a database to manage our uh 1259 00:53:39,359 --> 00:53:41,200 IDs because typically the database is 1260 00:53:41,200 --> 00:53:44,359 responsible for generating those 1261 00:53:44,359 --> 00:53:47,440 IDs all I'm going to do is just take uh 1262 00:53:47,440 --> 00:53:49,960 the last element the last user in the 1263 00:53:49,960 --> 00:53:52,599 mock user array uh take the ID of that 1264 00:53:52,599 --> 00:53:55,440 last user add one to it and assign that 1265 00:53:55,440 --> 00:53:59,400 to the new user the new new user's ID 1266 00:53:59,400 --> 00:54:03,440 okay so what I'll do is I'll first do 1267 00:54:03,440 --> 00:54:05,359 this mock users so I'm just going to 1268 00:54:05,359 --> 00:54:07,920 reference mock users um and then I'm 1269 00:54:07,920 --> 00:54:10,559 going to want to get the last element so 1270 00:54:10,559 --> 00:54:13,520 mock users. length minus 1271 00:54:13,520 --> 00:54:16,760 one okay so the length of our array is 1272 00:54:16,760 --> 00:54:18,520 going to be seven and I want to 1273 00:54:18,520 --> 00:54:20,400 reference the last element so that's 1274 00:54:20,400 --> 00:54:22,079 going to be at index six because 1275 00:54:22,079 --> 00:54:24,240 remember arrays are indexed at arrays 1276 00:54:24,240 --> 00:54:26,240 are zero indexed 1277 00:54:26,240 --> 00:54:27,880 and then we're going to reference ID and 1278 00:54:27,880 --> 00:54:29,160 just add one I know this is kind of like 1279 00:54:29,160 --> 00:54:30,920 a hacky way to do it but I just want I'm 1280 00:54:30,920 --> 00:54:33,440 just doing it just for a very simple 1281 00:54:33,440 --> 00:54:35,880 example and then what I'll do is I'm 1282 00:54:35,880 --> 00:54:37,440 just going to 1283 00:54:37,440 --> 00:54:40,599 sign uh or I'm going to destructure the 1284 00:54:40,599 --> 00:54:41,599 request 1285 00:54:41,599 --> 00:54:44,160 body so I'm going to destructure body 1286 00:54:44,160 --> 00:54:46,440 from the request object and then I'll 1287 00:54:46,440 --> 00:54:49,520 just simply use the spreader operator on 1288 00:54:49,520 --> 00:54:53,040 the body object to take all the fields 1289 00:54:53,040 --> 00:54:56,079 from the body object and unpack it into 1290 00:54:56,079 --> 00:54:58,599 this new object that I am 1291 00:54:58,599 --> 00:55:02,000 creating that is assigned to new user 1292 00:55:02,000 --> 00:55:04,119 and then I'm just then going 1293 00:55:04,119 --> 00:55:08,799 to uh reference mock users. push new 1294 00:55:08,799 --> 00:55:12,520 user and then I'm going to go and just 1295 00:55:12,520 --> 00:55:15,160 return the new user and remember we want 1296 00:55:15,160 --> 00:55:17,760 to send back a status code of 1297 00:55:17,760 --> 00:55:20,880 200 or I'm sorry 2011 because if I were 1298 00:55:20,880 --> 00:55:22,359 to send this right 1299 00:55:22,359 --> 00:55:24,640 now you you can see that I do get back 1300 00:55:24,640 --> 00:55:26,400 the user but it sends back sends me back 1301 00:55:26,400 --> 00:55:28,839 a 200 and for good practice you want to 1302 00:55:28,839 --> 00:55:31,400 make sure the post request sends a 2011 1303 00:55:31,400 --> 00:55:34,720 so I can just set response. status call 1304 00:55:34,720 --> 00:55:37,280 the status method and pass into a one 1305 00:55:37,280 --> 00:55:40,599 and then call do send so we did this in 1306 00:55:40,599 --> 00:55:42,680 an earlier part of the tutorial where we 1307 00:55:42,680 --> 00:55:45,880 were making get requests right over here 1308 00:55:45,880 --> 00:55:48,319 okay so let's go ahead 1309 00:55:48,319 --> 00:55:52,200 and test this out click Send all right 1310 00:55:52,200 --> 00:55:53,920 so you can see whenever I click Send it 1311 00:55:53,920 --> 00:55:56,640 will just keep creating a new user and 1312 00:55:56,640 --> 00:55:59,559 it'll send it back to uh the client 1313 00:55:59,559 --> 00:56:02,280 which is this uh Thunder client as a 1314 00:56:02,280 --> 00:56:05,319 response and it has the ID Auto 1315 00:56:05,319 --> 00:56:08,559 incremented so that's pretty 1316 00:56:10,520 --> 00:56:13,240 cool all right so aside from get 1317 00:56:13,240 --> 00:56:15,640 requests and post requests there are 1318 00:56:15,640 --> 00:56:19,079 also a bunch of other HTTP request 1319 00:56:19,079 --> 00:56:22,119 methods that you can use to handle in 1320 00:56:22,119 --> 00:56:24,480 your Express API however you don't need 1321 00:56:24,480 --> 00:56:27,359 to know all of them but there are three 1322 00:56:27,359 --> 00:56:29,200 others that I think is worth knowing 1323 00:56:29,200 --> 00:56:31,160 about okay because you will be seeing 1324 00:56:31,160 --> 00:56:34,079 them everywhere in documentation when 1325 00:56:34,079 --> 00:56:35,799 you're working with apis and you 1326 00:56:35,799 --> 00:56:38,160 yourself will need to use as well so 1327 00:56:38,160 --> 00:56:41,720 let's talk about it so the other three 1328 00:56:41,720 --> 00:56:44,559 are the put request patch request and 1329 00:56:44,559 --> 00:56:47,760 delete request methods okay put and 1330 00:56:47,760 --> 00:56:50,520 Patch request both are used to update 1331 00:56:50,520 --> 00:56:53,839 data but they are technically different 1332 00:56:53,839 --> 00:56:57,200 on how you update data so to better 1333 00:56:57,200 --> 00:56:58,760 understand put requests let's first talk 1334 00:56:58,760 --> 00:57:01,280 about patch request first so let's say 1335 00:57:01,280 --> 00:57:04,119 for example you want to update some data 1336 00:57:04,119 --> 00:57:07,280 on the backend using our users example 1337 00:57:07,280 --> 00:57:09,079 let's say I want to update one of my 1338 00:57:09,079 --> 00:57:11,359 users uh username so maybe I want to 1339 00:57:11,359 --> 00:57:15,319 change my username from anen to Anon 123 1340 00:57:15,319 --> 00:57:18,680 I would do that using a patch request 1341 00:57:18,680 --> 00:57:22,880 okay a patch request updates a record 1342 00:57:22,880 --> 00:57:25,400 but it updates it partially and what 1343 00:57:25,400 --> 00:57:27,000 that means is you're not updating the 1344 00:57:27,000 --> 00:57:29,559 entire user itself you're only updating 1345 00:57:29,559 --> 00:57:32,480 a partial field you're only updating a 1346 00:57:32,480 --> 00:57:35,240 portion of that entire user record so 1347 00:57:35,240 --> 00:57:37,200 instead of updating everything of that 1348 00:57:37,200 --> 00:57:38,680 user record you're only updating 1349 00:57:38,680 --> 00:57:40,960 username you're not updating username 1350 00:57:40,960 --> 00:57:44,400 and display name okay so hopefully that 1351 00:57:44,400 --> 00:57:47,520 part makes sense now with put request 1352 00:57:47,520 --> 00:57:49,480 you're not actually just updating a 1353 00:57:49,480 --> 00:57:52,319 partial entity of that record you're 1354 00:57:52,319 --> 00:57:55,319 updating the entire resource 1355 00:57:55,319 --> 00:57:57,960 okay so whenever you make a request to 1356 00:57:57,960 --> 00:58:00,720 update something on the database using a 1357 00:58:00,720 --> 00:58:03,359 put request you're including every 1358 00:58:03,359 --> 00:58:05,720 single field in that request body even 1359 00:58:05,720 --> 00:58:08,079 if not updating it because if you don't 1360 00:58:08,079 --> 00:58:09,960 include that field then those fields 1361 00:58:09,960 --> 00:58:12,200 will pretty much be removed or if you're 1362 00:58:12,200 --> 00:58:14,440 using like a SQL database those fields 1363 00:58:14,440 --> 00:58:16,599 will be null so the next time you fetch 1364 00:58:16,599 --> 00:58:19,000 the data those fields will always be 1365 00:58:19,000 --> 00:58:21,119 null of course if you're using mongodb 1366 00:58:21,119 --> 00:58:23,559 they'll just be updated in the document 1367 00:58:23,559 --> 00:58:26,039 and they won't even appear at all so 1368 00:58:26,039 --> 00:58:28,079 hopefully that makes sense so so think 1369 00:58:28,079 --> 00:58:30,200 of it like this put is for updating the 1370 00:58:30,200 --> 00:58:33,839 entire record okay so even if you only 1371 00:58:33,839 --> 00:58:36,440 care about updating username but you 1372 00:58:36,440 --> 00:58:38,599 don't want to update display name then 1373 00:58:38,599 --> 00:58:40,200 what you need to do is when you update 1374 00:58:40,200 --> 00:58:41,880 the username to whatever it is that you 1375 00:58:41,880 --> 00:58:43,520 want you need to make sure that you 1376 00:58:43,520 --> 00:58:46,400 include the current value of whatever 1377 00:58:46,400 --> 00:58:49,720 display name is otherwise it will be 1378 00:58:49,720 --> 00:58:52,760 overridden with patch you can pretty 1379 00:58:52,760 --> 00:58:55,520 much just update only a portion of that 1380 00:58:55,520 --> 00:58:57,680 user record so you don't need to include 1381 00:58:57,680 --> 00:58:58,799 anything that you're not trying to 1382 00:58:58,799 --> 00:59:00,799 update so if you update only the display 1383 00:59:00,799 --> 00:59:03,039 name you only include the display name 1384 00:59:03,039 --> 00:59:05,480 in the request body you don't need to 1385 00:59:05,480 --> 00:59:07,839 include the username and if you only 1386 00:59:07,839 --> 00:59:09,160 want to update the username then you 1387 00:59:09,160 --> 00:59:11,680 don't include the display name so 1388 00:59:11,680 --> 00:59:13,359 hopefully that helps you better 1389 00:59:13,359 --> 00:59:14,559 understand the difference between put 1390 00:59:14,559 --> 00:59:16,400 and Patch delete is pretty 1391 00:59:16,400 --> 00:59:18,839 straightforward it's used to pretty much 1392 00:59:18,839 --> 00:59:21,799 just delete records from the database 1393 00:59:21,799 --> 00:59:23,760 okay so you only really use it if you 1394 00:59:23,760 --> 00:59:25,200 need to delete a resource SCE whether 1395 00:59:25,200 --> 00:59:27,160 you're deleting a user or if you're 1396 00:59:27,160 --> 00:59:30,319 deleting a product or an order whatever 1397 00:59:30,319 --> 00:59:33,920 it is okay so hopefully that explanation 1398 00:59:33,920 --> 00:59:36,520 makes sense so let's go ahead and set up 1399 00:59:36,520 --> 00:59:39,200 a put request for updating a user by its 1400 00:59:39,200 --> 00:59:41,480 ID so we're going to reference app and 1401 00:59:41,480 --> 00:59:44,640 call the put method and for the path I'm 1402 00:59:44,640 --> 00:59:46,599 going to reuse the path that we used 1403 00:59:46,599 --> 00:59:50,039 earlier for our get users by ID request 1404 00:59:50,039 --> 00:59:52,599 so I'll just copy this up top over here 1405 00:59:52,599 --> 00:59:55,119 and paste this over here and like I said 1406 00:59:55,119 --> 00:59:57,599 before we can use the same path but as 1407 00:59:57,599 --> 00:59:59,079 long as we have different request 1408 00:59:59,079 --> 01:00:03,480 methods it will still work fine okay and 1409 01:00:03,480 --> 01:00:06,400 we'll now pass in the request Handler 1410 01:00:06,400 --> 01:00:09,200 function as a second argument in the put 1411 01:00:09,200 --> 01:00:12,599 method call so now we also need to make 1412 01:00:12,599 --> 01:00:15,480 sure we are uh grabbing the route 1413 01:00:15,480 --> 01:00:18,119 parameter from the request body or from 1414 01:00:18,119 --> 01:00:20,359 the request object and we also want to 1415 01:00:20,359 --> 01:00:22,799 make sure we're grabbing the request 1416 01:00:22,799 --> 01:00:25,119 body as well from the request object so 1417 01:00:25,119 --> 01:00:26,440 let's do some 1418 01:00:26,440 --> 01:00:30,400 destructuring so I'll go ahead and from 1419 01:00:30,400 --> 01:00:31,599 the 1420 01:00:31,599 --> 01:00:34,160 request object I will go ahead and the 1421 01:00:34,160 --> 01:00:36,119 structure body so I'm going to get the 1422 01:00:36,119 --> 01:00:37,799 request body because we need that 1423 01:00:37,799 --> 01:00:40,559 because that is what is to contain the 1424 01:00:40,559 --> 01:00:42,480 data that we're using to update the 1425 01:00:42,480 --> 01:00:46,559 current user object and I'm also going 1426 01:00:46,559 --> 01:00:50,119 to destructure the prams object and then 1427 01:00:50,119 --> 01:00:52,039 um from the primes object right over 1428 01:00:52,039 --> 01:00:54,359 here I'm going to destructure ID just 1429 01:00:54,359 --> 01:00:56,799 like like that now let's go ahead and 1430 01:00:56,799 --> 01:00:59,280 parse the ID uh convert it into an 1431 01:00:59,280 --> 01:01:01,839 integer and check to see if is not a 1432 01:01:01,839 --> 01:01:03,760 number so that way we ensure that we're 1433 01:01:03,760 --> 01:01:06,599 only passing in numeric strings and then 1434 01:01:06,599 --> 01:01:11,039 we can convert them accordingly 1435 01:01:11,039 --> 01:01:16,960 so const par ID equals parse ins ID just 1436 01:01:16,960 --> 01:01:20,240 like that and then we'll use the uh is 1437 01:01:20,240 --> 01:01:23,119 not a number function to check if par ID 1438 01:01:23,119 --> 01:01:26,400 is not a number so if this condition is 1439 01:01:26,400 --> 01:01:29,319 true if par ID is not a number then we 1440 01:01:29,319 --> 01:01:31,000 want to 1441 01:01:31,000 --> 01:01:32,720 return 1442 01:01:32,720 --> 01:01:36,799 response. send status and we'll just do 1443 01:01:36,799 --> 01:01:38,760 400 which just means 1444 01:01:38,760 --> 01:01:42,400 invalid um bad 1445 01:01:42,400 --> 01:01:45,400 request okay 1446 01:01:45,400 --> 01:01:49,240 uh and now we can 1447 01:01:49,240 --> 01:01:51,960 continue so what we'll do next is we 1448 01:01:51,960 --> 01:01:55,000 want to find the user that we're trying 1449 01:01:55,000 --> 01:01:57,359 to update but we want to get the user's 1450 01:01:57,359 --> 01:01:58,920 index though we don't actually need to 1451 01:01:58,920 --> 01:02:01,200 get the user object just the user index 1452 01:02:01,200 --> 01:02:03,000 and even with the index itself we can 1453 01:02:03,000 --> 01:02:06,119 use the index to um retrieve the user by 1454 01:02:06,119 --> 01:02:08,359 referencing it using the square bracket 1455 01:02:08,359 --> 01:02:10,599 operator on the array itself so I'll 1456 01:02:10,599 --> 01:02:12,520 show you what I mean by that so first 1457 01:02:12,520 --> 01:02:15,599 let's grab the index uh of where the 1458 01:02:15,599 --> 01:02:19,480 user is located so I'll call this const 1459 01:02:19,480 --> 01:02:21,880 um find user 1460 01:02:21,880 --> 01:02:24,400 index and then we'll reference mock user 1461 01:02:24,400 --> 01:02:27,880 users and I'll call find 1462 01:02:27,880 --> 01:02:31,480 index so now we'll pass in a predicate 1463 01:02:31,480 --> 01:02:34,240 function and we want to search for the 1464 01:02:34,240 --> 01:02:37,559 user by its ID so I'm going to reference 1465 01:02:37,559 --> 01:02:39,520 user which is this argument in the 1466 01:02:39,520 --> 01:02:42,359 predicate callback function and then 1467 01:02:42,359 --> 01:02:44,200 we'll search for the user byes ID so 1468 01:02:44,200 --> 01:02:49,520 user. ID is equal to par ID okay not ID 1469 01:02:49,520 --> 01:02:51,880 but par ID because we just converted it 1470 01:02:51,880 --> 01:02:54,039 from a numeric string into an actual 1471 01:02:54,039 --> 01:02:56,359 string 1472 01:02:57,079 --> 01:02:58,520 so 1473 01:02:58,520 --> 01:03:01,200 now what we'll do is we do need to check 1474 01:03:01,200 --> 01:03:03,760 to make sure that the the index is not 1475 01:03:03,760 --> 01:03:06,480 negative 1 because if we actually don't 1476 01:03:06,480 --> 01:03:08,960 find this user if this predicate returns 1477 01:03:08,960 --> 01:03:11,480 false then that means the return value 1478 01:03:11,480 --> 01:03:13,480 of find index is actually negative -1 1479 01:03:13,480 --> 01:03:15,559 which means that that user is not found 1480 01:03:15,559 --> 01:03:18,720 by its ID so we'll check if find user 1481 01:03:18,720 --> 01:03:20,480 index is equal 1482 01:03:20,480 --> 01:03:25,559 to1 and we'll return a stat status code 1483 01:03:25,559 --> 01:03:30,559 of 404 so response. send status 404 if 1484 01:03:30,559 --> 01:03:32,520 find user index is equal 1485 01:03:32,520 --> 01:03:35,440 to1 so if find user index is not 1486 01:03:35,440 --> 01:03:37,599 negative 1 that means we actually were 1487 01:03:37,599 --> 01:03:41,359 able to get the index of the user based 1488 01:03:41,359 --> 01:03:43,760 on the predicate function right over 1489 01:03:43,760 --> 01:03:46,920 here so that means we can use find user 1490 01:03:46,920 --> 01:03:48,599 index to access the user that we're 1491 01:03:48,599 --> 01:03:50,960 trying to update okay so hopefully that 1492 01:03:50,960 --> 01:03:53,279 makes sense now let's go ahead and do 1493 01:03:53,279 --> 01:03:59,880 this let's go ahead and reference mock 1494 01:03:59,880 --> 01:04:02,720 users and then we'll use the square 1495 01:04:02,720 --> 01:04:05,079 bracket operator and pass in fine user 1496 01:04:05,079 --> 01:04:07,480 index in between the square brackets 1497 01:04:07,480 --> 01:04:09,440 just like this so this allows me to 1498 01:04:09,440 --> 01:04:11,279 access the element at the mock users 1499 01:04:11,279 --> 01:04:14,400 array by the by its index and remember 1500 01:04:14,400 --> 01:04:16,960 we're updating this entire user 1501 01:04:16,960 --> 01:04:21,119 object so I can just simply assign this 1502 01:04:21,119 --> 01:04:24,440 user object to whatever this object is 1503 01:04:24,440 --> 01:04:25,920 now keep in mind that since we are using 1504 01:04:25,920 --> 01:04:28,480 a put request remember we are updating 1505 01:04:28,480 --> 01:04:31,839 the entire um object itself we're not 1506 01:04:31,839 --> 01:04:33,839 updating only one or two Fields we're 1507 01:04:33,839 --> 01:04:37,039 updating the entire thing okay and we're 1508 01:04:37,039 --> 01:04:38,720 updating the entire thing based on 1509 01:04:38,720 --> 01:04:41,319 whatever the request body is so let's 1510 01:04:41,319 --> 01:04:42,920 say for example if the request body is 1511 01:04:42,920 --> 01:04:45,200 missing certain properties but those 1512 01:04:45,200 --> 01:04:47,279 properties are currently existing on 1513 01:04:47,279 --> 01:04:49,400 that user we're trying to update then 1514 01:04:49,400 --> 01:04:51,200 that means those properties will no 1515 01:04:51,200 --> 01:04:54,599 longer be on the user once we update it 1516 01:04:54,599 --> 01:04:56,039 but there are some properties though 1517 01:04:56,039 --> 01:04:59,079 that you never will update at all so for 1518 01:04:59,079 --> 01:05:01,279 example if you're using a database and 1519 01:05:01,279 --> 01:05:03,880 once we do get to it you'll see that 1520 01:05:03,880 --> 01:05:05,760 we're never going to actually 1521 01:05:05,760 --> 01:05:08,760 modify um the ID at all because the ID 1522 01:05:08,760 --> 01:05:09,520 is 1523 01:05:09,520 --> 01:05:13,279 autogenerated by the database server so 1524 01:05:13,279 --> 01:05:15,720 let's leave the ID alone so I'll keep 1525 01:05:15,720 --> 01:05:20,760 the ID as pars ID just like this but 1526 01:05:20,760 --> 01:05:22,680 everything else all the other fields 1527 01:05:22,680 --> 01:05:25,440 will come from the request body object 1528 01:05:25,440 --> 01:05:27,839 which is this body object right over 1529 01:05:27,839 --> 01:05:30,680 here so I'm going to just destructure 1530 01:05:30,680 --> 01:05:32,839 body so that will take all of the field 1531 01:05:32,839 --> 01:05:35,559 all the fields from the body object 1532 01:05:35,559 --> 01:05:37,119 unpack it and put it into this new 1533 01:05:37,119 --> 01:05:40,160 object right over here okay and since we 1534 01:05:40,160 --> 01:05:44,039 are not going to be passing in the ID in 1535 01:05:44,039 --> 01:05:46,760 the request body that's also okay as 1536 01:05:46,760 --> 01:05:50,880 well okay so what we just did was we 1537 01:05:50,880 --> 01:05:53,119 kept the ID the same and whatever the 1538 01:05:53,119 --> 01:05:56,880 user passed in the request body is going 1539 01:05:56,880 --> 01:06:01,839 to be used to update the user okay so if 1540 01:06:01,839 --> 01:06:03,920 there were values that we did not 1541 01:06:03,920 --> 01:06:05,720 include in a request body well they are 1542 01:06:05,720 --> 01:06:07,799 now gone assuming that the user had 1543 01:06:07,799 --> 01:06:10,920 those values defined so let's go ahead 1544 01:06:10,920 --> 01:06:13,240 and finally return a 1545 01:06:13,240 --> 01:06:16,160 response um let's send a status code 1546 01:06:16,160 --> 01:06:18,359 send 1547 01:06:18,359 --> 01:06:21,440 status of let's just do 200 you can send 1548 01:06:21,440 --> 01:06:22,400 200 or 1549 01:06:22,400 --> 01:06:25,240 204 but I'll just keep it simple and 1550 01:06:25,240 --> 01:06:26,839 send 1551 01:06:26,839 --> 01:06:32,400 200 and now let's go ahead and click new 1552 01:06:32,400 --> 01:06:35,920 request let's select put and let's 1553 01:06:35,920 --> 01:06:40,079 change this to Local Host Port 3000 API 1554 01:06:40,079 --> 01:06:41,520 users and now we're going to go ahead 1555 01:06:41,520 --> 01:06:44,160 and update the user using a put request 1556 01:06:44,160 --> 01:06:46,640 by the ID So currently we only have 1557 01:06:46,640 --> 01:06:50,000 seven users let's update uh let's update 1558 01:06:50,000 --> 01:06:53,440 Jack so the ID is two so for the route 1559 01:06:53,440 --> 01:06:54,760 parameter we're we're going to pass to 1560 01:06:54,760 --> 01:06:56,559 and remember make sure you have put 1561 01:06:56,559 --> 01:06:59,160 request selected we're going to go ahead 1562 01:06:59,160 --> 01:07:02,599 and select select the body Tab and let's 1563 01:07:02,599 --> 01:07:07,119 pass a request payload or request body 1564 01:07:07,119 --> 01:07:11,359 so remember this okay we are trying to 1565 01:07:11,359 --> 01:07:15,359 update Jack this user object that has 1566 01:07:15,359 --> 01:07:17,440 the ID of two username Jack and display 1567 01:07:17,440 --> 01:07:18,279 name 1568 01:07:18,279 --> 01:07:21,039 Jack if I want to only 1569 01:07:21,039 --> 01:07:24,440 update uh let's say the username but I 1570 01:07:24,440 --> 01:07:26,720 don't want to update display name with a 1571 01:07:26,720 --> 01:07:30,839 put request then I must include all of 1572 01:07:30,839 --> 01:07:35,799 the uh current values as well okay so 1573 01:07:35,799 --> 01:07:36,880 for 1574 01:07:36,880 --> 01:07:40,359 example let's update uh username so 1575 01:07:40,359 --> 01:07:43,119 let's update it from Jack to 1576 01:07:43,119 --> 01:07:45,559 Jackie or 1577 01:07:45,559 --> 01:07:49,559 Jackson and let's keep the display 1578 01:07:49,559 --> 01:07:53,680 name as Jack so we'll assume that we're 1579 01:07:53,680 --> 01:07:56,039 using the same value which we are click 1580 01:07:56,039 --> 01:07:58,599 Send and you can see we get a 200 okay 1581 01:07:58,599 --> 01:08:01,000 let's go ahead and make a get request so 1582 01:08:01,000 --> 01:08:02,359 let's do new 1583 01:08:02,359 --> 01:08:06,119 request get the user by ID of two you 1584 01:08:06,119 --> 01:08:08,920 can see now we updated the username but 1585 01:08:08,920 --> 01:08:11,440 we also did override the display name 1586 01:08:11,440 --> 01:08:13,160 because we passed that in as the request 1587 01:08:13,160 --> 01:08:14,599 object we passed it in the request 1588 01:08:14,599 --> 01:08:17,439 object as well but now watch this if I 1589 01:08:17,439 --> 01:08:20,399 omit display name and then I click 1590 01:08:20,399 --> 01:08:24,080 Send now watch this when I request the 1591 01:08:24,080 --> 01:08:27,040 user by ID of two you can see that it 1592 01:08:27,040 --> 01:08:29,479 only gives me uh it gives me the correct 1593 01:08:29,479 --> 01:08:33,439 user but we only have username now okay 1594 01:08:33,439 --> 01:08:35,439 that's because we omitted the display 1595 01:08:35,439 --> 01:08:38,479 name and so that just pretty much gets 1596 01:08:38,479 --> 01:08:39,279 uh 1597 01:08:39,279 --> 01:08:41,040 removed okay it's pretty much just 1598 01:08:41,040 --> 01:08:43,238 taking the request body and using that 1599 01:08:43,238 --> 01:08:45,399 request body to update the entire user 1600 01:08:45,399 --> 01:08:47,600 object as a whole of course we're never 1601 01:08:47,600 --> 01:08:49,799 updating the ID as well now watch this 1602 01:08:49,799 --> 01:08:53,238 if I remove username and I click Send 1603 01:08:53,238 --> 01:08:56,439 and if I try to to get the user of id2 1604 01:08:56,439 --> 01:08:58,359 it just gives me this object with only 1605 01:08:58,359 --> 01:09:00,600 the ID field so hopefully you're 1606 01:09:00,600 --> 01:09:02,880 starting to understand what exactly the 1607 01:09:02,880 --> 01:09:05,319 put request is now of course if this is 1608 01:09:05,319 --> 01:09:06,640 not what you're trying to do and you 1609 01:09:06,640 --> 01:09:09,238 only want to update a 1610 01:09:09,238 --> 01:09:12,479 partial uh the a a partial part of the 1611 01:09:12,479 --> 01:09:14,359 user object so you only want to update 1612 01:09:14,359 --> 01:09:16,759 the username field without having to uh 1613 01:09:16,759 --> 01:09:18,719 worrying about passing in the display 1614 01:09:18,719 --> 01:09:21,960 name or other fields because your user 1615 01:09:21,960 --> 01:09:24,520 field can have your user can have a lot 1616 01:09:24,520 --> 01:09:26,158 of different fields and it can be a 1617 01:09:26,158 --> 01:09:28,560 nuisance to pass all of those fields in 1618 01:09:28,560 --> 01:09:31,279 the request body so this is where patch 1619 01:09:31,279 --> 01:09:34,640 comes in okay so we'll work on the patch 1620 01:09:34,640 --> 01:09:36,759 request next but I just wanted to show 1621 01:09:36,759 --> 01:09:39,238 you this as well if I were to just pass 1622 01:09:39,238 --> 01:09:42,520 in a invalid ID it's going to give me a 1623 01:09:42,520 --> 01:09:47,359 bad request if I pass in a valid numeric 1624 01:09:47,799 --> 01:09:51,040 value but that ID did not exist in the 1625 01:09:51,040 --> 01:09:53,238 array or that user cannot be found in 1626 01:09:53,238 --> 01:09:55,159 the array it's going to give me four for 1627 01:09:55,159 --> 01:09:57,280 not found so that is 1628 01:09:57,280 --> 01:09:58,840 good 1629 01:09:58,840 --> 01:10:03,400 okay cool yeah so hopefully that makes 1630 01:10:05,760 --> 01:10:08,679 sense all right so let's go ahead and 1631 01:10:08,679 --> 01:10:12,040 set up a Pat request so patch request 1632 01:10:12,040 --> 01:10:13,760 basically allows us to 1633 01:10:13,760 --> 01:10:18,159 update an entity or a resource or a 1634 01:10:18,159 --> 01:10:20,719 record whatever you want to call it 1635 01:10:20,719 --> 01:10:24,120 partially okay so in the put request 1636 01:10:24,120 --> 01:10:26,600 example we are updating the entire 1637 01:10:26,600 --> 01:10:29,120 resource based on whatever we provide in 1638 01:10:29,120 --> 01:10:30,960 the request body so everything just gets 1639 01:10:30,960 --> 01:10:32,239 updated as a 1640 01:10:32,239 --> 01:10:34,880 whole for the pass request we only want 1641 01:10:34,880 --> 01:10:38,600 to be able to update either one or two 1642 01:10:38,600 --> 01:10:41,840 or just a partial amount of fields 1643 01:10:41,840 --> 01:10:44,480 without having to include every single 1644 01:10:44,480 --> 01:10:46,920 field in the record that we're trying to 1645 01:10:46,920 --> 01:10:49,040 update okay because it can become 1646 01:10:49,040 --> 01:10:51,800 annoying if you have let's say a user 1647 01:10:51,800 --> 01:10:53,159 with 10 different fields and you don't 1648 01:10:53,159 --> 01:10:55,920 want to include that all the time so 1649 01:10:55,920 --> 01:10:57,080 let's go ahead and set up a patch 1650 01:10:57,080 --> 01:11:02,520 request so app. patch SL API users so 1651 01:11:02,520 --> 01:11:03,960 we're going to reuse the same path of 1652 01:11:03,960 --> 01:11:06,440 course because we can since we're using 1653 01:11:06,440 --> 01:11:09,040 a different request method and then 1654 01:11:09,040 --> 01:11:12,120 let's go ahead and pass in the request 1655 01:11:12,120 --> 01:11:16,000 Handler function in the as a second 1656 01:11:16,000 --> 01:11:19,400 argument for the patch call and since 1657 01:11:19,400 --> 01:11:21,239 we're going to do the same 1658 01:11:21,239 --> 01:11:25,800 thing um with the check for the ID for 1659 01:11:25,800 --> 01:11:26,880 the route 1660 01:11:26,880 --> 01:11:29,600 ID sorry the route parameter ID I'm just 1661 01:11:29,600 --> 01:11:32,040 going to go ahead and copy um actually 1662 01:11:32,040 --> 01:11:33,640 most of the stuff up over here but I 1663 01:11:33,640 --> 01:11:35,000 will explain again for those of you who 1664 01:11:35,000 --> 01:11:37,760 are watching just the segmented part of 1665 01:11:37,760 --> 01:11:40,159 the entire expressjs tutorial cuz I have 1666 01:11:40,159 --> 01:11:42,760 this in a one hour about like a 1 hour 1667 01:11:42,760 --> 01:11:44,600 to one and a half hour long video and 1668 01:11:44,600 --> 01:11:46,800 also in its own individual videos as 1669 01:11:46,800 --> 01:11:49,560 well so in case you uh are not watching 1670 01:11:49,560 --> 01:11:52,600 the entire tutorial Series so what we're 1671 01:11:52,600 --> 01:11:55,320 doing here is is we are destructuring 1672 01:11:55,320 --> 01:11:57,360 the request body object as well as the 1673 01:11:57,360 --> 01:11:59,239 route parameter object and then from the 1674 01:11:59,239 --> 01:12:00,840 route parameter object I'm destructuring 1675 01:12:00,840 --> 01:12:05,080 the ID all from the request object okay 1676 01:12:05,080 --> 01:12:08,760 then I'm going to parse the ID so I'm 1677 01:12:08,760 --> 01:12:10,440 basically taking the ID which I'm 1678 01:12:10,440 --> 01:12:13,199 expecting it to be a numeric value so I 1679 01:12:13,199 --> 01:12:16,239 use the parse int function to ensure 1680 01:12:16,239 --> 01:12:19,600 that when I passing the ID the return 1681 01:12:19,600 --> 01:12:22,719 value of pars int or of par int with 1682 01:12:22,719 --> 01:12:26,600 that ID gives me a valid numeric value 1683 01:12:26,600 --> 01:12:29,040 so we use the is Nan function to check 1684 01:12:29,040 --> 01:12:32,080 to make sure that parse ID was parsed 1685 01:12:32,080 --> 01:12:34,880 correctly and is not a number because if 1686 01:12:34,880 --> 01:12:38,320 you were to pass in a string let's say 1687 01:12:38,320 --> 01:12:41,199 you know just random name instead of a 1688 01:12:41,199 --> 01:12:44,880 number then parse ID would resolve into 1689 01:12:44,880 --> 01:12:47,040 Nan which is not a number so we would 1690 01:12:47,040 --> 01:12:50,120 return response. send status then what 1691 01:12:50,120 --> 01:12:51,719 we're doing over here on this line which 1692 01:12:51,719 --> 01:12:53,239 is identical to what we did up here in 1693 01:12:53,239 --> 01:12:55,440 the put request is we're just simply 1694 01:12:55,440 --> 01:12:57,480 searching for the user in the mock users 1695 01:12:57,480 --> 01:13:00,800 array based on the ID and that's where 1696 01:13:00,800 --> 01:13:03,560 this whole line comes in we pretty much 1697 01:13:03,560 --> 01:13:06,679 just check if user. ID is equal to parse 1698 01:13:06,679 --> 01:13:12,040 ID okay and additionally if uh find user 1699 01:13:12,040 --> 01:13:14,679 index if its value is negative one that 1700 01:13:14,679 --> 01:13:17,600 means find index the method call uh was 1701 01:13:17,600 --> 01:13:22,480 not able to find that user um by the ID 1702 01:13:22,480 --> 01:13:24,159 so that predicate failed so it was not 1703 01:13:24,159 --> 01:13:26,000 able to find the user so it returns 1704 01:13:26,000 --> 01:13:29,080 negative 1 but if it returns anything 1705 01:13:29,080 --> 01:13:31,080 but negative 1 then that means it was 1706 01:13:31,080 --> 01:13:33,040 able to find the user so hopefully that 1707 01:13:33,040 --> 01:13:34,600 makes sense so we do this check right 1708 01:13:34,600 --> 01:13:38,560 over here if find user index equals -1 1709 01:13:38,560 --> 01:13:40,880 then we return a status code of 404 1710 01:13:40,880 --> 01:13:42,120 because that means the user was not 1711 01:13:42,120 --> 01:13:44,159 found so hopefully that makes sense I 1712 01:13:44,159 --> 01:13:45,320 didn't want to have to rewrite that 1713 01:13:45,320 --> 01:13:47,480 whole thing again but I hope it is 1714 01:13:47,480 --> 01:13:49,400 straightforward so and if it is a little 1715 01:13:49,400 --> 01:13:51,000 bit confusing just re-watch the previous 1716 01:13:51,000 --> 01:13:53,440 video on the put request so that that 1717 01:13:53,440 --> 01:13:55,080 way you understand but that I only did 1718 01:13:55,080 --> 01:13:56,120 that for people who are watching these 1719 01:13:56,120 --> 01:13:58,800 segments AP part okay so now the major 1720 01:13:58,800 --> 01:14:00,920 difference here is the way that we 1721 01:14:00,920 --> 01:14:03,400 update the user record in the pull 1722 01:14:03,400 --> 01:14:05,880 request we updated the entire thing you 1723 01:14:05,880 --> 01:14:07,159 can see the only thing that we did not 1724 01:14:07,159 --> 01:14:09,639 update of course was the ID but we took 1725 01:14:09,639 --> 01:14:11,920 the entire request body and we pretty 1726 01:14:11,920 --> 01:14:14,679 much just uh put it into took all the 1727 01:14:14,679 --> 01:14:16,360 field values and put it into this new 1728 01:14:16,360 --> 01:14:18,679 object and took that object and assigned 1729 01:14:18,679 --> 01:14:22,320 it to well we override we overrided 1730 01:14:22,320 --> 01:14:24,440 current existing user 1731 01:14:24,440 --> 01:14:27,159 okay in the patch request we're not 1732 01:14:27,159 --> 01:14:30,400 going to override every single field so 1733 01:14:30,400 --> 01:14:32,440 the body itself the request body might 1734 01:14:32,440 --> 01:14:34,679 only contain let's say one field that 1735 01:14:34,679 --> 01:14:37,800 we're trying to update so any current 1736 01:14:37,800 --> 01:14:39,920 field values that are in that user 1737 01:14:39,920 --> 01:14:42,920 record must not be touched at all so 1738 01:14:42,920 --> 01:14:45,040 they must stay the same so the way we 1739 01:14:45,040 --> 01:14:46,960 can do that is first let's reference 1740 01:14:46,960 --> 01:14:49,520 mock users and pass in the square 1741 01:14:49,520 --> 01:14:51,760 brackets find user index so that way 1742 01:14:51,760 --> 01:14:53,040 this is the user that we're trying to 1743 01:14:53,040 --> 01:14:56,360 update date and what we're going to do 1744 01:14:56,360 --> 01:14:57,360 is 1745 01:14:57,360 --> 01:15:01,000 this we're going to go ahead and take 1746 01:15:01,000 --> 01:15:03,440 we're going to copy this mock users uh 1747 01:15:03,440 --> 01:15:05,480 fine user index I'm going to reference 1748 01:15:05,480 --> 01:15:08,000 it I'm going to use the spreader 1749 01:15:08,000 --> 01:15:12,480 operator on this mock users reference at 1750 01:15:12,480 --> 01:15:15,800 fine user index okay so I'm going to 1751 01:15:15,800 --> 01:15:18,280 take all of the current field value 1752 01:15:18,280 --> 01:15:20,840 Pairs and put it into this new object so 1753 01:15:20,840 --> 01:15:22,440 all of the current values will be inside 1754 01:15:22,440 --> 01:15:25,040 this new object and then I am going to 1755 01:15:25,040 --> 01:15:28,840 take all of the field value pairs from 1756 01:15:28,840 --> 01:15:31,840 the request body use the spreader 1757 01:15:31,840 --> 01:15:34,880 operator on it and put it into this new 1758 01:15:34,880 --> 01:15:37,600 object so basically what I'm doing is 1759 01:15:37,600 --> 01:15:40,719 this I'm taking the existing user 1760 01:15:40,719 --> 01:15:42,239 currently that we're trying to update 1761 01:15:42,239 --> 01:15:45,239 taking all of its key value pairs 1762 01:15:45,239 --> 01:15:48,280 putting it into a new object then I want 1763 01:15:48,280 --> 01:15:51,560 to take the request body and all of its 1764 01:15:51,560 --> 01:15:53,440 key value pairs that we are using to 1765 01:15:53,440 --> 01:15:56,440 update the actual user and unpack it and 1766 01:15:56,440 --> 01:15:57,920 put it into this new object so that way 1767 01:15:57,920 --> 01:16:01,639 it will override those current values 1768 01:16:01,639 --> 01:16:05,280 okay so imagine if you had the current 1769 01:16:05,280 --> 01:16:07,440 user um let's see for example right over 1770 01:16:07,440 --> 01:16:09,920 here the current user is currently ID of 1771 01:16:09,920 --> 01:16:11,920 have three username atom display name 1772 01:16:11,920 --> 01:16:14,320 atom I'm taking all of this data right 1773 01:16:14,320 --> 01:16:16,639 over here putting it in that new object 1774 01:16:16,639 --> 01:16:19,719 and then the request body would uh have 1775 01:16:19,719 --> 01:16:21,600 whatever key value pairs that we were 1776 01:16:21,600 --> 01:16:24,560 send sending over to the server so if we 1777 01:16:24,560 --> 01:16:27,400 sent username it would 1778 01:16:27,400 --> 01:16:30,239 override uh it would use the request 1779 01:16:30,239 --> 01:16:32,960 body's username field and override it 1780 01:16:32,960 --> 01:16:35,199 with the current username field so Adam 1781 01:16:35,199 --> 01:16:36,600 would get overridden with whatever we 1782 01:16:36,600 --> 01:16:40,080 passed in for the request body okay and 1783 01:16:40,080 --> 01:16:41,679 if we didn't pass in a display name 1784 01:16:41,679 --> 01:16:44,480 that's okay because display name would 1785 01:16:44,480 --> 01:16:46,639 not be touched at all and it would stay 1786 01:16:46,639 --> 01:16:50,040 the same so hopefully that makes 1787 01:16:50,040 --> 01:16:55,080 sense and let's go ahead and just return 1788 01:16:55,199 --> 01:16:58,199 uh let's do send status of 1789 01:16:58,199 --> 01:17:01,800 200 because we can also send either 200 1790 01:17:01,800 --> 01:17:02,639 or 1791 01:17:02,639 --> 01:17:05,760 204 as a status code for patch as well 1792 01:17:05,760 --> 01:17:07,400 so let's actually try to use the patch 1793 01:17:07,400 --> 01:17:09,400 request so first let's grab all the 1794 01:17:09,400 --> 01:17:11,840 users let's make sure everything is 1795 01:17:11,840 --> 01:17:15,719 okay so I'll go ahead and update 1796 01:17:15,719 --> 01:17:17,880 Jack but this time we'll use a patch 1797 01:17:17,880 --> 01:17:19,800 request so I'll just change it from put 1798 01:17:19,800 --> 01:17:25,360 to patch uh the ID is going to be number 1799 01:17:25,360 --> 01:17:27,880 two okay so now watch this when I use 1800 01:17:27,880 --> 01:17:29,679 the patch request I'm going to update 1801 01:17:29,679 --> 01:17:32,000 the name the username from Jack to 1802 01:17:32,000 --> 01:17:35,239 Jackson okay we got a 200 okay status 1803 01:17:35,239 --> 01:17:37,920 now watch this when I make a get request 1804 01:17:37,920 --> 01:17:41,440 you'll see that username is updated and 1805 01:17:41,440 --> 01:17:43,600 display name stays the same now remember 1806 01:17:43,600 --> 01:17:45,960 in the put request if I don't include 1807 01:17:45,960 --> 01:17:48,679 the display name the display name will 1808 01:17:48,679 --> 01:17:52,120 be overridden okay with the patch 1809 01:17:52,120 --> 01:17:55,239 request I can only update what I just 1810 01:17:55,239 --> 01:17:57,320 want to update I don't have to include 1811 01:17:57,320 --> 01:17:59,719 display name and put a value for 1812 01:17:59,719 --> 01:18:03,400 it I can just update username and then 1813 01:18:03,400 --> 01:18:05,760 only username gets updated if I only 1814 01:18:05,760 --> 01:18:08,159 wanted to update display name let's do 1815 01:18:08,159 --> 01:18:13,040 display name to 1816 01:18:13,040 --> 01:18:15,639 Jackson I can just update the display 1817 01:18:15,639 --> 01:18:17,400 name if I want to update both so let's 1818 01:18:17,400 --> 01:18:18,840 update 1819 01:18:18,840 --> 01:18:21,920 both back to 1820 01:18:21,920 --> 01:18:26,120 Jack not password uh display 1821 01:18:26,120 --> 01:18:28,400 name 1822 01:18:28,400 --> 01:18:31,440 Jack Okay click Send and you can see now 1823 01:18:31,440 --> 01:18:34,120 both Fields get updated so hopefully 1824 01:18:34,120 --> 01:18:35,480 this helps you understand the difference 1825 01:18:35,480 --> 01:18:38,400 between put and Patch requests you use 1826 01:18:38,400 --> 01:18:41,080 both of them to update data but the way 1827 01:18:41,080 --> 01:18:44,040 that you update the data is different 1828 01:18:44,040 --> 01:18:47,000 remember put is used to update the 1829 01:18:47,000 --> 01:18:49,360 entire resource in our case we would use 1830 01:18:49,360 --> 01:18:51,880 put to update the entire user object 1831 01:18:51,880 --> 01:18:54,320 patch is used to update only certain 1832 01:18:54,320 --> 01:18:58,360 fields on that user object okay so 1833 01:18:58,360 --> 01:19:01,719 hopefully that makes 1834 01:19:05,600 --> 01:19:08,080 sense all right so now we will take a 1835 01:19:08,080 --> 01:19:11,480 look at the delete request method so 1836 01:19:11,480 --> 01:19:13,800 it's pretty easy to use delete and it's 1837 01:19:13,800 --> 01:19:15,600 pretty straightforward you just use it 1838 01:19:15,600 --> 01:19:18,239 to delete stuff on the backend server 1839 01:19:18,239 --> 01:19:19,639 and the backend server typically deletes 1840 01:19:19,639 --> 01:19:22,480 it from a data source like a SQL server 1841 01:19:22,480 --> 01:19:26,199 or mongod DB uh any general database so 1842 01:19:26,199 --> 01:19:28,239 what we'll do is we'll reference app and 1843 01:19:28,239 --> 01:19:30,120 call the delete method and I'm going to 1844 01:19:30,120 --> 01:19:32,120 reuse the same path because it doesn't 1845 01:19:32,120 --> 01:19:35,560 make sense to to not reuse it so now we 1846 01:19:35,560 --> 01:19:40,440 have/ API users slon ID for the route 1847 01:19:40,440 --> 01:19:42,639 parameter and we have this path 1848 01:19:42,639 --> 01:19:44,639 registered with a delete method we'll 1849 01:19:44,639 --> 01:19:47,600 pass in the request Handler callback 1850 01:19:47,600 --> 01:19:50,920 function now with delete requests you 1851 01:19:50,920 --> 01:19:53,400 actually typically don't need to pass 1852 01:19:53,400 --> 01:19:55,800 a request body you can if you need to 1853 01:19:55,800 --> 01:19:57,880 perhaps you might need to provide extra 1854 01:19:57,880 --> 01:20:00,440 data in the request body so just wanted 1855 01:20:00,440 --> 01:20:02,480 to point that out as well but typically 1856 01:20:02,480 --> 01:20:04,080 if you're just deleting stuff it's 1857 01:20:04,080 --> 01:20:05,440 pretty straightforward so you don't 1858 01:20:05,440 --> 01:20:07,600 really need to provide you know like a 1859 01:20:07,600 --> 01:20:09,199 payload but if there's other things that 1860 01:20:09,199 --> 01:20:11,280 you would like to do on the server side 1861 01:20:11,280 --> 01:20:13,880 and you it requires data in the payload 1862 01:20:13,880 --> 01:20:15,280 then you can definitely pass a request 1863 01:20:15,280 --> 01:20:17,159 body so we'll do the same thing we'll go 1864 01:20:17,159 --> 01:20:20,080 ahead and grab the route parameter so 1865 01:20:20,080 --> 01:20:21,520 I'm going to do the same thing that I 1866 01:20:21,520 --> 01:20:23,639 did earlier for the patch request and 1867 01:20:23,639 --> 01:20:24,719 the put 1868 01:20:24,719 --> 01:20:26,840 request I'll 1869 01:20:26,840 --> 01:20:29,120 simply just D 1870 01:20:29,120 --> 01:20:32,199 structure uh request. prams I'll 1871 01:20:32,199 --> 01:20:36,080 destructure the ID from request. prams 1872 01:20:36,080 --> 01:20:39,080 um actually I'll do it like this 1873 01:20:39,080 --> 01:20:42,440 prams ID just like that it looks more 1874 01:20:42,440 --> 01:20:44,000 cleaner and then what I'll do is I'll 1875 01:20:44,000 --> 01:20:47,480 parse the ID to make sure it is a valid 1876 01:20:47,480 --> 01:20:51,280 numeric ID so cons parse ID equals parse 1877 01:20:51,280 --> 01:20:56,480 parse ins ID and then we'll do the if is 1878 01:20:56,480 --> 01:20:59,280 Nan check we want to make sure that 1879 01:20:59,280 --> 01:21:02,679 parse ID is if it is not a number then 1880 01:21:02,679 --> 01:21:04,960 we want to return a response with a 1881 01:21:04,960 --> 01:21:06,760 status 1882 01:21:06,760 --> 01:21:09,600 code of 400 which means 1883 01:21:09,600 --> 01:21:12,679 invalid which just means bad request 1884 01:21:12,679 --> 01:21:15,719 which is because of the invalid ID okay 1885 01:21:15,719 --> 01:21:19,080 so now we want to of course just remove 1886 01:21:19,080 --> 01:21:22,400 the user from the array itself or if 1887 01:21:22,400 --> 01:21:24,440 again if you're using a database once we 1888 01:21:24,440 --> 01:21:25,719 get to that we would actually remove the 1889 01:21:25,719 --> 01:21:27,480 user from the database in our case we 1890 01:21:27,480 --> 01:21:29,040 have to remove the user from the 1891 01:21:29,040 --> 01:21:35,280 inmemory array so what we can do is this 1892 01:21:35,280 --> 01:21:37,280 so I'll go ahead and 1893 01:21:37,280 --> 01:21:39,840 use the 1894 01:21:39,840 --> 01:21:43,159 splice method and uh what I need to do 1895 01:21:43,159 --> 01:21:45,000 is I need to get the index of the user 1896 01:21:45,000 --> 01:21:46,920 that we're trying to remove so let's do 1897 01:21:46,920 --> 01:21:49,199 that so we want to get the index and 1898 01:21:49,199 --> 01:21:51,800 then we'll pass into splice and I'll 1899 01:21:51,800 --> 01:21:53,840 just remove the user from from the array 1900 01:21:53,840 --> 01:21:58,159 so let's do this const find user index 1901 01:21:58,159 --> 01:22:01,880 mock users find index and I'm going to 1902 01:22:01,880 --> 01:22:03,360 go ahead and pass the predicate function 1903 01:22:03,360 --> 01:22:04,800 which is just going 1904 01:22:04,800 --> 01:22:09,199 to um find the user by its 1905 01:22:09,199 --> 01:22:11,960 ID and remember find index will return 1906 01:22:11,960 --> 01:22:13,360 negative 1 if we're not able to actually 1907 01:22:13,360 --> 01:22:15,239 find the user at all in the array so we 1908 01:22:15,239 --> 01:22:17,199 need to do a check to see if F user 1909 01:22:17,199 --> 01:22:21,239 index is equal to1 and if it is we'll do 1910 01:22:21,239 --> 01:22:23,520 the same that we did in our patch input 1911 01:22:23,520 --> 01:22:27,120 request we'll return response. send 1912 01:22:27,120 --> 01:22:29,520 status and we'll do 404 because we can't 1913 01:22:29,520 --> 01:22:30,520 find the 1914 01:22:30,520 --> 01:22:34,320 user all right so now that we were able 1915 01:22:34,320 --> 01:22:37,320 to uh handle this case at this point we 1916 01:22:37,320 --> 01:22:39,239 know that the user uh we we have the 1917 01:22:39,239 --> 01:22:41,719 user index of where of where they are 1918 01:22:41,719 --> 01:22:44,360 located so what I can do is I can call 1919 01:22:44,360 --> 01:22:46,760 mock users. 1920 01:22:46,760 --> 01:22:51,280 splice pass in uh starts and I I don't 1921 01:22:51,280 --> 01:22:52,400 think we need the leete count because it 1922 01:22:52,400 --> 01:22:54,600 will just remove the element from the 1923 01:22:54,600 --> 01:22:56,960 array um so we and I think it'll just 1924 01:22:56,960 --> 01:22:59,960 remove only one I 1925 01:23:00,760 --> 01:23:05,639 think okay uh hopefully that makes 1926 01:23:05,800 --> 01:23:09,360 sense and now uh what I can do is I'll 1927 01:23:09,360 --> 01:23:14,480 just return response. send status 1928 01:23:14,760 --> 01:23:17,800 200 and let's see what happens so let's 1929 01:23:17,800 --> 01:23:19,719 go into our Thunder client let's click 1930 01:23:19,719 --> 01:23:22,760 new request we'll make a request we'll 1931 01:23:22,760 --> 01:23:24,280 make a delete request so I'm going to 1932 01:23:24,280 --> 01:23:27,920 select the delete method and let's do 1933 01:23:27,920 --> 01:23:32,199 localhost API users um I want to delete 1934 01:23:32,199 --> 01:23:35,440 user with ID number two so I'm going to 1935 01:23:35,440 --> 01:23:37,960 use I'm going to pass number two as the 1936 01:23:37,960 --> 01:23:41,040 route parameter and we're not going to 1937 01:23:41,040 --> 01:23:42,719 put any request body we're not going to 1938 01:23:42,719 --> 01:23:45,960 send any request body uh let's see parse 1939 01:23:45,960 --> 01:23:48,199 is not Define that I messed something up 1940 01:23:48,199 --> 01:23:50,480 let's 1941 01:23:51,480 --> 01:23:54,480 see oh whoops I forgot to change this to 1942 01:23:54,480 --> 01:23:56,719 I forgot to write the rest out it's 1943 01:23:56,719 --> 01:23:57,920 pared 1944 01:23:57,920 --> 01:24:02,360 ID not parse ID or parse y sorry about 1945 01:24:02,360 --> 01:24:04,560 that um yeah this is kind of reason this 1946 01:24:04,560 --> 01:24:06,800 is one of the downsides of JavaScript I 1947 01:24:06,800 --> 01:24:09,520 do need a linter though I agree with 1948 01:24:09,520 --> 01:24:12,120 needing a linter to detect these issues 1949 01:24:12,120 --> 01:24:16,560 but it's okay we fixed it and let's go 1950 01:24:16,560 --> 01:24:20,719 and run our app again so going back to 1951 01:24:20,719 --> 01:24:23,040 vs code in the Thunder clim 1952 01:24:23,040 --> 01:24:25,040 I'm going to go ahead and click Send and 1953 01:24:25,040 --> 01:24:27,840 let's just see if I can get the user by 1954 01:24:27,840 --> 01:24:30,000 its ID so it says four for not found 1955 01:24:30,000 --> 01:24:33,080 let's make sure the users array is valid 1956 01:24:33,080 --> 01:24:36,520 um let's see so it seems like um okay so 1957 01:24:36,520 --> 01:24:38,080 I know what the problem is so it seems 1958 01:24:38,080 --> 01:24:41,119 like it's sliced it spliced everything 1959 01:24:41,119 --> 01:24:42,639 starting at the index so we actually 1960 01:24:42,639 --> 01:24:44,800 only need to delete we need to do spec 1961 01:24:44,800 --> 01:24:46,840 we need to specify the delete count cuz 1962 01:24:46,840 --> 01:24:49,840 I think it removes everything after that 1963 01:24:49,840 --> 01:24:52,719 index so let me actually fix that so so 1964 01:24:52,719 --> 01:24:55,360 we have all of our users let's try to 1965 01:24:55,360 --> 01:24:58,400 delete the user so 1966 01:24:58,400 --> 01:25:01,320 delete okay and let's try to get all the 1967 01:25:01,320 --> 01:25:04,280 users again okay so all of our users are 1968 01:25:04,280 --> 01:25:07,000 here I'm not sure why though it is 1969 01:25:07,000 --> 01:25:09,440 returning as gray this time that's kind 1970 01:25:09,440 --> 01:25:11,080 of weird if you ask 1971 01:25:11,080 --> 01:25:12,960 me 1972 01:25:12,960 --> 01:25:16,320 um but if I try to get the user by its 1973 01:25:16,320 --> 01:25:18,840 ID it's not 1974 01:25:18,840 --> 01:25:21,159 found but I but I can grab the other 1975 01:25:21,159 --> 01:25:24,880 users Okay so so um that works 1976 01:25:24,880 --> 01:25:27,639 so um that's pretty cool and you can see 1977 01:25:27,639 --> 01:25:30,400 that in the browser I don't have the 1978 01:25:30,400 --> 01:25:33,040 user by its ID anymore so let's just 1979 01:25:33,040 --> 01:25:34,440 delete a couple 1980 01:25:34,440 --> 01:25:38,880 more let's delete three if I 1981 01:25:38,880 --> 01:25:42,440 refresh I don't have user of ID3 1982 01:25:42,440 --> 01:25:45,600 anymore let's delete user number 1983 01:25:45,600 --> 01:25:48,400 one okay so now I'm only down to four 1984 01:25:48,400 --> 01:25:50,040 users left so that is pretty much how 1985 01:25:50,040 --> 01:25:53,280 you can use the delete method 1986 01:25:53,280 --> 01:25:55,000 okay so 1987 01:25:55,000 --> 01:25:57,159 hopefully this makes sense and hopefully 1988 01:25:57,159 --> 01:25:59,800 you now better understand how to use all 1989 01:25:59,800 --> 01:26:03,920 five of these HTTP request methods we 1990 01:26:03,920 --> 01:26:06,560 have went over get requests post 1991 01:26:06,560 --> 01:26:09,520 requests we went over put requests and 1992 01:26:09,520 --> 01:26:11,880 Patch requests and then finally we went 1993 01:26:11,880 --> 01:26:14,520 over delete requests now if you want to 1994 01:26:14,520 --> 01:26:17,760 see a list of more HTTP request methods 1995 01:26:17,760 --> 01:26:20,840 you can go over to the Mozilla docs over 1996 01:26:20,840 --> 01:26:23,159 here you just Google this uh HTTP 1997 01:26:23,159 --> 01:26:25,159 request methods you can see that there 1998 01:26:25,159 --> 01:26:27,600 are get requests there's also a head 1999 01:26:27,600 --> 01:26:29,880 method as well but um you know you don't 2000 01:26:29,880 --> 01:26:31,840 really use these that much but sometimes 2001 01:26:31,840 --> 01:26:33,960 you will use them there's also the 2002 01:26:33,960 --> 01:26:36,440 connect method options Trace I 2003 01:26:36,440 --> 01:26:38,040 personally have never really used these 2004 01:26:38,040 --> 01:26:39,719 that much but there might come a case 2005 01:26:39,719 --> 01:26:41,360 where you do need it but in that case 2006 01:26:41,360 --> 01:26:43,080 you can just read up about it and you 2007 01:26:43,080 --> 01:26:44,880 know see what you're supposed to do with 2008 01:26:44,880 --> 01:26:48,320 these methods and use it 2009 01:26:49,960 --> 01:26:51,719 accordingly all right so now I'm going 2010 01:26:51,719 --> 01:26:53,480 to go ahead and teach you all about how 2011 01:26:53,480 --> 01:26:57,080 middleware Works in expressjs keep in 2012 01:26:57,080 --> 01:26:58,840 mind that middleware can be defined 2013 01:26:58,840 --> 01:27:01,239 differently in different environment but 2014 01:27:01,239 --> 01:27:03,119 in the general sense it really just 2015 01:27:03,119 --> 01:27:05,480 means one thing and that means it's just 2016 01:27:05,480 --> 01:27:09,600 a mid process between one or two or many 2017 01:27:09,600 --> 01:27:12,400 different functions or other processes 2018 01:27:12,400 --> 01:27:14,840 so in the context of expressjs a 2019 01:27:14,840 --> 01:27:17,920 middleware is just a function that can 2020 01:27:17,920 --> 01:27:19,760 have logic but the middleware function 2021 01:27:19,760 --> 01:27:22,800 also is a request Handler as as well so 2022 01:27:22,800 --> 01:27:25,679 that middleware function has the request 2023 01:27:25,679 --> 01:27:29,159 response arguments as well and you can 2024 01:27:29,159 --> 01:27:31,520 actually use the middleware function to 2025 01:27:31,520 --> 01:27:33,520 return a response if you want to so I'll 2026 01:27:33,520 --> 01:27:35,040 show you a simple example of what a 2027 01:27:35,040 --> 01:27:37,199 middleware function could look like so 2028 01:27:37,199 --> 01:27:39,480 I'll create a simple function I'll call 2029 01:27:39,480 --> 01:27:42,920 this logging 2030 01:27:43,000 --> 01:27:45,360 middleware and remember this middleware 2031 01:27:45,360 --> 01:27:48,880 function will have access to the request 2032 01:27:48,880 --> 01:27:50,000 and 2033 01:27:50,000 --> 01:27:53,840 response objects as arguments 2034 01:27:53,840 --> 01:27:56,119 okay and 2035 01:27:56,119 --> 01:27:59,440 additionally the middleware function or 2036 01:27:59,440 --> 01:28:01,920 the request Handler function also has 2037 01:28:01,920 --> 01:28:06,159 access to this next argument which is a 2038 01:28:06,159 --> 01:28:07,920 function that you call when you are done 2039 01:28:07,920 --> 01:28:08,719 with the 2040 01:28:08,719 --> 01:28:12,320 middleware okay so what I'll do in this 2041 01:28:12,320 --> 01:28:14,760 simple example is I'll console 2042 01:28:14,760 --> 01:28:20,080 log two things I will consol log the 2043 01:28:20,080 --> 01:28:22,800 request method 2044 01:28:22,800 --> 01:28:26,119 and then I'll conso log the request 2045 01:28:26,119 --> 01:28:29,080 URL okay and then once I'm done with 2046 01:28:29,080 --> 01:28:30,800 logging that to the console I'm just 2047 01:28:30,800 --> 01:28:34,480 going to call next and that's it in 2048 01:28:34,480 --> 01:28:36,800 order to now use my middleware there's 2049 01:28:36,800 --> 01:28:39,560 two ways I can enable it globally so all 2050 01:28:39,560 --> 01:28:41,239 of my routes will have this middleware 2051 01:28:41,239 --> 01:28:44,040 be invoked right before its request 2052 01:28:44,040 --> 01:28:46,600 Handler is 2053 01:28:46,600 --> 01:28:49,440 called or I can enable it for each 2054 01:28:49,440 --> 01:28:52,080 specific endpoint so I'll show you both 2055 01:28:52,080 --> 01:28:54,360 examples so let's call app.use to 2056 01:28:54,360 --> 01:28:56,880 register our middleware globally so all 2057 01:28:56,880 --> 01:28:58,560 I do is I just pass in logging 2058 01:28:58,560 --> 01:29:01,480 middleware as a function or as an 2059 01:29:01,480 --> 01:29:07,080 argument like this okay now if I were to 2060 01:29:07,080 --> 01:29:11,320 go into let's say my browser if I visit 2061 01:29:11,320 --> 01:29:14,440 the base URL of our application you can 2062 01:29:14,440 --> 01:29:18,840 see that in the console it now logs the 2063 01:29:18,840 --> 01:29:20,760 request type so 2064 01:29:20,760 --> 01:29:22,679 gets what 2065 01:29:22,679 --> 01:29:26,440 o it logs get and then the URL that 2066 01:29:26,440 --> 01:29:29,800 we're trying to visit if I go to SL API 2067 01:29:29,800 --> 01:29:32,239 users it will go ahead and log that as 2068 01:29:32,239 --> 01:29:35,600 well if I were to make any request to 2069 01:29:35,600 --> 01:29:38,560 any endpoint it's going to log that okay 2070 01:29:38,560 --> 01:29:41,360 so it is enabled globally if I only want 2071 01:29:41,360 --> 01:29:44,320 this middleware to occur for only 2072 01:29:44,320 --> 01:29:46,719 certain endpoints so instead of logging 2073 01:29:46,719 --> 01:29:50,080 it globally or instead of um registering 2074 01:29:50,080 --> 01:29:53,280 globally I want to take this middle 2075 01:29:53,280 --> 01:29:55,440 let's say I only want to do it for this 2076 01:29:55,440 --> 01:29:58,719 uh base URL so what I can do is I can 2077 01:29:58,719 --> 01:30:01,679 pass that middleware function as an 2078 01:30:01,679 --> 01:30:03,040 argument like 2079 01:30:03,040 --> 01:30:06,320 this okay so now if I go back 2080 01:30:06,320 --> 01:30:10,440 to let's let's just go to/ API users and 2081 01:30:10,440 --> 01:30:13,400 you'll see that nothing is being logged 2082 01:30:13,400 --> 01:30:15,639 however the moment I go to the base URL 2083 01:30:15,639 --> 01:30:18,719 and make a request there it logs it to 2084 01:30:18,719 --> 01:30:21,600 the console so that is good now one more 2085 01:30:21,600 --> 01:30:24,679 thing to mention is that uh you don't 2086 01:30:24,679 --> 01:30:26,840 even need to assign it to a variable and 2087 01:30:26,840 --> 01:30:28,719 pass it in like this you can just pass 2088 01:30:28,719 --> 01:30:31,560 it in like this if you really wanted to 2089 01:30:31,560 --> 01:30:32,440 and 2090 01:30:32,440 --> 01:30:35,480 then pass in the arguments request 2091 01:30:35,480 --> 01:30:38,520 response next okay now one thing to 2092 01:30:38,520 --> 01:30:40,199 mention is that if you don't have the 2093 01:30:40,199 --> 01:30:42,520 next function and if you don't call it 2094 01:30:42,520 --> 01:30:44,040 it's not going to go ahead and call the 2095 01:30:44,040 --> 01:30:46,600 next middleware so in this situation 2096 01:30:46,600 --> 01:30:49,199 right over here I have two middleware 2097 01:30:49,199 --> 01:30:51,159 functions uh let's call this one 2098 01:30:51,159 --> 01:30:54,480 middleware a okay uh middleware a will 2099 01:30:54,480 --> 01:30:56,880 be called first and then after middle 2100 01:30:56,880 --> 01:30:59,639 middleware a is called it has the option 2101 01:30:59,639 --> 01:31:01,560 notice how I said option to call the 2102 01:31:01,560 --> 01:31:03,520 next middleware the reason why I say it 2103 01:31:03,520 --> 01:31:05,000 has the option is because in this 2104 01:31:05,000 --> 01:31:07,560 middleware you can control the request 2105 01:31:07,560 --> 01:31:09,960 and send back the response if you need 2106 01:31:09,960 --> 01:31:14,239 to so this allows you to uh write 2107 01:31:14,239 --> 01:31:18,480 additional logic that you can reuse for 2108 01:31:18,480 --> 01:31:20,360 different endpoints that might share 2109 01:31:20,360 --> 01:31:22,639 that same logic and let's say for 2110 01:31:22,639 --> 01:31:24,960 example if the incoming request is 2111 01:31:24,960 --> 01:31:27,800 missing some kind of authorization token 2112 01:31:27,800 --> 01:31:29,600 you don't want to continue to the next 2113 01:31:29,600 --> 01:31:33,159 middleware or the next request Handler 2114 01:31:33,159 --> 01:31:34,760 so you want to reject that request by 2115 01:31:34,760 --> 01:31:37,480 sending back uh maybe like a status code 2116 01:31:37,480 --> 01:31:38,520 of 2117 01:31:38,520 --> 01:31:43,199 401 okay so for example what I'll do is 2118 01:31:43,199 --> 01:31:46,679 I'll just simply log base URL and I'm 2119 01:31:46,679 --> 01:31:50,159 not going to call the next function now 2120 01:31:50,159 --> 01:31:52,119 watch what happens if I refresh you'll 2121 01:31:52,119 --> 01:31:55,400 see how it is stuck in this pending 2122 01:31:55,400 --> 01:31:58,400 State and it it does log base URL but 2123 01:31:58,400 --> 01:32:00,119 the client never receives a response 2124 01:32:00,119 --> 01:32:01,920 back that's because we're not calling 2125 01:32:01,920 --> 01:32:03,800 the next function so you need to make 2126 01:32:03,800 --> 01:32:06,320 sure you call the next function in order 2127 01:32:06,320 --> 01:32:08,920 for it to call the next middleware down 2128 01:32:08,920 --> 01:32:11,199 the chain okay you can have as many 2129 01:32:11,199 --> 01:32:14,199 middlewares as you want called in 2130 01:32:14,199 --> 01:32:16,960 sequential order so I can even add two 2131 01:32:16,960 --> 01:32:19,159 more like 2132 01:32:19,159 --> 01:32:22,040 this and then I'll just call next after 2133 01:32:22,040 --> 01:32:24,520 each one and if I refresh and if I look 2134 01:32:24,520 --> 01:32:26,440 at the logs you can see every single 2135 01:32:26,440 --> 01:32:29,960 middleware was called and let me add one 2136 01:32:29,960 --> 01:32:32,600 two and three so you can see that it is 2137 01:32:32,600 --> 01:32:36,480 called in sequential order base URL 1 2 2138 01:32:36,480 --> 01:32:39,360 and three all being logged in sequential 2139 01:32:39,360 --> 01:32:41,840 order hopefully that makes sense another 2140 01:32:41,840 --> 01:32:43,639 thing that I also want to mention is 2141 01:32:43,639 --> 01:32:46,920 middleware must be registered before a 2142 01:32:46,920 --> 01:32:50,080 route if you're using app.use register 2143 01:32:50,080 --> 01:32:51,760 it so what I mean by that is if if you 2144 01:32:51,760 --> 01:32:53,960 want a middleware to be registered for 2145 01:32:53,960 --> 01:32:55,760 all of your routes you need to make sure 2146 01:32:55,760 --> 01:32:58,880 that you call app.use right before you 2147 01:32:58,880 --> 01:33:02,440 call you know app.get app.st app. put 2148 01:33:02,440 --> 01:33:05,159 Etc okay if you were to register the 2149 01:33:05,159 --> 01:33:07,320 middleware let's say after you called 2150 01:33:07,320 --> 01:33:10,239 those app. poost and app.get methods 2151 01:33:10,239 --> 01:33:11,280 your middleware is not going to be 2152 01:33:11,280 --> 01:33:13,400 registered for those routes so order 2153 01:33:13,400 --> 01:33:15,040 matters in this case so I'll show you an 2154 01:33:15,040 --> 01:33:18,360 example let's say I'm going to go after 2155 01:33:18,360 --> 01:33:22,080 SL API users I'm going to go ahead and 2156 01:33:22,080 --> 01:33:24,760 and uh 2157 01:33:24,760 --> 01:33:28,040 call uh app.use right over here on line 2158 01:33:28,040 --> 01:33:32,080 44 and pass the logging middleware so 2159 01:33:32,080 --> 01:33:33,360 what I'm doing is I'm registering the 2160 01:33:33,360 --> 01:33:35,480 middleware for all of the endpoints that 2161 01:33:35,480 --> 01:33:38,040 are registered after I'm calling this 2162 01:33:38,040 --> 01:33:39,560 app.use 2163 01:33:39,560 --> 01:33:42,480 so these two endpoints right over here 2164 01:33:42,480 --> 01:33:44,280 will not have the logging middleware 2165 01:33:44,280 --> 01:33:47,639 registered so I go into the browser and 2166 01:33:47,639 --> 01:33:50,040 if I go to the base URL you'll see that 2167 01:33:50,040 --> 01:33:52,280 the console does not log anything if I 2168 01:33:52,280 --> 01:33:57,280 go to SL users or/ API users you'll see 2169 01:33:57,280 --> 01:33:59,360 that the console doesn't lock anything 2170 01:33:59,360 --> 01:34:01,520 but if I were to go ahead and make a 2171 01:34:01,520 --> 01:34:04,679 post request or a get request to this 2172 01:34:04,679 --> 01:34:07,119 endpoint so let's make a get request to/ 2173 01:34:07,119 --> 01:34:09,560 API 2174 01:34:09,560 --> 01:34:12,880 users1 uh you will see that it now 2175 01:34:12,880 --> 01:34:16,320 logs uh it now logs in the console the 2176 01:34:16,320 --> 01:34:19,000 request type or the request method and 2177 01:34:19,000 --> 01:34:22,960 the URL okay so we're remember order 2178 01:34:22,960 --> 01:34:24,760 matters and one more thing that I do 2179 01:34:24,760 --> 01:34:28,040 want to talk about is in the app.use uh 2180 01:34:28,040 --> 01:34:31,239 method call you can also pass in as 2181 01:34:31,239 --> 01:34:34,119 many uh middlewares as you want and they 2182 01:34:34,119 --> 01:34:35,880 will also be called in sequential order 2183 01:34:35,880 --> 01:34:38,880 as well so just one more quick example 2184 01:34:38,880 --> 01:34:42,520 I'll pass another middleware function 2185 01:34:42,520 --> 01:34:43,639 and I'll 2186 01:34:43,639 --> 01:34:46,920 just write a console log and I'll say uh 2187 01:34:46,920 --> 01:34:49,199 finished 2188 01:34:49,199 --> 01:34:53,080 logging and I'll call next 2189 01:34:53,080 --> 01:34:55,920 and Let me refresh and you should see 2190 01:34:55,920 --> 01:34:59,159 that it says uh so it first it 2191 01:34:59,159 --> 01:35:01,560 logs uh the first thing which comes from 2192 01:35:01,560 --> 01:35:03,600 the logging middleware and then once 2193 01:35:03,600 --> 01:35:05,880 that middleware is done it calls the 2194 01:35:05,880 --> 01:35:07,159 next 2195 01:35:07,159 --> 01:35:09,840 middleware okay and then the logs 2196 01:35:09,840 --> 01:35:12,360 finished logging and now once we are 2197 01:35:12,360 --> 01:35:16,040 done with this next middleware remember 2198 01:35:16,040 --> 01:35:18,520 we call the next function and then it'll 2199 01:35:18,520 --> 01:35:21,560 just go down to the endpoint level so 2200 01:35:21,560 --> 01:35:23,760 remember the request and response 2201 01:35:23,760 --> 01:35:27,520 Handler is also a middleware as well 2202 01:35:27,520 --> 01:35:31,600 so it also takes in the next function 2203 01:35:31,600 --> 01:35:33,719 and then you can also call next in here 2204 01:35:33,719 --> 01:35:34,840 as well but if you don't have any 2205 01:35:34,840 --> 01:35:36,679 additional middlewares after it then 2206 01:35:36,679 --> 01:35:39,840 there's no point to add that argument in 2207 01:35:39,840 --> 01:35:42,800 the function signature okay so hopefully 2208 01:35:42,800 --> 01:35:46,840 that makes sense okay so let's go ahead 2209 01:35:46,840 --> 01:35:50,560 and take what we just learned and put it 2210 01:35:50,560 --> 01:35:52,280 into 2211 01:35:52,280 --> 01:35:55,280 uh work so what I'll do is since I have 2212 01:35:55,280 --> 01:35:58,400 a lot of reusable Logic for a lot of my 2213 01:35:58,400 --> 01:36:01,679 endpoints so notice how in this app dop 2214 01:36:01,679 --> 01:36:06,320 put uh endpoint I have a lot of the same 2215 01:36:06,320 --> 01:36:08,600 logic right over here that is written in 2216 01:36:08,600 --> 01:36:11,239 the app. patch method and the app. 2217 01:36:11,239 --> 01:36:13,520 delete method okay so what I'm going to 2218 01:36:13,520 --> 01:36:15,760 do is I'm going to take all of this 2219 01:36:15,760 --> 01:36:19,280 right over here I'm going to copy it and 2220 01:36:19,280 --> 01:36:20,679 I'm going to go ahead and create a 2221 01:36:20,679 --> 01:36:23,280 function up top over here and I'll call 2222 01:36:23,280 --> 01:36:26,719 this handle user by ID or maybe I should 2223 01:36:26,719 --> 01:36:30,600 call it resolve user by ID okay because 2224 01:36:30,600 --> 01:36:32,440 the goal of this logic is to really get 2225 01:36:32,440 --> 01:36:36,360 that um index by the user ID so maybe I 2226 01:36:36,360 --> 01:36:40,719 should call this resolve index by user 2227 01:36:40,719 --> 01:36:44,760 ID yeah let me call it this 2228 01:36:44,760 --> 01:36:47,480 instead okay so this will be an arrow 2229 01:36:47,480 --> 01:36:49,000 function I'm going to paste the logic in 2230 01:36:49,000 --> 01:36:50,840 here and remember because it is a 2231 01:36:50,840 --> 01:36:53,040 middleware and the middleware is also a 2232 01:36:53,040 --> 01:36:54,080 request 2233 01:36:54,080 --> 01:36:57,040 Handler we have the request response and 2234 01:36:57,040 --> 01:37:00,400 we need the next argument as well so 2235 01:37:00,400 --> 01:37:03,480 three arguments Al together and now 2236 01:37:03,480 --> 01:37:05,360 here's the thing though what I want to 2237 01:37:05,360 --> 01:37:07,960 do is I want to use this middleware 2238 01:37:07,960 --> 01:37:08,719 right 2239 01:37:08,719 --> 01:37:11,880 before I uh let's see I want to use this 2240 01:37:11,880 --> 01:37:13,600 middleware right 2241 01:37:13,600 --> 01:37:17,440 before I call this uh request 2242 01:37:17,440 --> 01:37:20,840 Handler because remember this middleware 2243 01:37:20,840 --> 01:37:22,679 that I'm cre right now the purpose of 2244 01:37:22,679 --> 01:37:24,560 this is 2245 01:37:24,560 --> 01:37:29,239 to um grab that user index where it is 2246 01:37:29,239 --> 01:37:32,159 located in the mock users and we want to 2247 01:37:32,159 --> 01:37:36,360 be able to um uh have the next 2248 01:37:36,360 --> 01:37:39,159 middleware that is called use it but in 2249 01:37:39,159 --> 01:37:40,520 order for the next middleware to 2250 01:37:40,520 --> 01:37:43,000 actually be able to use that information 2251 01:37:43,000 --> 01:37:45,119 we need to be able to pass it somehow 2252 01:37:45,119 --> 01:37:48,360 now there's no direct way to passing uh 2253 01:37:48,360 --> 01:37:51,040 data from one middleware to the other 2254 01:37:51,040 --> 01:37:52,760 but but what you can do is you can 2255 01:37:52,760 --> 01:37:55,119 attach properties to the request object 2256 01:37:55,119 --> 01:37:56,760 since we're using JavaScript we can 2257 01:37:56,760 --> 01:37:58,800 dynamically attach 2258 01:37:58,800 --> 01:38:01,280 properties very easily to the object so 2259 01:38:01,280 --> 01:38:03,639 I can reference request and I'm going to 2260 01:38:03,639 --> 01:38:04,639 go ahead 2261 01:38:04,639 --> 01:38:07,320 and reference find user index so this 2262 01:38:07,320 --> 01:38:08,880 obviously does not exist on the request 2263 01:38:08,880 --> 01:38:11,119 object but I'm just going to assign it 2264 01:38:11,119 --> 01:38:14,960 to this just like that okay so now in 2265 01:38:14,960 --> 01:38:18,840 the next middleware and all of the uh 2266 01:38:18,840 --> 01:38:20,199 succeeding middlewares that are being 2267 01:38:20,199 --> 01:38:22,280 called assuming that we don't ever 2268 01:38:22,280 --> 01:38:24,719 delete F user index from the request 2269 01:38:24,719 --> 01:38:27,119 object they will have access to this F 2270 01:38:27,119 --> 01:38:29,199 user index property which is going to be 2271 01:38:29,199 --> 01:38:32,800 a numeric value so once we are done 2272 01:38:32,800 --> 01:38:34,480 we're just going to call 2273 01:38:34,480 --> 01:38:36,840 next okay and now one more thing that I 2274 01:38:36,840 --> 01:38:39,520 also did not mention is that uh next 2275 01:38:39,520 --> 01:38:42,320 actually does take an argument but it 2276 01:38:42,320 --> 01:38:45,440 expects an error object or null so if 2277 01:38:45,440 --> 01:38:48,119 you do pass an error object or an eror 2278 01:38:48,119 --> 01:38:51,199 an error instance such as like this like 2279 01:38:51,199 --> 01:38:53,800 new error this will actually throw an 2280 01:38:53,800 --> 01:38:55,360 error at the express level if you don't 2281 01:38:55,360 --> 01:38:56,920 pass in anything then it won't throw any 2282 01:38:56,920 --> 01:38:58,199 error at all and it will just assume 2283 01:38:58,199 --> 01:39:01,400 that everything is successful okay but 2284 01:39:01,400 --> 01:39:04,119 we are handling errors though with uh 2285 01:39:04,119 --> 01:39:06,520 these checks and sending back the 2286 01:39:06,520 --> 01:39:09,320 correct status based on whatever data 2287 01:39:09,320 --> 01:39:12,800 that we send to the server so we should 2288 01:39:12,800 --> 01:39:14,960 be fine okay and since we're not 2289 01:39:14,960 --> 01:39:17,080 referencing the request body in this 2290 01:39:17,080 --> 01:39:19,199 middleware at all because we actually 2291 01:39:19,199 --> 01:39:21,719 referenc the request body at the final 2292 01:39:21,719 --> 01:39:24,199 request Handler function we're just 2293 01:39:24,199 --> 01:39:26,320 going to remove that the structure of 2294 01:39:26,320 --> 01:39:29,119 the request body right there so let's 2295 01:39:29,119 --> 01:39:30,280 actually use this for one of our 2296 01:39:30,280 --> 01:39:32,199 endpoints first just so you all can see 2297 01:39:32,199 --> 01:39:34,679 how this works so what I'll do is I'll 2298 01:39:34,679 --> 01:39:37,800 use it for my put request to update the 2299 01:39:37,800 --> 01:39:41,400 user by ID so we just pretty much take 2300 01:39:41,400 --> 01:39:43,560 that middleware and we pass it as an 2301 01:39:43,560 --> 01:39:46,199 argument right before our final request 2302 01:39:46,199 --> 01:39:48,480 Handler which is this request and 2303 01:39:48,480 --> 01:39:50,280 response or which is this request and 2304 01:39:50,280 --> 01:39:52,440 response Handler over here here now 2305 01:39:52,440 --> 01:39:55,040 since all the logic that we have inside 2306 01:39:55,040 --> 01:39:57,639 resolve index by user resolve index by 2307 01:39:57,639 --> 01:40:00,599 user ID um we all this logic that we see 2308 01:40:00,599 --> 01:40:02,639 over here was moved into that middleware 2309 01:40:02,639 --> 01:40:04,679 function so I can just delete all of 2310 01:40:04,679 --> 01:40:07,599 this okay so I'm going to just remove 2311 01:40:07,599 --> 01:40:10,239 all this I still will need the request 2312 01:40:10,239 --> 01:40:13,400 body though so I will remove at least 2313 01:40:13,400 --> 01:40:16,040 the destructuring of the request prams 2314 01:40:16,040 --> 01:40:18,040 but I'll leave the the structuring of 2315 01:40:18,040 --> 01:40:19,840 the request body right over here and 2316 01:40:19,840 --> 01:40:21,440 remember we never modified the request 2317 01:40:21,440 --> 01:40:23,599 body at all now we do need a reference 2318 01:40:23,599 --> 01:40:26,560 to find user index in this request 2319 01:40:26,560 --> 01:40:29,080 Handler scope so I will need to 2320 01:40:29,080 --> 01:40:32,280 destructure that and I can now that in 2321 01:40:32,280 --> 01:40:35,560 the resolve index by user ID middleware 2322 01:40:35,560 --> 01:40:38,480 function I got the index and I attached 2323 01:40:38,480 --> 01:40:41,760 it to the request object okay so at this 2324 01:40:41,760 --> 01:40:44,040 point if we hit 2325 01:40:44,040 --> 01:40:46,679 this part right over here we can assume 2326 01:40:46,679 --> 01:40:50,119 that fine user index is in fact defined 2327 01:40:50,119 --> 01:40:53,360 because if it was not defined um then we 2328 01:40:53,360 --> 01:40:57,440 actually wouldn't even be uh in this in 2329 01:40:57,440 --> 01:41:01,960 this uh final middleware at all okay so 2330 01:41:01,960 --> 01:41:04,599 now I can access find user index from 2331 01:41:04,599 --> 01:41:07,239 the request object but now I also need 2332 01:41:07,239 --> 01:41:09,480 to fix this par ID because that was 2333 01:41:09,480 --> 01:41:11,960 something from that we had before but we 2334 01:41:11,960 --> 01:41:14,080 no longer have that but that's fine 2335 01:41:14,080 --> 01:41:16,400 because I can still reference mock users 2336 01:41:16,400 --> 01:41:20,599 use f user index to uh reference the 2337 01:41:20,599 --> 01:41:22,880 exact element at the position in the 2338 01:41:22,880 --> 01:41:24,840 mock us array and then just simply 2339 01:41:24,840 --> 01:41:28,159 reference. ID like this okay let's go 2340 01:41:28,159 --> 01:41:29,800 ahead and just test this out before we 2341 01:41:29,800 --> 01:41:31,560 modify the other end points and see how 2342 01:41:31,560 --> 01:41:34,080 this works all right so back into our 2343 01:41:34,080 --> 01:41:36,880 Thunder client let's make a put 2344 01:41:36,880 --> 01:41:42,560 request so put and then logo host uh 2345 01:41:42,560 --> 01:41:44,960 Slash API 2346 01:41:44,960 --> 01:41:48,440 users so I guess we'll update user uh 2347 01:41:48,440 --> 01:41:51,880 with ID3 which will be adom 2348 01:41:51,880 --> 01:41:54,199 so let's send the request body so let's 2349 01:41:54,199 --> 01:41:55,360 do 2350 01:41:55,360 --> 01:41:57,800 username let's change this to 2351 01:41:57,800 --> 01:42:00,280 Jackie so if I click send everything 2352 01:42:00,280 --> 01:42:02,679 works fine if I were to get that user 2353 01:42:02,679 --> 01:42:05,480 again the username was updated and since 2354 01:42:05,480 --> 01:42:07,080 we didn't provide a display name that 2355 01:42:07,080 --> 01:42:10,320 was um pretty much just overrided okay 2356 01:42:10,320 --> 01:42:12,119 so that is working just fine so let's 2357 01:42:12,119 --> 01:42:14,760 just update the other 2358 01:42:14,760 --> 01:42:17,520 um the other 2359 01:42:17,520 --> 01:42:21,320 uh end points as well but before let's 2360 01:42:21,320 --> 01:42:23,159 just recap of what's going on so 2361 01:42:23,159 --> 01:42:26,000 whenever I make a put request to/ API 2362 01:42:26,000 --> 01:42:27,960 users and then the route parameter we 2363 01:42:27,960 --> 01:42:29,800 provide which was number three it's 2364 01:42:29,800 --> 01:42:31,920 going to go ahead and first call the 2365 01:42:31,920 --> 01:42:34,320 first middleware well first it will call 2366 01:42:34,320 --> 01:42:36,360 all of its uh Global middlewares if 2367 01:42:36,360 --> 01:42:38,080 there are any that are registered before 2368 01:42:38,080 --> 01:42:40,239 that route in our case we only have 2369 01:42:40,239 --> 01:42:42,719 express. Json which we need to parse 2370 01:42:42,719 --> 01:42:47,159 that Json um into that Json into natural 2371 01:42:47,159 --> 01:42:50,280 Json object so that is invoked first 2372 01:42:50,280 --> 01:42:51,400 obviously 2373 01:42:51,400 --> 01:42:54,440 then we don't have any other Global 2374 01:42:54,440 --> 01:42:58,080 middlewares invoked so we invoke the 2375 01:42:58,080 --> 01:43:00,840 resolve index by user 2376 01:43:00,840 --> 01:43:03,520 ID middleware and then it will go 2377 01:43:03,520 --> 01:43:05,520 through this logic basically the same 2378 01:43:05,520 --> 01:43:06,760 thing that we've been doing already just 2379 01:43:06,760 --> 01:43:08,960 moved into a separate function uh the 2380 01:43:08,960 --> 01:43:10,599 only difference is that we attach this 2381 01:43:10,599 --> 01:43:13,520 fine user index property or we add this 2382 01:43:13,520 --> 01:43:14,840 property to the request object and 2383 01:43:14,840 --> 01:43:17,320 assign it a value which is the index of 2384 01:43:17,320 --> 01:43:19,719 where the user is in the mock users 2385 01:43:19,719 --> 01:43:22,880 array and we call next so once next is 2386 01:43:22,880 --> 01:43:27,280 called it will now call this final 2387 01:43:27,280 --> 01:43:30,320 request Handler which is also middleware 2388 01:43:30,320 --> 01:43:32,159 and then from here we just 2389 01:43:32,159 --> 01:43:35,080 simply uh update the mock users or 2390 01:43:35,080 --> 01:43:37,320 update the user in the mock us array and 2391 01:43:37,320 --> 01:43:39,880 then we return a status code of 200 2392 01:43:39,880 --> 01:43:42,320 that's it let's go ahead and update uh 2393 01:43:42,320 --> 01:43:43,920 our patch request let me just copy this 2394 01:43:43,920 --> 01:43:46,440 middleware function name pass that in 2395 01:43:46,440 --> 01:43:49,679 there uh same thing just remove that 2396 01:43:49,679 --> 01:43:51,320 prams destructure 2397 01:43:51,320 --> 01:43:54,239 I don't need all of this 2398 01:43:54,239 --> 01:43:58,080 anymore uh and then let's see let's the 2399 01:43:58,080 --> 01:44:00,440 structure find user 2400 01:44:00,440 --> 01:44:03,320 index and I think that's it for patch 2401 01:44:03,320 --> 01:44:04,800 let's just test that out make sure that 2402 01:44:04,800 --> 01:44:07,400 patch works so I'm going to go ahead and 2403 01:44:07,400 --> 01:44:09,599 update user number three so since we 2404 01:44:09,599 --> 01:44:10,719 restarted the 2405 01:44:10,719 --> 01:44:14,400 server um the username should be back to 2406 01:44:14,400 --> 01:44:16,679 Adam and so the display didn't be there 2407 01:44:16,679 --> 01:44:18,920 as well I'll just update the username 2408 01:44:18,920 --> 01:44:21,199 using the patch request 2409 01:44:21,199 --> 01:44:23,199 there we go and if I click send you can 2410 01:44:23,199 --> 01:44:25,599 see the username was updated okay so the 2411 01:44:25,599 --> 01:44:27,440 patch request is working good and of 2412 01:44:27,440 --> 01:44:29,880 course if I were to provide an invalid 2413 01:44:29,880 --> 01:44:31,280 value the middleware will pick that up 2414 01:44:31,280 --> 01:44:33,480 for us you can see it says bad request 2415 01:44:33,480 --> 01:44:35,040 invalid 2416 01:44:35,040 --> 01:44:41,280 ID and that is handled right over let's 2417 01:44:43,719 --> 01:44:46,280 see should be handled somewhere over 2418 01:44:46,280 --> 01:44:47,880 here 2419 01:44:47,880 --> 01:44:50,000 um oh you know what we haven't did it 2420 01:44:50,000 --> 01:44:51,159 for we haven't done it for the get 2421 01:44:51,159 --> 01:44:53,800 request it we'll do that later okay but 2422 01:44:53,800 --> 01:44:54,760 at 2423 01:44:54,760 --> 01:44:57,080 least uh let me do this if I make a put 2424 01:44:57,080 --> 01:44:59,639 request to this you can see that this 2425 01:44:59,639 --> 01:45:01,639 part gets handled by the middleware for 2426 01:45:01,639 --> 01:45:03,159 the put request and same thing for the 2427 01:45:03,159 --> 01:45:05,360 patch request as well we get a bad 2428 01:45:05,360 --> 01:45:07,520 request if I were to pass in a valid 2429 01:45:07,520 --> 01:45:10,000 numeric ID but it doesn't there's no 2430 01:45:10,000 --> 01:45:11,440 user with that ID then it'll just give 2431 01:45:11,440 --> 01:45:13,560 us a four for and that is all being 2432 01:45:13,560 --> 01:45:16,320 handled by the middleware that we just 2433 01:45:16,320 --> 01:45:19,599 created okay cool let's go ahead and do 2434 01:45:19,599 --> 01:45:21,840 the same thing for delete 2435 01:45:21,840 --> 01:45:25,040 uh so let's pass in resolve index by 2436 01:45:25,040 --> 01:45:29,280 user ID let me remove all of this stuff 2437 01:45:29,280 --> 01:45:30,560 right over 2438 01:45:30,560 --> 01:45:35,080 here yep and for this part we just need 2439 01:45:35,080 --> 01:45:40,560 to uh grab the fine index user just like 2440 01:45:40,560 --> 01:45:42,840 that so that way we know where to splice 2441 01:45:42,840 --> 01:45:44,560 or I'm sorry it's fine user 2442 01:45:44,560 --> 01:45:47,400 index okay now let's go ahead and try to 2443 01:45:47,400 --> 01:45:51,679 delete a user so I'll will uh delete 2444 01:45:51,679 --> 01:45:54,840 user with ID3 if I tried to get that 2445 01:45:54,840 --> 01:45:57,920 User it's not there 2446 01:45:58,080 --> 01:46:01,719 anymore okay see how user of 2447 01:46:01,719 --> 01:46:04,159 ID3 is not here 2448 01:46:04,159 --> 01:46:06,679 anymore okay let's just go ahead and fix 2449 01:46:06,679 --> 01:46:09,280 up the last one which is the get 2450 01:46:09,280 --> 01:46:14,239 request um so let's see uh we need 2451 01:46:14,239 --> 01:46:17,560 to um so the way that we are actually 2452 01:46:17,560 --> 01:46:20,040 returning the user in the get request is 2453 01:46:20,040 --> 01:46:21,840 done different because we're using the 2454 01:46:21,840 --> 01:46:23,800 find method and not find 2455 01:46:23,800 --> 01:46:27,080 index to actually get the user object 2456 01:46:27,080 --> 01:46:29,320 itself and we're just returning that as 2457 01:46:29,320 --> 01:46:31,440 a response but what I could do is I 2458 01:46:31,440 --> 01:46:32,920 could still use the same middleware 2459 01:46:32,920 --> 01:46:37,000 resolve index by user ID and I can grab 2460 01:46:37,000 --> 01:46:40,920 the uh find user 2461 01:46:40,920 --> 01:46:43,000 index 2462 01:46:43,000 --> 01:46:46,679 value from the request object and then I 2463 01:46:46,679 --> 01:46:49,000 can just use that to reference the mock 2464 01:46:49,000 --> 01:46:51,119 users array to return the correct user 2465 01:46:51,119 --> 01:46:52,440 so let's just do that just so that 2466 01:46:52,440 --> 01:46:54,760 everything is consistent with each other 2467 01:46:54,760 --> 01:46:57,080 so let me remove all of 2468 01:46:57,080 --> 01:46:58,639 this 2469 01:46:58,639 --> 01:47:01,840 and I'll go ahead and the 2470 01:47:01,840 --> 01:47:04,400 structure find user 2471 01:47:04,400 --> 01:47:07,599 index um and I'll just do this Con find 2472 01:47:07,599 --> 01:47:09,880 User it's always good to just double 2473 01:47:09,880 --> 01:47:11,239 check just to make sure even though we 2474 01:47:11,239 --> 01:47:15,880 know that find user index does 100% 2475 01:47:15,880 --> 01:47:18,320 resolve to a user in that array but it's 2476 01:47:18,320 --> 01:47:20,520 always good to check just to be safe so 2477 01:47:20,520 --> 01:47:23,360 mock users find user 2478 01:47:23,360 --> 01:47:25,880 index and if there's no user we'll just 2479 01:47:25,880 --> 01:47:27,960 return to 404 and if there is we'll just 2480 01:47:27,960 --> 01:47:30,320 return find user okay so we're still 2481 01:47:30,320 --> 01:47:31,560 using we're still utilizing our 2482 01:47:31,560 --> 01:47:35,199 middleware for all of our um endpoints 2483 01:47:35,199 --> 01:47:37,119 that we're using the user ID to search 2484 01:47:37,119 --> 01:47:40,040 or perform operations on so now if I try 2485 01:47:40,040 --> 01:47:42,719 to uh get the user by 2486 01:47:42,719 --> 01:47:45,480 ID uh it says mock users oh let me fix 2487 01:47:45,480 --> 01:47:49,080 that I think it's mock I uh misspelled 2488 01:47:49,080 --> 01:47:52,639 that it's case sensitive let's try again 2489 01:47:52,639 --> 01:47:55,679 okay there we go so that is pretty much 2490 01:47:55,679 --> 01:47:59,280 it with middleware I know it's a lot but 2491 01:47:59,280 --> 01:48:01,360 middleware is very powerful and 2492 01:48:01,360 --> 01:48:04,639 understanding how it works is very worth 2493 01:48:04,639 --> 01:48:06,560 it when you're using expressjs because 2494 01:48:06,560 --> 01:48:08,880 everything in expressjs that you're 2495 01:48:08,880 --> 01:48:10,440 going to be using has to do with 2496 01:48:10,440 --> 01:48:13,040 middleware in some way shape or form so 2497 01:48:13,040 --> 01:48:16,719 it's good to learn how middleware Works 2498 01:48:16,719 --> 01:48:19,400 learn the ins and outs of it and 2499 01:48:19,400 --> 01:48:21,199 understand how you can use it to your 2500 01:48:21,199 --> 01:48:23,159 advantage okay so hopefully all of this 2501 01:48:23,159 --> 01:48:25,440 made 2502 01:48:27,239 --> 01:48:30,520 sense all right everyone in this part of 2503 01:48:30,520 --> 01:48:32,520 the tutorial I'm going to teach you all 2504 01:48:32,520 --> 01:48:34,880 how to use express validator to validate 2505 01:48:34,880 --> 01:48:37,880 incoming data for our Express API the 2506 01:48:37,880 --> 01:48:40,159 reason why this is important is because 2507 01:48:40,159 --> 01:48:41,960 sometimes the data that you expect is 2508 01:48:41,960 --> 01:48:44,480 not the data that you receive let's say 2509 01:48:44,480 --> 01:48:47,159 you want to create a user and save it to 2510 01:48:47,159 --> 01:48:49,679 a database so you obviously need a post 2511 01:48:49,679 --> 01:48:51,719 request to to a user's endpoint to 2512 01:48:51,719 --> 01:48:53,560 create that user so that post request 2513 01:48:53,560 --> 01:48:55,360 will expect a request body and we want 2514 01:48:55,360 --> 01:48:57,360 to make sure that the username is not 2515 01:48:57,360 --> 01:48:59,920 over 32 characters of length now we are 2516 01:48:59,920 --> 01:49:01,520 expecting that but that doesn't mean 2517 01:49:01,520 --> 01:49:03,760 that the client will send a username 2518 01:49:03,760 --> 01:49:05,760 that matches our constraints so you want 2519 01:49:05,760 --> 01:49:08,320 to make sure that you check to see if 2520 01:49:08,320 --> 01:49:11,199 the username is 32 characters or less 2521 01:49:11,199 --> 01:49:12,719 now some of you might be wondering well 2522 01:49:12,719 --> 01:49:14,599 if I'm validating on the client side 2523 01:49:14,599 --> 01:49:16,360 let's say if I have a react or angular 2524 01:49:16,360 --> 01:49:17,960 project and that project has a form and 2525 01:49:17,960 --> 01:49:19,760 when I click on the form button it will 2526 01:49:19,760 --> 01:49:22,599 send a post request to my server well if 2527 01:49:22,599 --> 01:49:25,920 I'm validating that form before I click 2528 01:49:25,920 --> 01:49:28,560 that button before I submit that API 2529 01:49:28,560 --> 01:49:30,440 request why do I still need to validate 2530 01:49:30,440 --> 01:49:32,560 on the server side the reason why is 2531 01:49:32,560 --> 01:49:35,480 because the API your express application 2532 01:49:35,480 --> 01:49:37,320 does not know where that data is coming 2533 01:49:37,320 --> 01:49:39,880 from you can literally go into the web 2534 01:49:39,880 --> 01:49:42,440 app open up the network Tab and inspect 2535 01:49:42,440 --> 01:49:44,880 where that post request is being sent to 2536 01:49:44,880 --> 01:49:46,960 which endpoint it's being sent to take 2537 01:49:46,960 --> 01:49:49,080 that URL and throw it in something like 2538 01:49:49,080 --> 01:49:51,080 Postman or even the Thunder client that 2539 01:49:51,080 --> 01:49:53,239 we've been using to make API calls and 2540 01:49:53,239 --> 01:49:55,280 send whatever you want and bypass the 2541 01:49:55,280 --> 01:49:58,119 client side validation so you don't know 2542 01:49:58,119 --> 01:49:59,480 where the data is coming from so you 2543 01:49:59,480 --> 01:50:01,320 always need to make sure you are 2544 01:50:01,320 --> 01:50:03,320 validating it on the server side in my 2545 01:50:03,320 --> 01:50:04,599 opinion I think the server side 2546 01:50:04,599 --> 01:50:07,360 validation is the most important more 2547 01:50:07,360 --> 01:50:09,360 important than the client side because 2548 01:50:09,360 --> 01:50:10,880 the server side is where you're actually 2549 01:50:10,880 --> 01:50:13,360 going to process that data save it to a 2550 01:50:13,360 --> 01:50:15,520 database submit it to another external 2551 01:50:15,520 --> 01:50:17,679 API or do whatever you want with it okay 2552 01:50:17,679 --> 01:50:20,239 so you must always validate on the 2553 01:50:20,239 --> 01:50:22,239 server side no matter what so let's go 2554 01:50:22,239 --> 01:50:24,520 ahead and install Express validator so 2555 01:50:24,520 --> 01:50:25,639 I'm going to type 2556 01:50:25,639 --> 01:50:28,960 npmi Express hyphen validator just like 2557 01:50:28,960 --> 01:50:33,480 this and then I'll run my Express 2558 01:50:34,719 --> 01:50:38,400 server let's go into our code and we're 2559 01:50:38,400 --> 01:50:39,800 going to go ahead and 2560 01:50:39,800 --> 01:50:43,360 import a function from the Express 2561 01:50:43,360 --> 01:50:46,560 validator package just like this whoops 2562 01:50:46,560 --> 01:50:49,480 so import and then pair of curly braces 2563 01:50:49,480 --> 01:50:52,239 so I'll I'll import the query function 2564 01:50:52,239 --> 01:50:55,480 and this is used for validating query 2565 01:50:55,480 --> 01:50:58,000 parameters okay so there are a bunch of 2566 01:50:58,000 --> 01:50:59,280 different middleware functions that you 2567 01:50:59,280 --> 01:51:01,920 can import from Express validator and in 2568 01:51:01,920 --> 01:51:03,960 case if I didn't mention earlier yes 2569 01:51:03,960 --> 01:51:06,159 these functions that you are importing 2570 01:51:06,159 --> 01:51:09,199 you are using them as middleware so what 2571 01:51:09,199 --> 01:51:10,880 that means is that we're going to be 2572 01:51:10,880 --> 01:51:11,880 calling 2573 01:51:11,880 --> 01:51:15,239 them uh by passing it as an argument to 2574 01:51:15,239 --> 01:51:18,320 our request methods such as app.get for 2575 01:51:18,320 --> 01:51:20,079 example and then we want to make make 2576 01:51:20,079 --> 01:51:21,800 sure we're calling them right before our 2577 01:51:21,800 --> 01:51:23,920 final request Handler which is also a 2578 01:51:23,920 --> 01:51:25,920 middleware as well in case if you didn't 2579 01:51:25,920 --> 01:51:28,360 see the middleware section of this 2580 01:51:28,360 --> 01:51:30,119 tutorial definitely check that part out 2581 01:51:30,119 --> 01:51:31,400 either in the previous video if you're 2582 01:51:31,400 --> 01:51:34,320 watching just this Express validation 2583 01:51:34,320 --> 01:51:35,480 tutorial or if you're watching the 2584 01:51:35,480 --> 01:51:37,520 entire thing just go a few minutes back 2585 01:51:37,520 --> 01:51:41,159 to where we talked about middleware okay 2586 01:51:41,159 --> 01:51:43,159 but what I want to do is I want 2587 01:51:43,159 --> 01:51:47,040 to pass this query function call as if 2588 01:51:47,040 --> 01:51:49,000 it was a middle wear so we're going to 2589 01:51:49,000 --> 01:51:52,199 pass as an argument right before I 2590 01:51:52,199 --> 01:51:56,520 passed this request Handler function so 2591 01:51:56,520 --> 01:51:58,800 it's pretty easy we're just going to go 2592 01:51:58,800 --> 01:52:01,320 ahead and call query and then comma so 2593 01:52:01,320 --> 01:52:03,679 we now have three Arguments for this 2594 01:52:03,679 --> 01:52:06,199 app.get endpoint right over 2595 01:52:06,199 --> 01:52:08,960 here and now what I want to do is I want 2596 01:52:08,960 --> 01:52:10,719 to specify the query parameter that I 2597 01:52:10,719 --> 01:52:13,320 want to validate so I'll just do a 2598 01:52:13,320 --> 01:52:15,280 simple one we'll do 2599 01:52:15,280 --> 01:52:16,840 filter 2600 01:52:16,840 --> 01:52:20,400 okay and when you call the these 2601 01:52:20,400 --> 01:52:22,040 functions okay in this case we're 2602 01:52:22,040 --> 01:52:23,320 calling 2603 01:52:23,320 --> 01:52:26,679 query it creates a validation chain so 2604 01:52:26,679 --> 01:52:28,920 basically it just basically means that 2605 01:52:28,920 --> 01:52:31,040 when you call this function it returns 2606 01:52:31,040 --> 01:52:33,800 an instance of validation 2607 01:52:33,800 --> 01:52:36,960 chain and from here on you have access 2608 01:52:36,960 --> 01:52:40,520 to a bunch of methods that you can use 2609 01:52:40,520 --> 01:52:42,760 to determine what you want to validate 2610 01:52:42,760 --> 01:52:45,360 so if you want to validate that filter 2611 01:52:45,360 --> 01:52:46,560 is a 2612 01:52:46,560 --> 01:52:49,520 string you can call this is string 2613 01:52:49,520 --> 01:52:50,639 method 2614 01:52:50,639 --> 01:52:52,480 and this returns an instance of 2615 01:52:52,480 --> 01:52:56,079 validation chain so you can call 2616 01:52:56,079 --> 01:52:57,679 literally the same method as many times 2617 01:52:57,679 --> 01:52:59,400 as you want obviously you don't want to 2618 01:52:59,400 --> 01:53:01,920 do that but the whole point is that you 2619 01:53:01,920 --> 01:53:04,400 can use this validation chain to perform 2620 01:53:04,400 --> 01:53:07,320 more validations in order or it doesn't 2621 01:53:07,320 --> 01:53:08,920 really matter in this 2622 01:53:08,920 --> 01:53:11,239 point but the point is is that you can 2623 01:53:11,239 --> 01:53:14,440 use this validation chain to keep on 2624 01:53:14,440 --> 01:53:17,920 calling more methods to validate on that 2625 01:53:17,920 --> 01:53:20,040 single filter field so let's say you 2626 01:53:20,040 --> 01:53:22,639 want to validate that filter is a string 2627 01:53:22,639 --> 01:53:25,960 and then you also want to make sure uh 2628 01:53:25,960 --> 01:53:29,920 let's see that is not 2629 01:53:29,920 --> 01:53:32,960 empty okay so this will ensure that it 2630 01:53:32,960 --> 01:53:35,679 is a string and it is also not empty now 2631 01:53:35,679 --> 01:53:37,159 I will mention that with query 2632 01:53:37,159 --> 01:53:40,159 parameters in expressjs they are always 2633 01:53:40,159 --> 01:53:42,679 parsed as strings so even if you pass in 2634 01:53:42,679 --> 01:53:46,199 a numeric value in the address bar as 2635 01:53:46,199 --> 01:53:48,280 the query parameter for our filter or 2636 01:53:48,280 --> 01:53:49,760 really any query parameter it's it's 2637 01:53:49,760 --> 01:53:50,920 going to be parsed as a string and 2638 01:53:50,920 --> 01:53:52,119 you'll see in just a 2639 01:53:52,119 --> 01:53:56,520 second so what I'll do right now is um 2640 01:53:56,520 --> 01:54:00,040 this so I'll make a 2641 01:54:00,040 --> 01:54:02,119 request so let me go into my thunder 2642 01:54:02,119 --> 01:54:03,840 client right over here and let's make a 2643 01:54:03,840 --> 01:54:08,360 get request to the users endpoint and 2644 01:54:08,360 --> 01:54:09,880 for now we're not going to pass in the 2645 01:54:09,880 --> 01:54:13,360 query parameter for filter I'll click 2646 01:54:13,360 --> 01:54:16,079 Send and you'll see right over here that 2647 01:54:16,079 --> 01:54:17,440 nothing happens although we are 2648 01:54:17,440 --> 01:54:20,360 expecting an error to occur why is that 2649 01:54:20,360 --> 01:54:23,000 the case well here's the thing these 2650 01:54:23,000 --> 01:54:25,480 functions don't actually throw an error 2651 01:54:25,480 --> 01:54:27,079 they don't reject the request you 2652 01:54:27,079 --> 01:54:29,760 actually have to handle that yourself so 2653 01:54:29,760 --> 01:54:32,560 how do we handle that well remember 2654 01:54:32,560 --> 01:54:34,840 middlewares are called in sequential 2655 01:54:34,840 --> 01:54:36,280 order 2656 01:54:36,280 --> 01:54:39,440 so this query function that I'm calling 2657 01:54:39,440 --> 01:54:40,880 this is going to be the first middleware 2658 01:54:40,880 --> 01:54:43,000 that we're calling and then it's going 2659 01:54:43,000 --> 01:54:45,719 to call the next middleware so it'll 2660 01:54:45,719 --> 01:54:47,960 call this request Handler which remember 2661 01:54:47,960 --> 01:54:50,239 it is also a middleware as well so 2662 01:54:50,239 --> 01:54:52,679 inside our request Handler in the 2663 01:54:52,679 --> 01:54:55,679 function body we need to take care of 2664 01:54:55,679 --> 01:54:57,440 the error handling part because the 2665 01:54:57,440 --> 01:54:58,960 query function won't throw any errors 2666 01:54:58,960 --> 01:55:00,560 for you it will validate the fields but 2667 01:55:00,560 --> 01:55:02,719 it just won't throw any errors so you as 2668 01:55:02,719 --> 01:55:04,760 developer needs to handle that yourself 2669 01:55:04,760 --> 01:55:06,560 so just very quickly I want to consol 2670 01:55:06,560 --> 01:55:08,199 log this request object because I want 2671 01:55:08,199 --> 01:55:10,239 to show you what happens under the hood 2672 01:55:10,239 --> 01:55:11,639 I know some of you might not be 2673 01:55:11,639 --> 01:55:12,920 concerned about it but I want you to 2674 01:55:12,920 --> 01:55:14,920 also get full context with what is going 2675 01:55:14,920 --> 01:55:16,440 on instead of just telling you to call 2676 01:55:16,440 --> 01:55:18,159 all these functions so what I'll do is 2677 01:55:18,159 --> 01:55:21,639 I'll make a get request to this endpoint 2678 01:55:21,639 --> 01:55:24,400 and uh I'm logging that request object 2679 01:55:24,400 --> 01:55:25,800 but I want to show 2680 01:55:25,800 --> 01:55:29,079 you that you can see right over here in 2681 01:55:29,079 --> 01:55:32,040 the request object we have this new 2682 01:55:32,040 --> 01:55:34,079 property that is attached to that 2683 01:55:34,079 --> 01:55:35,880 request object and express validator 2684 01:55:35,880 --> 01:55:39,239 attach that themselves okay and it's 2685 01:55:39,239 --> 01:55:42,440 attached when we call this query 2686 01:55:42,440 --> 01:55:45,040 function middleware and then now we can 2687 01:55:45,040 --> 01:55:47,480 actually see that it's right over here 2688 01:55:47,480 --> 01:55:49,800 okay and I'll go even further further 2689 01:55:49,800 --> 01:55:52,400 let me just copy this part over here and 2690 01:55:52,400 --> 01:55:55,040 access that direct 2691 01:55:55,040 --> 01:55:57,199 field and show 2692 01:55:57,199 --> 01:56:01,599 you even further what that looks like so 2693 01:56:01,599 --> 01:56:04,199 you can see we have this context array 2694 01:56:04,199 --> 01:56:06,239 uh or this array of context it's an 2695 01:56:06,239 --> 01:56:09,199 object and it has a bunch of metadata 2696 01:56:09,199 --> 01:56:11,639 about uh the validation you can see we 2697 01:56:11,639 --> 01:56:13,400 have Fields 2698 01:56:13,400 --> 01:56:16,239 locations um errors okay a bunch of 2699 01:56:16,239 --> 01:56:17,679 different stuff obviously you don't have 2700 01:56:17,679 --> 01:56:20,239 to worry about this but underneath the 2701 01:56:20,239 --> 01:56:22,040 hood what happens is when we call this 2702 01:56:22,040 --> 01:56:25,119 query middleware function it's going to 2703 01:56:25,119 --> 01:56:28,199 go ahead and validate um the field for 2704 01:56:28,199 --> 01:56:29,960 you and then it'll attach the data to 2705 01:56:29,960 --> 01:56:34,040 this request object okay and from here 2706 01:56:34,040 --> 01:56:36,480 what you want to do is you want to call 2707 01:56:36,480 --> 01:56:39,880 this validation result function so let 2708 01:56:39,880 --> 01:56:42,920 me import that from Express validator so 2709 01:56:42,920 --> 01:56:45,880 validation result that's a function and 2710 01:56:45,880 --> 01:56:48,679 you want to call that function and let's 2711 01:56:48,679 --> 01:56:52,800 assign the return value to a variable 2712 01:56:52,800 --> 01:56:53,960 called 2713 01:56:53,960 --> 01:56:55,760 result so we're going to call this 2714 01:56:55,760 --> 01:56:57,239 validation result function and we want 2715 01:56:57,239 --> 01:57:01,360 to pass in the request object okay just 2716 01:57:01,360 --> 01:57:03,679 like that and what this will do is it'll 2717 01:57:03,679 --> 01:57:06,920 grab that field and it will extract 2718 01:57:06,920 --> 01:57:09,560 pretty much the validation errors and 2719 01:57:09,560 --> 01:57:11,639 you can handle that 2720 01:57:11,639 --> 01:57:13,840 yourself so instead of having to 2721 01:57:13,840 --> 01:57:15,840 manually do that yourself you can use 2722 01:57:15,840 --> 01:57:17,320 this validation result to do it for you 2723 01:57:17,320 --> 01:57:21,400 that's why they provide this function 2724 01:57:21,440 --> 01:57:23,960 so let's go ahead and send a request 2725 01:57:23,960 --> 01:57:25,599 again and now you can 2726 01:57:25,599 --> 01:57:30,159 see that when I log that result object I 2727 01:57:30,159 --> 01:57:31,960 now have everything in a proper format 2728 01:57:31,960 --> 01:57:34,239 you can see that we have this errors 2729 01:57:34,239 --> 01:57:37,079 property which is an array of errors and 2730 01:57:37,079 --> 01:57:39,679 you can see that we have two errors one 2731 01:57:39,679 --> 01:57:42,639 we have an invalid value which I guess 2732 01:57:42,639 --> 01:57:46,480 makes sense because we are trying to 2733 01:57:46,480 --> 01:57:48,400 validate if it is a 2734 01:57:48,400 --> 01:57:50,960 string and then we're also trying to 2735 01:57:50,960 --> 01:57:53,679 validate if it is not empty so we have 2736 01:57:53,679 --> 01:57:55,639 two different things we're trying to 2737 01:57:55,639 --> 01:57:58,000 validate so we have two errors total so 2738 01:57:58,000 --> 01:58:00,360 invalid value and an invalid value as 2739 01:58:00,360 --> 01:58:04,079 well but if I go ahead and pass in that 2740 01:58:04,079 --> 01:58:05,520 filter query 2741 01:58:05,520 --> 01:58:08,159 parameter and then let's just give it a 2742 01:58:08,159 --> 01:58:10,400 random value let's just do 2743 01:58:10,400 --> 01:58:12,800 Anson and now you can see that that 2744 01:58:12,800 --> 01:58:15,520 errors array is no 2745 01:58:15,520 --> 01:58:17,880 longer populated with errors because we 2746 01:58:17,880 --> 01:58:20,159 don't have any errors anymore 2747 01:58:20,159 --> 01:58:22,679 because we just pass in that filter 2748 01:58:22,679 --> 01:58:24,800 value right over here of course if I 2749 01:58:24,800 --> 01:58:28,000 don't pass in a value we still will get 2750 01:58:28,000 --> 01:58:30,639 an error it says invalid value because 2751 01:58:30,639 --> 01:58:33,599 we have this not empty call right over 2752 01:58:33,599 --> 01:58:36,360 here and you can also take a look at the 2753 01:58:36,360 --> 01:58:38,840 documentation or in your vs code if you 2754 01:58:38,840 --> 01:58:41,360 just uh use the dot operator after this 2755 01:58:41,360 --> 01:58:44,239 not empty call you can look at all the 2756 01:58:44,239 --> 01:58:46,960 other methods that you can 2757 01:58:46,960 --> 01:58:50,960 use to validate your Fe Fields so let's 2758 01:58:50,960 --> 01:58:55,320 do a validation on the length of the 2759 01:58:55,320 --> 01:59:00,599 value for filter so let's use is 2760 01:59:00,599 --> 01:59:03,159 length and then this will take in 2761 01:59:03,159 --> 01:59:07,079 options so let's do uh Min character 2762 01:59:07,079 --> 01:59:10,599 length we will go ahead and 2763 01:59:10,599 --> 01:59:13,800 do three characters and then for Max 2764 01:59:13,800 --> 01:59:16,960 will do 10 characters so 2765 01:59:16,960 --> 01:59:20,280 now I'm going to go back to my code or 2766 01:59:20,280 --> 01:59:24,440 my uh vender client so if I pass in 2767 01:59:24,440 --> 01:59:27,880 let's just leave it empty like 2768 01:59:28,400 --> 01:59:32,719 this let's see we should 2769 01:59:33,480 --> 01:59:38,520 get two errors okay um if I remove the 2770 01:59:38,520 --> 01:59:40,239 entire query parameter we should get 2771 01:59:40,239 --> 01:59:43,400 three because now we're calling three 2772 01:59:43,400 --> 01:59:47,239 functions we're chaining three functions 2773 01:59:47,239 --> 01:59:48,760 let's go ahead and pass in the filter 2774 01:59:48,760 --> 01:59:53,199 and let's do do a n so n if I click Send 2775 01:59:53,199 --> 01:59:55,800 you'll see that we get this error and 2776 01:59:55,800 --> 01:59:57,199 now if you actually look at these errors 2777 01:59:57,199 --> 01:59:58,679 you're probably starting to feel 2778 01:59:58,679 --> 02:00:02,360 confused on what is what you know so we 2779 02:00:02,360 --> 02:00:05,199 can actually use this method after we 2780 02:00:05,199 --> 02:00:08,360 call is length called with message you 2781 02:00:08,360 --> 02:00:10,199 can actually pass in a custom error 2782 02:00:10,199 --> 02:00:11,440 message and you can see over here it 2783 02:00:11,440 --> 02:00:13,239 says it sets the error message for the 2784 02:00:13,239 --> 02:00:17,000 previous validator so this with message 2785 02:00:17,000 --> 02:00:19,400 call is going to uh the the message 2786 02:00:19,400 --> 02:00:21,360 itself that you pass in here will relate 2787 02:00:21,360 --> 02:00:26,400 for this validator okay so we'll do um 2788 02:00:26,400 --> 02:00:33,119 must be between must be at least 3 to 10 2789 02:00:33,119 --> 02:00:35,800 characters uh and let's add a custom 2790 02:00:35,800 --> 02:00:38,599 with message call for not empty so must 2791 02:00:38,599 --> 02:00:41,199 not be 2792 02:00:41,199 --> 02:00:43,639 empty uh okay let's try it out so if I 2793 02:00:43,639 --> 02:00:44,639 click 2794 02:00:44,639 --> 02:00:47,560 Send and look at the console you can see 2795 02:00:47,560 --> 02:00:49,880 now we have that custom ER error 2796 02:00:49,880 --> 02:00:52,400 message okay hopefully that makes sense 2797 02:00:52,400 --> 02:00:54,520 and if I leave this 2798 02:00:54,520 --> 02:00:56,760 empty you can see that we have both of 2799 02:00:56,760 --> 02:00:58,800 our errors must not be empty and must be 2800 02:00:58,800 --> 02:01:01,800 at least 3 to 10 characters so that is 2801 02:01:01,800 --> 02:01:04,679 good okay so now if I were 2802 02:01:04,679 --> 02:01:07,599 to pass in just let's say five 2803 02:01:07,599 --> 02:01:09,000 characters now we shouldn't get that 2804 02:01:09,000 --> 02:01:12,920 error anymore and we don't but if I 2805 02:01:12,920 --> 02:01:15,239 exceed uh 10 2806 02:01:15,239 --> 02:01:17,400 characters it now it gives me this error 2807 02:01:17,400 --> 02:01:19,320 must be at least 3 to 10 characters so 2808 02:01:19,320 --> 02:01:22,079 hopefully this makes sense so this is 2809 02:01:22,079 --> 02:01:24,560 how you can validate query parameters 2810 02:01:24,560 --> 02:01:27,239 now let me go ahead and show you how we 2811 02:01:27,239 --> 02:01:28,840 can validate request bodies because 2812 02:01:28,840 --> 02:01:31,320 that's also very important as well so 2813 02:01:31,320 --> 02:01:34,400 similar to query parameter uh we have 2814 02:01:34,400 --> 02:01:37,000 this query function for quate parameters 2815 02:01:37,000 --> 02:01:39,159 but we also have a function to validate 2816 02:01:39,159 --> 02:01:42,159 request bodies and that is the body 2817 02:01:42,159 --> 02:01:43,840 function and you import that from 2818 02:01:43,840 --> 02:01:46,599 Express validator just like this and it 2819 02:01:46,599 --> 02:01:49,560 is used the same exact way 2820 02:01:49,560 --> 02:01:51,800 so if you understand how to validate the 2821 02:01:51,800 --> 02:01:53,679 query parameter you then can validate 2822 02:01:53,679 --> 02:01:55,960 very easily the request body so here's 2823 02:01:55,960 --> 02:01:59,480 what we'll do we'll go down to uh our 2824 02:01:59,480 --> 02:02:03,320 post request for the API users endpoint 2825 02:02:03,320 --> 02:02:05,119 and right before our final request 2826 02:02:05,119 --> 02:02:07,599 Handler we'll go ahead and call the body 2827 02:02:07,599 --> 02:02:10,520 function and pass that as an argument 2828 02:02:10,520 --> 02:02:12,920 into appost so it's going to go ahead 2829 02:02:12,920 --> 02:02:15,599 and call this body middleware function 2830 02:02:15,599 --> 02:02:18,000 first and then perform the validation 2831 02:02:18,000 --> 02:02:19,480 and then it's going to go ahead and call 2832 02:02:19,480 --> 02:02:21,800 that next middleware that final request 2833 02:02:21,800 --> 02:02:24,280 Handler function right over here and 2834 02:02:24,280 --> 02:02:25,599 then similar to what we did with the 2835 02:02:25,599 --> 02:02:28,400 query parameter in that request Handler 2836 02:02:28,400 --> 02:02:31,639 function we want to actually uh use that 2837 02:02:31,639 --> 02:02:33,639 validation result function to see if 2838 02:02:33,639 --> 02:02:37,320 there were any errors at all okay so 2839 02:02:37,320 --> 02:02:39,280 here's what we'll do so now we'll go 2840 02:02:39,280 --> 02:02:42,239 ahead and specify what field that we 2841 02:02:42,239 --> 02:02:45,679 want to validate on the request body so 2842 02:02:45,679 --> 02:02:48,040 for the request body I want to validate 2843 02:02:48,040 --> 02:02:50,040 the username 2844 02:02:50,040 --> 02:02:53,079 and then I also want to verify that is 2845 02:02:53,079 --> 02:02:56,400 not empty by calling the not 2846 02:02:56,400 --> 02:03:00,560 empty function and I'll use with message 2847 02:03:00,560 --> 02:03:04,280 username cannot be 2848 02:03:04,280 --> 02:03:08,719 empty and then I want to make sure that 2849 02:03:08,719 --> 02:03:10,920 the max 2850 02:03:10,920 --> 02:03:15,719 is let's do 32 characters so is 2851 02:03:15,719 --> 02:03:20,040 length we'll do a Min of five characters 2852 02:03:20,040 --> 02:03:23,159 and Max of uh 2853 02:03:23,159 --> 02:03:25,360 32 and then with 2854 02:03:25,360 --> 02:03:31,639 message username must be at least five 2855 02:03:31,639 --> 02:03:34,079 to five 2856 02:03:34,079 --> 02:03:38,400 characters with a Max of 32 2857 02:03:38,400 --> 02:03:41,760 characters and then um let's 2858 02:03:41,760 --> 02:03:45,880 see oh let's also make sure it's a 2859 02:03:46,320 --> 02:03:50,159 string username must be a 2860 02:03:50,159 --> 02:03:52,440 string all right and let's go ahead and 2861 02:03:52,440 --> 02:03:55,800 try to make a request a post request to 2862 02:03:55,800 --> 02:03:57,760 this 2863 02:03:57,760 --> 02:04:01,840 endpoint uh whoops let me see oh let me 2864 02:04:01,840 --> 02:04:04,960 go ahead and do this before we do the 2865 02:04:04,960 --> 02:04:07,480 post request I forgot that we need to go 2866 02:04:07,480 --> 02:04:10,159 ahead and call 2867 02:04:10,639 --> 02:04:12,920 that validation 2868 02:04:12,920 --> 02:04:16,079 result function so let's do that 2869 02:04:16,079 --> 02:04:19,639 validation result result okay so cons 2870 02:04:19,639 --> 02:04:21,639 result equals validation 2871 02:04:21,639 --> 02:04:24,239 result and then let's conso log result 2872 02:04:24,239 --> 02:04:26,920 and see what 2873 02:04:27,880 --> 02:04:32,719 happens so I'll send this request 2874 02:04:32,719 --> 02:04:35,880 uh oh whoops I'm sorry I was supposed to 2875 02:04:35,880 --> 02:04:37,400 pass in request not result for the 2876 02:04:37,400 --> 02:04:39,719 validation result sorry about that okay 2877 02:04:39,719 --> 02:04:42,119 there we go that works okay so let's see 2878 02:04:42,119 --> 02:04:44,360 what the problem is okay so now we can 2879 02:04:44,360 --> 02:04:46,719 see that username cannot be empty uh 2880 02:04:46,719 --> 02:04:47,760 username must be at least five 2881 02:04:47,760 --> 02:04:49,000 characters with a Max of 32 two 2882 02:04:49,000 --> 02:04:51,440 characters so we do have our errors so 2883 02:04:51,440 --> 02:04:54,400 that's good okay now here's the thing 2884 02:04:54,400 --> 02:04:57,159 though I want to validate not just one 2885 02:04:57,159 --> 02:04:59,199 field but I want to validate multiple 2886 02:04:59,199 --> 02:05:01,079 Fields as well I don't want to just 2887 02:05:01,079 --> 02:05:03,360 validate the username field okay so what 2888 02:05:03,360 --> 02:05:07,000 we can do is we can actually pass an 2889 02:05:07,000 --> 02:05:11,159 array of body function calls so instead 2890 02:05:11,159 --> 02:05:14,159 of having to pass it in like this and 2891 02:05:14,159 --> 02:05:16,360 then uh you know pass in another 2892 02:05:16,360 --> 02:05:17,520 middleware function let's say if I 2893 02:05:17,520 --> 02:05:18,920 wanted to just very quickly validate 2894 02:05:18,920 --> 02:05:23,040 display name and do not empty like 2895 02:05:23,040 --> 02:05:24,639 this 2896 02:05:24,639 --> 02:05:27,920 okay let me make a request and you can 2897 02:05:27,920 --> 02:05:30,280 see now let me zoom out a little 2898 02:05:30,280 --> 02:05:32,719 bit you can see now I have four errors 2899 02:05:32,719 --> 02:05:36,280 all like this instead of just passing 2900 02:05:36,280 --> 02:05:39,559 it uh individually like different 2901 02:05:39,559 --> 02:05:41,520 arguments we can actually just pass it 2902 02:05:41,520 --> 02:05:44,400 as array like this so we pass in one 2903 02:05:44,400 --> 02:05:47,679 array and then it'll call all of 2904 02:05:47,679 --> 02:05:52,040 these not that it will matter all that 2905 02:05:52,040 --> 02:05:55,239 much because in the end it will still 2906 02:05:55,239 --> 02:05:57,800 validate everything for you so let's go 2907 02:05:57,800 --> 02:06:00,079 back to thunder client and we'll send 2908 02:06:00,079 --> 02:06:03,239 some requests now with some valid 2909 02:06:03,239 --> 02:06:08,320 data so let's go to Local Host Port 2000 2910 02:06:08,320 --> 02:06:10,480 API 2911 02:06:10,480 --> 02:06:13,400 users username 2912 02:06:13,400 --> 02:06:16,760 Anon display 2913 02:06:16,760 --> 02:06:20,079 name Anon the 2914 02:06:20,079 --> 02:06:24,199 dev and if I look at my console you can 2915 02:06:24,199 --> 02:06:26,520 see that we have no errors the moment 2916 02:06:26,520 --> 02:06:29,280 that I omit any one of these 2917 02:06:29,280 --> 02:06:32,280 values I'm going to get an error in that 2918 02:06:32,280 --> 02:06:34,800 errors array right over there so if I 2919 02:06:34,800 --> 02:06:36,960 omit 2920 02:06:36,960 --> 02:06:40,599 username you can see that uh yep we get 2921 02:06:40,599 --> 02:06:43,239 the errors for username okay that's good 2922 02:06:43,239 --> 02:06:45,440 so let's go ahead and customize our 2923 02:06:45,440 --> 02:06:48,239 logic now because we obviously don't 2924 02:06:48,239 --> 02:06:51,280 want to do anything at all when there 2925 02:06:51,280 --> 02:06:52,400 are 2926 02:06:52,400 --> 02:06:56,000 errors in our case we don't so we want 2927 02:06:56,000 --> 02:06:58,119 to make sure that we can actually check 2928 02:06:58,119 --> 02:07:00,440 to see if there are errors or not on 2929 02:07:00,440 --> 02:07:02,920 this result object and to do that 2930 02:07:02,920 --> 02:07:06,719 there's actually this is empty function 2931 02:07:06,719 --> 02:07:08,559 and this returns true or 2932 02:07:08,559 --> 02:07:11,239 false and it returns true if there are 2933 02:07:11,239 --> 02:07:13,559 no errors and if there are errors it 2934 02:07:13,559 --> 02:07:17,639 will return false so what I can do is I 2935 02:07:17,639 --> 02:07:20,280 can useing if statement and I'll just do 2936 02:07:20,280 --> 02:07:23,079 if there are no errors so if result is 2937 02:07:23,079 --> 02:07:24,599 not 2938 02:07:24,599 --> 02:07:27,040 empty then I'm going to go ahead and 2939 02:07:27,040 --> 02:07:28,239 return 2940 02:07:28,239 --> 02:07:30,880 response I'm going to set the status to 2941 02:07:30,880 --> 02:07:34,880 400 the status code to 400 and I want to 2942 02:07:34,880 --> 02:07:38,040 send back the errors so I'll just send 2943 02:07:38,040 --> 02:07:40,679 an object or yeah an object with the 2944 02:07:40,679 --> 02:07:42,280 errors property and to actually get the 2945 02:07:42,280 --> 02:07:44,480 errors you can reference 2946 02:07:44,480 --> 02:07:47,360 result and then you can just call this 2947 02:07:47,360 --> 02:07:50,159 array method and this will just give you 2948 02:07:50,159 --> 02:07:52,159 all the validation errors as an array as 2949 02:07:52,159 --> 02:07:54,000 it says over here gets the validation 2950 02:07:54,000 --> 02:07:57,239 errors as an array you can map it if you 2951 02:07:57,239 --> 02:08:00,440 want if you want to alter what is sent 2952 02:08:00,440 --> 02:08:03,040 back uh based on that errors array you 2953 02:08:03,040 --> 02:08:05,639 can do that if you want let's go ahead 2954 02:08:05,639 --> 02:08:09,920 and try this out okay so let's omit the 2955 02:08:09,920 --> 02:08:12,679 display name and you can see now I get 2956 02:08:12,679 --> 02:08:15,800 that 400 status code which means bad 2957 02:08:15,800 --> 02:08:17,960 request and that's typical whenever you 2958 02:08:17,960 --> 02:08:20,000 send an invalid payload so in this case 2959 02:08:20,000 --> 02:08:22,840 we're sending a payload with no display 2960 02:08:22,840 --> 02:08:26,040 name and you can see I get the error uh 2961 02:08:26,040 --> 02:08:29,440 invalid value path display name location 2962 02:08:29,440 --> 02:08:31,400 body now of course ideally you would 2963 02:08:31,400 --> 02:08:32,840 want to clean this up because you want 2964 02:08:32,840 --> 02:08:35,880 to have something that is more um 2965 02:08:35,880 --> 02:08:37,400 understanding to the 2966 02:08:37,400 --> 02:08:40,199 client so that they understand it very 2967 02:08:40,199 --> 02:08:42,320 easily but I'll let you all take care of 2968 02:08:42,320 --> 02:08:44,320 that and if I omit the username you can 2969 02:08:44,320 --> 02:08:47,480 see now all of the errors let me zoom on 2970 02:08:47,480 --> 02:08:50,400 a little bit whoops 2971 02:08:50,719 --> 02:08:52,559 you can see now all of the errors appear 2972 02:08:52,559 --> 02:08:55,199 right over here okay so hopefully that 2973 02:08:55,199 --> 02:08:58,079 makes sense so we're not quite done yet 2974 02:08:58,079 --> 02:09:01,159 because we still need to actually save 2975 02:09:01,159 --> 02:09:03,960 the valid data to our uh quote unquote 2976 02:09:03,960 --> 02:09:06,119 database or our array in this case of 2977 02:09:06,119 --> 02:09:09,440 users right now we're still using this 2978 02:09:09,440 --> 02:09:12,079 body object that comes from the request 2979 02:09:12,079 --> 02:09:14,960 object and this data can be either valid 2980 02:09:14,960 --> 02:09:17,960 or invalid we don't know okay but since 2981 02:09:17,960 --> 02:09:19,800 we are using Express validator we want 2982 02:09:19,800 --> 02:09:21,480 to make sure that we are using the 2983 02:09:21,480 --> 02:09:23,520 validated data of course there are 2984 02:09:23,520 --> 02:09:25,520 different ways that you can handle this 2985 02:09:25,520 --> 02:09:28,520 since at this point we are throwing an 2986 02:09:28,520 --> 02:09:31,360 error or returning a status code of 400 2987 02:09:31,360 --> 02:09:34,840 we can safely assume that the data is 2988 02:09:34,840 --> 02:09:37,199 valid by just referencing body Dot and 2989 02:09:37,199 --> 02:09:40,840 then whatever field name we want but 2990 02:09:40,840 --> 02:09:43,079 instead what we can do is we can 2991 02:09:43,079 --> 02:09:45,960 actually use this function called 2992 02:09:45,960 --> 02:09:49,040 matched data so let me import that up 2993 02:09:49,040 --> 02:09:51,800 there so match data is imported from 2994 02:09:51,800 --> 02:09:54,159 Express validator and then I'm going to 2995 02:09:54,159 --> 02:09:57,960 go back down here and what I can do is I 2996 02:09:57,960 --> 02:10:01,800 can simply just call let's do this const 2997 02:10:01,800 --> 02:10:04,719 data equals matched 2998 02:10:04,719 --> 02:10:08,360 data okay and just pass in the request 2999 02:10:08,360 --> 02:10:10,159 object like 3000 02:10:10,159 --> 02:10:13,280 this and then what this will do is it'll 3001 02:10:13,280 --> 02:10:16,360 grab you all of that 3002 02:10:16,360 --> 02:10:19,199 data that has been validated needed so 3003 02:10:19,199 --> 02:10:21,880 I'll console log this so you all can 3004 02:10:21,880 --> 02:10:26,280 see so let's get the username and let's 3005 02:10:26,280 --> 02:10:29,760 pass in a display 3006 02:10:30,320 --> 02:10:33,000 name so now I can see that I have 3007 02:10:33,000 --> 02:10:36,360 username and display name right over 3008 02:10:36,360 --> 02:10:39,159 here okay and this object that is being 3009 02:10:39,159 --> 02:10:40,440 logged 3010 02:10:40,440 --> 02:10:43,960 is the Matched data return value which 3011 02:10:43,960 --> 02:10:47,079 is pretty much the validated data so I 3012 02:10:47,079 --> 02:10:50,480 would recommend you to to use this data 3013 02:10:50,480 --> 02:10:53,199 object instead of the request body so 3014 02:10:53,199 --> 02:10:55,880 let's just fix that real quick so let's 3015 02:10:55,880 --> 02:11:01,159 remove uh this these two lines and then 3016 02:11:01,159 --> 02:11:04,320 let's see we're going to replace this 3017 02:11:04,320 --> 02:11:08,360 reference of body with 3018 02:11:08,360 --> 02:11:10,599 data and I think that's all we need to 3019 02:11:10,599 --> 02:11:13,239 do all right and that is pretty much it 3020 02:11:13,239 --> 02:11:15,639 that is how you can validate request 3021 02:11:15,639 --> 02:11:18,360 bodies for your post requests and you 3022 02:11:18,360 --> 02:11:21,040 can do the same thing for put request 3023 02:11:21,040 --> 02:11:22,400 for patch 3024 02:11:22,400 --> 02:11:25,239 request and really any request method 3025 02:11:25,239 --> 02:11:27,840 that takes a request body and since you 3026 02:11:27,840 --> 02:11:31,119 know how to validate uh validate request 3027 02:11:31,119 --> 02:11:32,520 bodies and also validate query 3028 02:11:32,520 --> 02:11:34,800 parameters you should now know how to 3029 02:11:34,800 --> 02:11:36,960 validate other things too such as 3030 02:11:36,960 --> 02:11:40,239 headers cookies route parameters it's 3031 02:11:40,239 --> 02:11:42,679 all the same thing now very quickly I 3032 02:11:42,679 --> 02:11:44,559 wanted to show you all how you can use a 3033 02:11:44,559 --> 02:11:48,280 schema in Express validator to make make 3034 02:11:48,280 --> 02:11:49,760 your code look a lot more cleaner 3035 02:11:49,760 --> 02:11:52,440 because right now if I look at this part 3036 02:11:52,440 --> 02:11:55,000 right over here um I have a bunch of 3037 02:11:55,000 --> 02:11:56,520 validation going on and this is just for 3038 02:11:56,520 --> 02:11:58,800 only two fields and you can imagine if 3039 02:11:58,800 --> 02:12:01,360 you have a lot of different fields that 3040 02:12:01,360 --> 02:12:05,239 is being sent to uh the the server this 3041 02:12:05,239 --> 02:12:06,480 can start to look more and more 3042 02:12:06,480 --> 02:12:09,079 cluttered so we can use a schema to make 3043 02:12:09,079 --> 02:12:11,639 things look a lot more cleaner and a 3044 02:12:11,639 --> 02:12:14,239 schema really just is an object that has 3045 02:12:14,239 --> 02:12:17,760 all of your validators defined so 3046 02:12:17,760 --> 02:12:20,679 instead of having to have all of this 3047 02:12:20,679 --> 02:12:22,320 being chained after calling each 3048 02:12:22,320 --> 02:12:24,639 function all you do is you use this 3049 02:12:24,639 --> 02:12:26,480 check schema function and you pass in 3050 02:12:26,480 --> 02:12:28,880 the schema which is just an object and 3051 02:12:28,880 --> 02:12:30,840 then what that function will do is it'll 3052 02:12:30,840 --> 02:12:34,480 create a list of validation chains so 3053 02:12:34,480 --> 02:12:37,159 that way it can just save you a couple 3054 02:12:37,159 --> 02:12:38,880 lines of code and make it look a lot 3055 02:12:38,880 --> 02:12:41,400 more readable so what I'll do is I'll 3056 02:12:41,400 --> 02:12:44,199 create a new folder called utils and 3057 02:12:44,199 --> 02:12:45,960 I'll create a new folder inside this 3058 02:12:45,960 --> 02:12:48,639 folder or not not inside uh I'll create 3059 02:12:48,639 --> 02:12:49,920 not a folder I'll create a new file 3060 02:12:49,920 --> 02:12:51,440 called 3061 02:12:51,440 --> 02:12:54,599 validation schemas because I want to 3062 02:12:54,599 --> 02:12:56,559 keep everything 3063 02:12:56,559 --> 02:13:00,840 separate and then what I'll do is I'll 3064 02:13:00,840 --> 02:13:05,960 create a variable called create user 3065 02:13:05,960 --> 02:13:08,719 validation schema I always like to be 3066 02:13:08,719 --> 02:13:11,119 verose with my variable names but it's 3067 02:13:11,119 --> 02:13:12,719 up to 3068 02:13:12,719 --> 02:13:15,360 you and then now what I want to do is I 3069 02:13:15,360 --> 02:13:17,320 want to define the field that I want to 3070 02:13:17,320 --> 02:13:18,760 validate so in this case I want to 3071 02:13:18,760 --> 02:13:20,800 validate the username field so I'm going 3072 02:13:20,800 --> 02:13:23,040 to use that as a field inside this 3073 02:13:23,040 --> 02:13:26,280 object create user validation schema and 3074 02:13:26,280 --> 02:13:29,599 then for username this will be an object 3075 02:13:29,599 --> 02:13:31,840 and inside this object is where you will 3076 02:13:31,840 --> 02:13:35,760 specify what you want to validate so if 3077 02:13:35,760 --> 02:13:39,719 I wanted to validate the length so what 3078 02:13:39,719 --> 02:13:41,559 you would do is you would just take the 3079 02:13:41,559 --> 02:13:43,320 name of this method they're going to 3080 02:13:43,320 --> 02:13:44,840 it's going to be the same exact thing so 3081 02:13:44,840 --> 02:13:46,679 whatever the name of the method is is 3082 02:13:46,679 --> 02:13:48,360 going to be the name of the field field 3083 02:13:48,360 --> 02:13:50,199 that you're going to configure inside 3084 02:13:50,199 --> 02:13:53,840 this object so for example is length I'm 3085 02:13:53,840 --> 02:13:55,719 going to go ahead and type is length 3086 02:13:55,719 --> 02:13:58,280 just like this and so this is going to 3087 02:13:58,280 --> 02:13:59,719 be an 3088 02:13:59,719 --> 02:14:03,559 object and you want to pass in these 3089 02:14:03,559 --> 02:14:05,480 options so 3090 02:14:05,480 --> 02:14:07,280 options and then it's going to be the 3091 02:14:07,280 --> 02:14:08,960 same exact thing you can pass in Min and 3092 02:14:08,960 --> 02:14:13,000 Max for options so Min of five so this 3093 02:14:13,000 --> 02:14:15,040 will be a minimum of five characters Max 3094 02:14:15,040 --> 02:14:16,880 of 32 3095 02:14:16,880 --> 02:14:19,599 characters so this is how you can 3096 02:14:19,599 --> 02:14:20,719 validate the 3097 02:14:20,719 --> 02:14:24,000 username and its length let's do the 3098 02:14:24,000 --> 02:14:26,400 same thing for uh the not empty 3099 02:14:26,400 --> 02:14:29,079 validator so again you take the same 3100 02:14:29,079 --> 02:14:31,119 method name not empty you can even just 3101 02:14:31,119 --> 02:14:34,040 copy and paste it uh whoops not there 3102 02:14:34,040 --> 02:14:37,000 right over here now since not empty does 3103 02:14:37,000 --> 02:14:39,320 not have any options we can just set 3104 02:14:39,320 --> 02:14:41,480 this to true but of course if we did 3105 02:14:41,480 --> 02:14:44,559 provide options we could just map this 3106 02:14:44,559 --> 02:14:46,920 to an object and do the same thing like 3107 02:14:46,920 --> 02:14:49,400 this options like 3108 02:14:49,400 --> 02:14:52,800 that same thing for the is string 3109 02:14:52,800 --> 02:14:54,480 validator we don't have any options 3110 02:14:54,480 --> 02:14:56,719 configured for that so I can just take 3111 02:14:56,719 --> 02:14:59,719 the same exact method name and then set 3112 02:14:59,719 --> 02:15:01,760 it set its value to 3113 02:15:01,760 --> 02:15:04,400 true however if you did have an error 3114 02:15:04,400 --> 02:15:06,000 message like a custom error message like 3115 02:15:06,000 --> 02:15:09,000 we have over here we can also specify 3116 02:15:09,000 --> 02:15:12,760 that as well so for is length inside the 3117 02:15:12,760 --> 02:15:15,719 is length object right over here I can 3118 02:15:15,719 --> 02:15:17,960 go ahead and specify this error message 3119 02:15:17,960 --> 02:15:21,000 field like this and I'll just go ahead 3120 02:15:21,000 --> 02:15:21,760 and 3121 02:15:21,760 --> 02:15:25,040 copy this whole string and paste this 3122 02:15:25,040 --> 02:15:26,599 right over here like 3123 02:15:26,599 --> 02:15:30,639 that now since uh not empty does have a 3124 02:15:30,639 --> 02:15:32,800 custom error message instead of setting 3125 02:15:32,800 --> 02:15:33,800 this 3126 02:15:33,800 --> 02:15:37,880 true what I'll do is I will provide an 3127 02:15:37,880 --> 02:15:43,280 object like this error message and then 3128 02:15:43,280 --> 02:15:48,239 that okay and then what we'll also do 3129 02:15:48,239 --> 02:15:51,199 same thing for is 3130 02:15:51,559 --> 02:15:57,239 string let's copy that and do 3131 02:15:57,800 --> 02:16:00,760 this and of course since you have this 3132 02:16:00,760 --> 02:16:02,400 object over here it pretty much implies 3133 02:16:02,400 --> 02:16:04,239 that you are going to obviously check 3134 02:16:04,239 --> 02:16:05,440 for these two validators so you don't 3135 02:16:05,440 --> 02:16:08,079 need to worry about the Boolean value 3136 02:16:08,079 --> 02:16:11,360 anymore and uh let's see what 3137 02:16:11,360 --> 02:16:14,199 else um okay yeah that's pretty much all 3138 02:16:14,199 --> 02:16:17,079 we need for this so now to actually use 3139 02:16:17,079 --> 02:16:20,159 the schema what we'll do is let's remove 3140 02:16:20,159 --> 02:16:21,920 all of this let me just make sure we 3141 02:16:21,920 --> 02:16:23,400 have 3142 02:16:23,400 --> 02:16:27,679 everything uh 3143 02:16:27,679 --> 02:16:30,559 username oh whoops that was only for 3144 02:16:30,559 --> 02:16:33,200 username uh let's do display name I 3145 02:16:33,200 --> 02:16:34,760 forgot about 3146 02:16:34,760 --> 02:16:36,718 that display 3147 02:16:36,718 --> 02:16:38,840 name and I think the only validation we 3148 02:16:38,840 --> 02:16:43,160 had was not empty so let's do not empty 3149 02:16:43,160 --> 02:16:46,200 and then let's 3150 02:16:46,679 --> 02:16:50,920 just we'll just do true for now 3151 02:16:50,920 --> 02:16:53,959 okay let me go ahead and remove this 3152 02:16:53,959 --> 02:16:56,599 whole array and then now we're just 3153 02:16:56,599 --> 02:16:59,599 going to call this check schem of 3154 02:16:59,599 --> 02:17:02,799 function so let me go up top over here 3155 02:17:02,799 --> 02:17:06,879 and import check schema just like 3156 02:17:06,879 --> 02:17:09,080 this and let me go back to the post 3157 02:17:09,080 --> 02:17:11,240 request so right over here we're going 3158 02:17:11,240 --> 02:17:13,040 to go ahead and call check 3159 02:17:13,040 --> 02:17:16,080 schema and then now you're going to pass 3160 02:17:16,080 --> 02:17:19,318 in that schema definition which is just 3161 02:17:19,318 --> 02:17:20,920 this object that we created so let's 3162 02:17:20,920 --> 02:17:24,760 import that so up top over here we'll 3163 02:17:24,760 --> 02:17:29,280 import from utils validation schemas 3164 02:17:29,280 --> 02:17:31,318 create user validation schema just like 3165 02:17:31,318 --> 02:17:35,398 that and I'll pass that object in here 3166 02:17:35,398 --> 02:17:38,200 and let's actually see what 3167 02:17:38,200 --> 02:17:41,920 happens so if I go into Thunder client 3168 02:17:41,920 --> 02:17:43,920 let's make a new 3169 02:17:43,920 --> 02:17:48,638 request to Local Host Port 3000 3170 02:17:48,638 --> 02:17:50,080 okay so I am getting an error in the 3171 02:17:50,080 --> 02:17:51,160 console and I think it's complaining 3172 02:17:51,160 --> 02:17:53,398 about the import uh I think it's because 3173 02:17:53,398 --> 02:17:55,799 I'm missing the extension at the end 3174 02:17:55,799 --> 02:17:59,240 tojs but I realized that I uh am 3175 02:17:59,240 --> 02:18:01,599 supposed to use MJS so let me just 3176 02:18:01,599 --> 02:18:04,879 quickly fix 3177 02:18:04,879 --> 02:18:08,760 this and add that MJS extension at the 3178 02:18:08,760 --> 02:18:10,558 end because if I don't have that 3179 02:18:10,558 --> 02:18:12,359 extension it's going to throw this error 3180 02:18:12,359 --> 02:18:15,080 but if I add that extension at the end 3181 02:18:15,080 --> 02:18:17,920 the error will be fixed okay so I just 3182 02:18:17,920 --> 02:18:21,120 wanted to mention that very quickly but 3183 02:18:21,120 --> 02:18:24,240 now if I were to go back into the 3184 02:18:24,240 --> 02:18:26,200 Thunder client and let's try to make a 3185 02:18:26,200 --> 02:18:28,280 post request 3186 02:18:28,280 --> 02:18:31,120 now you can see that now I get the 3187 02:18:31,120 --> 02:18:32,840 errors right over here it's complaining 3188 02:18:32,840 --> 02:18:36,280 about the display name the username and 3189 02:18:36,280 --> 02:18:38,558 all of the different errors are that's 3190 02:18:38,558 --> 02:18:42,879 going on with it if I pass in let's say 3191 02:18:42,879 --> 02:18:45,359 username and let's let's do let's say if 3192 02:18:45,359 --> 02:18:47,760 I pass in just two characters for 3193 02:18:47,760 --> 02:18:49,840 username you can see that it's going to 3194 02:18:49,840 --> 02:18:51,478 complain about username must be at least 3195 02:18:51,478 --> 02:18:53,840 five characters with a Max of 32 3196 02:18:53,840 --> 02:18:57,120 characters and the other two validator 3197 02:18:57,120 --> 02:19:00,439 errors are not there because we actually 3198 02:19:00,439 --> 02:19:01,599 provided a 3199 02:19:01,599 --> 02:19:05,359 value and it is not 3200 02:19:05,359 --> 02:19:07,478 undefined so yeah it's a string and it's 3201 02:19:07,478 --> 02:19:08,160 not 3202 02:19:08,160 --> 02:19:10,160 empty okay and then you can see that 3203 02:19:10,160 --> 02:19:12,760 since we don't have display name that is 3204 02:19:12,760 --> 02:19:15,000 not uh since we didn't provide that in 3205 02:19:15,000 --> 02:19:16,959 the request body the error appears right 3206 02:19:16,959 --> 02:19:18,478 over here 3207 02:19:18,478 --> 02:19:20,439 okay so hopefully that makes sense and 3208 02:19:20,439 --> 02:19:22,840 hopefully you can start to understand 3209 02:19:22,840 --> 02:19:26,000 how using schemas to validate your 3210 02:19:26,000 --> 02:19:28,718 request bodies is a lot more easier than 3211 02:19:28,718 --> 02:19:30,558 just having all of your 3212 02:19:30,558 --> 02:19:34,280 validators in the same uh in the same uh 3213 02:19:34,280 --> 02:19:36,638 file and just chaining them after each 3214 02:19:36,638 --> 02:19:39,318 call okay so I would encourage you to do 3215 02:19:39,318 --> 02:19:43,160 the same thing for this query validation 3216 02:19:43,160 --> 02:19:45,280 that we did earlier for this get request 3217 02:19:45,280 --> 02:19:46,840 I'll let you all take care of that 3218 02:19:46,840 --> 02:19:49,080 yourself just for exual practice so 3219 02:19:49,080 --> 02:19:52,560 hopefully this all made 3220 02:19:55,720 --> 02:19:58,280 sense so now what I want to do is I want 3221 02:19:58,280 --> 02:20:01,200 to show you all how we can organize all 3222 02:20:01,200 --> 02:20:05,120 of our requests using an Express router 3223 02:20:05,120 --> 02:20:07,000 the problem right now is even though we 3224 02:20:07,000 --> 02:20:10,000 only have a few routes to find as our 3225 02:20:10,000 --> 02:20:12,760 application grows we could have 50 3226 02:20:12,760 --> 02:20:16,000 routes 100 routes a whole bunch and you 3227 02:20:16,000 --> 02:20:17,960 obviously don't want all of it to be in 3228 02:20:17,960 --> 02:20:20,439 one single file you want to group 3229 02:20:20,439 --> 02:20:23,920 together your API endpoints based on 3230 02:20:23,920 --> 02:20:26,760 what is known as a domain so what I mean 3231 02:20:26,760 --> 02:20:29,880 by that is for example we 3232 02:20:29,880 --> 02:20:33,680 have a bunch of user endpoints and all 3233 02:20:33,680 --> 02:20:37,000 of this handles different operations so 3234 02:20:37,000 --> 02:20:38,880 that is known as the user domain the 3235 02:20:38,880 --> 02:20:41,920 user domain is everything related to the 3236 02:20:41,920 --> 02:20:44,600 user itself so when you create a user 3237 02:20:44,600 --> 02:20:46,000 when you grab all the users from the 3238 02:20:46,000 --> 02:20:47,880 database when you update a user that is 3239 02:20:47,880 --> 02:20:50,720 all part of the user domain if you see 3240 02:20:50,720 --> 02:20:52,560 right over here I have another products 3241 02:20:52,560 --> 02:20:54,319 endpoint that doesn't really have any 3242 02:20:54,319 --> 02:20:57,200 other endpoints to do much with such as 3243 02:20:57,200 --> 02:20:58,200 creating a 3244 02:20:58,200 --> 02:21:00,840 product and this would be the products 3245 02:21:00,840 --> 02:21:04,280 domain so anything related to operating 3246 02:21:04,280 --> 02:21:06,160 with the product domain would be things 3247 02:21:06,160 --> 02:21:08,439 such as creating a new product updating 3248 02:21:08,439 --> 02:21:10,640 a product deleting a product things like 3249 02:21:10,640 --> 02:21:13,240 that okay that's what I mean by domain 3250 02:21:13,240 --> 02:21:14,800 and what I want to do is I want to be 3251 02:21:14,800 --> 02:21:17,560 able to group together all of my users 3252 02:21:17,560 --> 02:21:19,840 endpoints separate from the products 3253 02:21:19,840 --> 02:21:21,040 endpoint because it doesn't make sense 3254 02:21:21,040 --> 02:21:24,240 to keep everything all together okay the 3255 02:21:24,240 --> 02:21:25,920 products endpoint should be grouped with 3256 02:21:25,920 --> 02:21:29,000 everything related to products users 3257 02:21:29,000 --> 02:21:31,120 endpoint should be grouped together with 3258 02:21:31,120 --> 02:21:33,880 everything related to users if you have 3259 02:21:33,880 --> 02:21:36,319 endpoints that handle Payment Processing 3260 02:21:36,319 --> 02:21:38,600 maybe you're talking to let's say uh the 3261 02:21:38,600 --> 02:21:40,479 stripe API or some other Payment 3262 02:21:40,479 --> 02:21:42,520 Processing API then you would have a 3263 02:21:42,520 --> 02:21:44,000 payments domain and you want to group 3264 02:21:44,000 --> 02:21:46,680 all of your payments and points together 3265 02:21:46,680 --> 02:21:48,319 so we can use use an Express router to 3266 02:21:48,319 --> 02:21:50,240 do this and what I'll do is I'll create 3267 02:21:50,240 --> 02:21:53,120 a new folder inside the source folder 3268 02:21:53,120 --> 02:21:54,720 and I'll call this 3269 02:21:54,720 --> 02:21:56,560 routes and then I'm going to go ahead 3270 02:21:56,560 --> 02:22:01,720 and create a new file and call it users. 3271 02:22:02,920 --> 02:22:06,160 MJS and then inside users. MJS we're 3272 02:22:06,160 --> 02:22:09,359 going to import the 3273 02:22:09,359 --> 02:22:12,160 router from Express okay and this is a 3274 02:22:12,160 --> 02:22:14,680 function that we can call to create an 3275 02:22:14,680 --> 02:22:17,359 instance of an Express router it's it's 3276 02:22:17,359 --> 02:22:21,520 the same way of how we imported Express 3277 02:22:21,520 --> 02:22:23,359 like this and then we called the express 3278 02:22:23,359 --> 02:22:25,680 function the router import is also a 3279 02:22:25,680 --> 02:22:28,280 function so I'm going to declare a 3280 02:22:28,280 --> 02:22:30,840 variable and we'll assign the return 3281 02:22:30,840 --> 02:22:33,520 value of the router function 3282 02:22:33,520 --> 02:22:38,040 call to this router variable which is in 3283 02:22:38,040 --> 02:22:40,080 which has a lowercase R okay so these 3284 02:22:40,080 --> 02:22:42,160 two are different obviously so now we 3285 02:22:42,160 --> 02:22:45,439 have our router and the nice thing about 3286 02:22:45,439 --> 02:22:47,760 this router is it has pretty pretty much 3287 02:22:47,760 --> 02:22:49,760 almost all the same exact methods and 3288 02:22:49,760 --> 02:22:52,640 properties that the express app instance 3289 02:22:52,640 --> 02:22:55,600 has so you can see that I can reference 3290 02:22:55,600 --> 02:22:59,040 the get method the post method delete 3291 02:22:59,040 --> 02:23:01,240 all that kind of stuff okay the router 3292 02:23:01,240 --> 02:23:03,399 itself is pretty much like a mini 3293 02:23:03,399 --> 02:23:06,399 application in your entire Express app 3294 02:23:06,399 --> 02:23:07,920 that can group together all of your 3295 02:23:07,920 --> 02:23:09,920 requests so you can register requests on 3296 02:23:09,920 --> 02:23:12,160 the router but then you also need to 3297 02:23:12,160 --> 02:23:15,640 register that router to express okay so 3298 02:23:15,640 --> 02:23:17,880 hopefully that makes sense so the same 3299 02:23:17,880 --> 02:23:20,319 way that I am registering let's say this 3300 02:23:20,319 --> 02:23:24,000 get request right over here for/ API 3301 02:23:24,000 --> 02:23:27,200 users I can do it do it on the router so 3302 02:23:27,200 --> 02:23:30,760 let me go ahead and copy uh let's say 3303 02:23:30,760 --> 02:23:33,479 this right over here it takes the same 3304 02:23:33,479 --> 02:23:36,120 exact method arguments you can see it 3305 02:23:36,120 --> 02:23:38,439 takes a path so let me paste the path 3306 02:23:38,439 --> 02:23:41,279 right over there so/ API users and then 3307 02:23:41,279 --> 02:23:43,960 it takes uh as many handlers as you want 3308 02:23:43,960 --> 02:23:46,760 to pass or middleware functions so we 3309 02:23:46,760 --> 02:23:49,720 can literally pass this exact 3310 02:23:49,720 --> 02:23:52,560 same these exact same 3311 02:23:52,560 --> 02:23:56,880 arguments right over here like this okay 3312 02:23:56,880 --> 02:23:58,720 let's just import that query function 3313 02:23:58,720 --> 02:24:01,319 from Express 3314 02:24:02,520 --> 02:24:04,319 validator and this is something that we 3315 02:24:04,319 --> 02:24:05,800 did in the previous section where we 3316 02:24:05,800 --> 02:24:08,279 talked about validation in 3317 02:24:08,279 --> 02:24:12,000 Express okay so next thing we need to do 3318 02:24:12,000 --> 02:24:14,640 is export this router and then we need 3319 02:24:14,640 --> 02:24:19,000 to import it into our our main index. 3320 02:24:19,000 --> 02:24:21,960 MJS file where we have the express app 3321 02:24:21,960 --> 02:24:23,479 instance because we need to register our 3322 02:24:23,479 --> 02:24:26,640 router with the main Express app in 3323 02:24:26,640 --> 02:24:28,120 order for the main Express app to 3324 02:24:28,120 --> 02:24:30,800 actually have these routes mapped out so 3325 02:24:30,800 --> 02:24:33,040 users can actually visit it or clients 3326 02:24:33,040 --> 02:24:36,920 can actually visit it so I'll 3327 02:24:36,920 --> 02:24:39,399 export uh I'll actually export this as a 3328 02:24:39,399 --> 02:24:42,080 default so export 3329 02:24:42,080 --> 02:24:45,080 default router just like 3330 02:24:45,080 --> 02:24:47,319 that and then next thing thing that 3331 02:24:47,319 --> 02:24:51,200 we'll do is we'll go into our index. MJS 3332 02:24:51,200 --> 02:24:54,040 file and we want to import 3333 02:24:54,040 --> 02:24:57,800 that router that user router so import 3334 02:24:57,800 --> 02:25:01,279 user users router 3335 02:25:01,279 --> 02:25:04,439 from uh and then the folder is 3336 02:25:04,439 --> 02:25:08,240 routes and then users. MJS let's make 3337 02:25:08,240 --> 02:25:09,800 sure we don't have any errors in the 3338 02:25:09,800 --> 02:25:11,800 console okay everything's good with the 3339 02:25:11,800 --> 02:25:14,960 import so we can register this router 3340 02:25:14,960 --> 02:25:17,600 now so that way um we can actually 3341 02:25:17,600 --> 02:25:20,479 access the endpoint defined at the 3342 02:25:20,479 --> 02:25:23,920 router level over here by simply just 3343 02:25:23,920 --> 02:25:29,040 using app.use just like this and then 3344 02:25:29,040 --> 02:25:31,960 you pass the user router like that and 3345 02:25:31,960 --> 02:25:34,040 since we have the same endpoint defined 3346 02:25:34,040 --> 02:25:35,439 at the router level we want to make sure 3347 02:25:35,439 --> 02:25:39,600 we remove this endpoint definition over 3348 02:25:39,600 --> 02:25:43,840 here so let's just remove that okay so 3349 02:25:43,840 --> 02:25:47,560 now let's go ahead and try to access 3350 02:25:47,560 --> 02:25:50,359 that endpoint using the Thunder client 3351 02:25:50,359 --> 02:25:52,000 so we're going to try and make a get 3352 02:25:52,000 --> 02:25:55,120 request to/ API 3353 02:25:55,120 --> 02:25:58,560 users so let's do 3354 02:25:58,560 --> 02:26:01,160 that and it says validation result is 3355 02:26:01,160 --> 02:26:03,080 not defined oh whoops I need to also 3356 02:26:03,080 --> 02:26:07,240 import validation result as 3357 02:26:07,399 --> 02:26:10,080 well and let's try to make a request 3358 02:26:10,080 --> 02:26:12,800 mock users uh is not defined oh that's 3359 02:26:12,800 --> 02:26:16,120 right because um yeah we're using mocku 3360 02:26:16,120 --> 02:26:20,040 okay let me quick do this I need to 3361 02:26:20,040 --> 02:26:22,479 move this mock users 3362 02:26:22,479 --> 02:26:25,880 array into a separate file so I can 3363 02:26:25,880 --> 02:26:27,880 export it from that file and import it 3364 02:26:27,880 --> 02:26:30,080 everywhere else I need it so I'll just 3365 02:26:30,080 --> 02:26:32,040 create a new file I'll call this 3366 02:26:32,040 --> 02:26:35,760 constants whoops 3367 02:26:36,359 --> 02:26:39,240 MJS and I'll export this mock users 3368 02:26:39,240 --> 02:26:41,600 constant and I'm going to remove this 3369 02:26:41,600 --> 02:26:44,040 right over here and let me just 3370 02:26:44,040 --> 02:26:47,800 quickly uh do this this let me import it 3371 02:26:47,800 --> 02:26:50,600 inside the index. MJS file because we 3372 02:26:50,600 --> 02:26:55,319 are using it here so up top I'll import 3373 02:26:55,319 --> 02:26:58,479 and this is a named export so mock 3374 02:26:58,479 --> 02:27:00,840 users 3375 02:27:00,840 --> 02:27:05,399 from utils constant. MJS and we'll do 3376 02:27:05,399 --> 02:27:08,479 the same thing inside the users. MJS 3377 02:27:08,479 --> 02:27:14,279 file so import mock users from and then 3378 02:27:14,279 --> 02:27:17,560 the pathway over here okay so no more 3379 02:27:17,560 --> 02:27:20,200 errors so that's good let's just try to 3380 02:27:20,200 --> 02:27:23,240 call this endpoint okay so you can see 3381 02:27:23,240 --> 02:27:25,479 that it works and that's good so we know 3382 02:27:25,479 --> 02:27:27,600 that it is working and if I were to 3383 02:27:27,600 --> 02:27:31,040 remove this app. use call and passing in 3384 02:27:31,040 --> 02:27:33,800 the user's router and save now if I try 3385 02:27:33,800 --> 02:27:35,080 to make a get request it's going to say 3386 02:27:35,080 --> 02:27:37,319 four for not found because we did not 3387 02:27:37,319 --> 02:27:40,279 register that router so hopefully this 3388 02:27:40,279 --> 02:27:43,439 makes sense so now what I can do is I 3389 02:27:43,439 --> 02:27:46,240 can take all of my end points that are 3390 02:27:46,240 --> 02:27:49,560 relevant into users and place it inside 3391 02:27:49,560 --> 02:27:52,720 this users. MJS file and then register 3392 02:27:52,720 --> 02:27:55,000 those endpoints at the router level so 3393 02:27:55,000 --> 02:27:58,560 let's do that so let's go ahead and do 3394 02:27:58,560 --> 02:28:02,920 the same thing for app. poost API users 3395 02:28:02,920 --> 02:28:05,000 so I'm going to go down here and I'm 3396 02:28:05,000 --> 02:28:07,720 going to reference router. poost and I'm 3397 02:28:07,720 --> 02:28:09,479 going to pass in those same exact 3398 02:28:09,479 --> 02:28:13,479 arguments that I had passed in to app 3399 02:28:13,479 --> 02:28:16,760 dopost so you can see right over here we 3400 02:28:16,760 --> 02:28:19,760 pass in for the first argument the path 3401 02:28:19,760 --> 02:28:21,800 and then all of our middleware functions 3402 02:28:21,800 --> 02:28:23,560 let's just make sure we have all of our 3403 02:28:23,560 --> 02:28:26,359 Imports so we need to import uh check 3404 02:28:26,359 --> 02:28:29,160 schema from Express 3405 02:28:29,160 --> 02:28:32,120 validator we need to import the create 3406 02:28:32,120 --> 02:28:35,080 user validation schema as well so let me 3407 02:28:35,080 --> 02:28:40,200 import that create user validation 3408 02:28:40,240 --> 02:28:44,600 schema okay and then uh let's also we 3409 02:28:44,600 --> 02:28:46,720 already have validation result imported 3410 02:28:46,720 --> 02:28:49,240 we are using the match data function 3411 02:28:49,240 --> 02:28:50,240 that comes 3412 02:28:50,240 --> 02:28:54,000 from Express validator so let's import 3413 02:28:54,000 --> 02:28:56,200 that and this should work so now let me 3414 02:28:56,200 --> 02:28:58,880 just go back to the index. MJS file 3415 02:28:58,880 --> 02:29:03,720 remove this app. poost because it's 3416 02:29:03,720 --> 02:29:06,600 redundant and uh let's go ahead and try 3417 02:29:06,600 --> 02:29:08,720 to make a post 3418 02:29:08,720 --> 02:29:10,560 request and you can see that it's 3419 02:29:10,560 --> 02:29:13,040 working just fine let me pass in a 3420 02:29:13,040 --> 02:29:15,439 request 3421 02:29:15,520 --> 02:29:18,520 body 3422 02:29:19,800 --> 02:29:20,640 and then 3423 02:29:20,640 --> 02:29:23,080 let's see the result and you can see 3424 02:29:23,080 --> 02:29:26,080 that we get a response back and of 3425 02:29:26,080 --> 02:29:29,359 course if I were to just remove 3426 02:29:29,359 --> 02:29:32,160 this it would say four for not 3427 02:29:32,160 --> 02:29:35,560 found Okay and like I said you could do 3428 02:29:35,560 --> 02:29:38,760 the same thing for all of 3429 02:29:38,760 --> 02:29:43,000 our end points for the users domain so 3430 02:29:43,000 --> 02:29:46,479 uh what I'll do is I'll move uh the get 3431 02:29:46,479 --> 02:29:49,319 user by ID in there so let me just copy 3432 02:29:49,319 --> 02:29:53,479 all of this and let me just delete 3433 02:29:53,680 --> 02:29:56,319 this so let's go up here I'll put this 3434 02:29:56,319 --> 02:29:59,080 right underneath 3435 02:29:59,080 --> 02:30:01,359 here okay and then we do need to resolve 3436 02:30:01,359 --> 02:30:03,920 index by user ID middleware function and 3437 02:30:03,920 --> 02:30:06,720 since that is defined inside the index. 3438 02:30:06,720 --> 02:30:09,479 MJS file we need to uh we need to of 3439 02:30:09,479 --> 02:30:11,520 course export it or whoops not this one 3440 02:30:11,520 --> 02:30:13,479 this one right over here let me do this 3441 02:30:13,479 --> 02:30:15,319 let me create a new file inside utils 3442 02:30:15,319 --> 02:30:20,120 I'll call this middle mware middlewares 3443 02:30:20,479 --> 02:30:24,160 MJS and let me export this and let me 3444 02:30:24,160 --> 02:30:28,640 make sure I am importing mock 3445 02:30:28,640 --> 02:30:31,640 users from constants 3446 02:30:31,640 --> 02:30:34,240 MJS okay this is good so now we can 3447 02:30:34,240 --> 02:30:37,479 import well first let me remove 3448 02:30:37,479 --> 02:30:41,680 this and uh I'll 3449 02:30:42,800 --> 02:30:47,080 import that middleware first from utils 3450 02:30:47,080 --> 02:30:49,439 middle 3451 02:30:50,040 --> 02:30:52,760 wees resolve index user by ID because 3452 02:30:52,760 --> 02:30:55,479 this index file is still using 3453 02:30:55,479 --> 02:30:59,120 it and then what I'll do is I'm going to 3454 02:30:59,120 --> 02:31:01,200 go ahead and import this inside the 3455 02:31:01,200 --> 02:31:04,439 users. MJS file so that gets imported 3456 02:31:04,439 --> 02:31:06,399 right up top over here as you can see 3457 02:31:06,399 --> 02:31:08,359 resolve index by user 3458 02:31:08,359 --> 02:31:10,520 ID for this 3459 02:31:10,520 --> 02:31:13,439 middleware um and I think we are good 3460 02:31:13,439 --> 02:31:18,000 for uh this end point right over here 3461 02:31:18,000 --> 02:31:20,479 and I did I did remove it so that's good 3462 02:31:20,479 --> 02:31:21,760 so now let me go ahead and just make 3463 02:31:21,760 --> 02:31:23,479 sure my other endpoints 3464 02:31:23,479 --> 02:31:28,040 work okay uh let's do three okay so our 3465 02:31:28,040 --> 02:31:30,160 end points are working so that's 3466 02:31:30,160 --> 02:31:33,399 good uh What else let's go ahead and 3467 02:31:33,399 --> 02:31:38,880 move these three end points into users. 3468 02:31:38,880 --> 02:31:40,279 MJS as well so I'm just going to paste 3469 02:31:40,279 --> 02:31:42,960 them all over here and just change app 3470 02:31:42,960 --> 02:31:44,840 to 3471 02:31:44,840 --> 02:31:48,359 router just like that 3472 02:31:49,399 --> 02:31:51,120 and we're using the same resolve index 3473 02:31:51,120 --> 02:31:53,120 by user ID which like I said we imported 3474 02:31:53,120 --> 02:31:55,000 up top over here from that new 3475 02:31:55,000 --> 02:31:57,680 middlewares MJS file that I just 3476 02:31:57,680 --> 02:32:01,399 created okay and uh there's nothing else 3477 02:32:01,399 --> 02:32:02,840 that we need to import because it's 3478 02:32:02,840 --> 02:32:04,680 using mock users which we already 3479 02:32:04,680 --> 02:32:05,880 imported 3480 02:32:05,880 --> 02:32:08,160 already um and the request and response 3481 02:32:08,160 --> 02:32:10,560 object comes from this callback function 3482 02:32:10,560 --> 02:32:12,439 so we're good to go with the rest of our 3483 02:32:12,439 --> 02:32:15,720 endpoints so look at our main file now 3484 02:32:15,720 --> 02:32:18,920 look at all the this now we 3485 02:32:18,920 --> 02:32:22,760 have cleaned up our index. MJS file and 3486 02:32:22,760 --> 02:32:25,399 anything relevant to users I can just go 3487 02:32:25,399 --> 02:32:28,800 into the routes users. MJS file and look 3488 02:32:28,800 --> 02:32:31,160 for the the corresponding endpoint that 3489 02:32:31,160 --> 02:32:32,319 I need to look 3490 02:32:32,319 --> 02:32:34,840 for okay let's just make sure let's just 3491 02:32:34,840 --> 02:32:38,960 make sure our other endpoints 3492 02:32:38,960 --> 02:32:41,520 work okay that's 3493 02:32:41,520 --> 02:32:43,680 good okay and if 3494 02:32:43,680 --> 02:32:46,359 I take a look at this I have 3495 02:32:46,359 --> 02:32:49,479 successfully updated the data and Patch 3496 02:32:49,479 --> 02:32:52,720 should work just fine as 3497 02:32:58,080 --> 02:33:00,800 well okay patch is working just fine I 3498 02:33:00,800 --> 02:33:03,240 was able to update my data using put and 3499 02:33:03,240 --> 02:33:04,800 patch and let's try 3500 02:33:04,800 --> 02:33:08,200 delete so I just deleted the user of ID3 3501 02:33:08,200 --> 02:33:09,840 so if I try to get that User it's going 3502 02:33:09,840 --> 02:33:12,600 to say four for not found and if I call 3503 02:33:12,600 --> 02:33:15,160 users it's not going to show that user 3504 02:33:15,160 --> 02:33:19,200 in the array okay so that's good so 3505 02:33:19,200 --> 02:33:22,600 hopefully this all makes sense so now I 3506 02:33:22,600 --> 02:33:25,319 can easily just clean up my index. MJS 3507 02:33:25,319 --> 02:33:27,359 code so let me remove all all of the 3508 02:33:27,359 --> 02:33:29,680 Imports that we're not using anymore so 3509 02:33:29,680 --> 02:33:31,840 all of this stuff we're not using 3510 02:33:31,840 --> 02:33:35,680 anymore all of this stuff over 3511 02:33:35,680 --> 02:33:37,840 here um I'm going to delete logging 3512 02:33:37,840 --> 02:33:39,439 middleware because we're not using that 3513 02:33:39,439 --> 02:33:42,000 anymore let me remove these coms down 3514 02:33:42,000 --> 02:33:44,880 here okay let's go ahead and create 3515 02:33:44,880 --> 02:33:47,760 another router for products so I'm going 3516 02:33:47,760 --> 02:33:49,640 to create a new file and I'll call this 3517 02:33:49,640 --> 02:33:51,160 products. 3518 02:33:51,160 --> 02:33:54,479 MJS and it's going to follow the same a 3519 02:33:54,479 --> 02:33:56,560 structure that we did for our users 3520 02:33:56,560 --> 02:33:58,520 router we're going to 3521 02:33:58,520 --> 02:34:01,479 import the router from 3522 02:34:01,479 --> 02:34:03,359 Express we're going to go ahead and 3523 02:34:03,359 --> 02:34:06,399 create the router instance by calling 3524 02:34:06,399 --> 02:34:08,479 the router function and assigning that 3525 02:34:08,479 --> 02:34:10,960 return value to the router variable and 3526 02:34:10,960 --> 02:34:13,439 then we want to also make sure we export 3527 02:34:13,439 --> 02:34:16,920 the router as a default export 3528 02:34:16,920 --> 02:34:19,720 and since products only had we only had 3529 02:34:19,720 --> 02:34:21,760 one endpoint for products I'm just going 3530 02:34:21,760 --> 02:34:24,000 to go ahead and copy all of this let me 3531 02:34:24,000 --> 02:34:25,520 remove 3532 02:34:25,520 --> 02:34:29,399 that and we'll call router. getet paste 3533 02:34:29,399 --> 02:34:33,120 this there and since we're not using any 3534 02:34:33,120 --> 02:34:35,080 other Imports or we're not referencing 3535 02:34:35,080 --> 02:34:36,960 anything else aside from just this 3536 02:34:36,960 --> 02:34:38,200 response argument in the function we 3537 02:34:38,200 --> 02:34:40,080 don't need to worry about importing 3538 02:34:40,080 --> 02:34:42,120 anything okay and the last thing that we 3539 02:34:42,120 --> 02:34:47,279 need to do is import the products router 3540 02:34:47,279 --> 02:34:49,520 like 3541 02:34:49,760 --> 02:34:53,520 this so import products router from uh 3542 02:34:53,520 --> 02:34:55,800 and then the path to that products MJS 3543 02:34:55,800 --> 02:34:58,520 file and then we need to call app.use 3544 02:34:58,520 --> 02:35:01,560 and pass in products router like this so 3545 02:35:01,560 --> 02:35:04,640 now if I try to make a request to 3546 02:35:04,640 --> 02:35:07,000 products I can get the list of products 3547 02:35:07,000 --> 02:35:09,760 and of course if I were to remove this 3548 02:35:09,760 --> 02:35:12,240 app.use uh with the products router 3549 02:35:12,240 --> 02:35:14,760 being passed as an argument we're not 3550 02:35:14,760 --> 02:35:16,600 registering that products end point 3551 02:35:16,600 --> 02:35:18,040 or that products router so we can't 3552 02:35:18,040 --> 02:35:20,760 access that products endpoint so 3553 02:35:20,760 --> 02:35:23,479 hopefully this makes sense now one more 3554 02:35:23,479 --> 02:35:25,640 thing that I will do and I'm only going 3555 02:35:25,640 --> 02:35:27,160 to do this because I want to structure 3556 02:35:27,160 --> 02:35:29,680 this entire project in a way for future 3557 02:35:29,680 --> 02:35:32,399 videos for future tutorial videos what 3558 02:35:32,399 --> 02:35:35,399 I'm going to do is I'm going to actually 3559 02:35:35,399 --> 02:35:37,359 create a new 3560 02:35:37,359 --> 02:35:39,960 file called index. 3561 02:35:39,960 --> 02:35:42,319 MJS and what I'm going to do is I'm 3562 02:35:42,319 --> 02:35:45,359 going to import the router as well and 3563 02:35:45,359 --> 02:35:48,680 I'm going to create an instance of 3564 02:35:48,680 --> 02:35:50,880 this and you don't have to do this if 3565 02:35:50,880 --> 02:35:53,240 you don't want to but personally for me 3566 02:35:53,240 --> 02:35:55,439 I prefer organizing everything like this 3567 02:35:55,439 --> 02:35:57,800 because it makes it so much easier I'm 3568 02:35:57,800 --> 02:36:00,920 going to also export this router as a 3569 02:36:00,920 --> 02:36:02,200 default 3570 02:36:02,200 --> 02:36:04,920 export and what I'll do is instead of 3571 02:36:04,920 --> 02:36:08,520 having all of my routers being imported 3572 02:36:08,520 --> 02:36:10,640 in the index. MJS file because you might 3573 02:36:10,640 --> 02:36:14,040 have a lot what I'll do is inside this 3574 02:36:14,040 --> 02:36:17,640 index. MJS file in the r folder I will 3575 02:36:17,640 --> 02:36:20,520 import all of my routers in here so you 3576 02:36:20,520 --> 02:36:22,479 can kind of think of this like a barrel 3577 02:36:22,479 --> 02:36:24,560 file if you've ever heard of that before 3578 02:36:24,560 --> 02:36:28,080 so let me import users router 3579 02:36:28,080 --> 02:36:31,520 from users. MJS and then we'll do the 3580 02:36:31,520 --> 02:36:34,840 same thing for products 3581 02:36:34,840 --> 02:36:38,960 router import that from products MJS and 3582 02:36:38,960 --> 02:36:42,160 then what you can do here is you can use 3583 02:36:42,160 --> 02:36:45,560 router and then call do use and in case 3584 02:36:45,560 --> 02:36:47,760 if I I mention earlier you can also 3585 02:36:47,760 --> 02:36:49,640 register middlewares the same way that 3586 02:36:49,640 --> 02:36:51,000 you would register middlewares on the 3587 02:36:51,000 --> 02:36:54,319 express app on the router itself and 3588 02:36:54,319 --> 02:36:56,000 that would of course register middleware 3589 02:36:56,000 --> 02:36:58,240 at the router layer so not for all of 3590 02:36:58,240 --> 02:37:00,960 your routes but for all of the routes 3591 02:37:00,960 --> 02:37:04,000 for your router specifically okay so you 3592 02:37:04,000 --> 02:37:06,160 can register middleware only for your 3593 02:37:06,160 --> 02:37:08,359 users routes and it won't have anything 3594 02:37:08,359 --> 02:37:11,560 to do with the products rout at all okay 3595 02:37:11,560 --> 02:37:13,080 but I'm going to go ahead and pass in 3596 02:37:13,080 --> 02:37:15,319 the users router like this and I'll do 3597 02:37:15,319 --> 02:37:17,399 the same thing but for products router 3598 02:37:17,399 --> 02:37:20,640 like that and then I export the router 3599 02:37:20,640 --> 02:37:23,279 as a default export from this index. MJS 3600 02:37:23,279 --> 02:37:26,640 file and then I can now go into my 3601 02:37:26,640 --> 02:37:29,160 index. MJS file in the root Source 3602 02:37:29,160 --> 02:37:32,080 folder right over here remove these two 3603 02:37:32,080 --> 02:37:34,399 Imports 3604 02:37:34,399 --> 02:37:35,920 import 3605 02:37:35,920 --> 02:37:38,319 um let's do this 3606 02:37:38,319 --> 02:37:41,600 import routes from and we're going to 3607 02:37:41,600 --> 02:37:44,359 import this from index. MJS so we're 3608 02:37:44,359 --> 02:37:47,399 importing this root router that we're 3609 02:37:47,399 --> 02:37:49,479 using to register all the other 3610 02:37:49,479 --> 02:37:52,800 routers okay and then I can go ahead and 3611 02:37:52,800 --> 02:37:56,359 pass routes into app.use so I only need 3612 02:37:56,359 --> 02:37:58,200 to do this one time because I already 3613 02:37:58,200 --> 02:38:01,479 have all of my routers registered with 3614 02:38:01,479 --> 02:38:03,600 this root router right over here and I'm 3615 02:38:03,600 --> 02:38:05,800 registering that root router with the 3616 02:38:05,800 --> 02:38:08,359 main Express app so it's still going to 3617 02:38:08,359 --> 02:38:10,279 work the same exact 3618 02:38:10,279 --> 02:38:12,640 way it doesn't matter whichever one you 3619 02:38:12,640 --> 02:38:14,160 prefer to do but I just prefer doing it 3620 02:38:14,160 --> 02:38:16,080 this way because I think it makes it a 3621 02:38:16,080 --> 02:38:18,600 lot more cleaner for the index file 3622 02:38:18,600 --> 02:38:21,800 itself okay and one one more thing that 3623 02:38:21,800 --> 02:38:25,040 I will do though is you can also set um 3624 02:38:25,040 --> 02:38:26,760 a prefix for all of your routes because 3625 02:38:26,760 --> 02:38:30,520 notice how right now I am I need to 3626 02:38:30,520 --> 02:38:34,760 prefix everything with/ API okay SL API 3627 02:38:34,760 --> 02:38:39,560 products SL API users Etc so hopefully 3628 02:38:39,560 --> 02:38:41,319 all of this makes sense and you can now 3629 02:38:41,319 --> 02:38:43,760 see how or how much more organized your 3630 02:38:43,760 --> 02:38:45,720 entire application looks so you can you 3631 02:38:45,720 --> 02:38:48,200 can go ahead and add add more endpoints 3632 02:38:48,200 --> 02:38:49,920 for products you can add more endpoints 3633 02:38:49,920 --> 02:38:51,399 for 3634 02:38:51,399 --> 02:38:54,319 users and you will know where every 3635 02:38:54,319 --> 02:38:56,040 single thing is without having to scroll 3636 02:38:56,040 --> 02:39:00,120 through one file try to look for 3637 02:39:01,800 --> 02:39:04,680 it all right everyone so the next topic 3638 02:39:04,680 --> 02:39:07,160 that we're going to talk about are HTTP 3639 02:39:07,160 --> 02:39:09,439 cookies so cookies are pretty much a 3640 02:39:09,439 --> 02:39:10,960 simple concept but a lot of people tend 3641 02:39:10,960 --> 02:39:12,279 to get confused about it they get 3642 02:39:12,279 --> 02:39:14,520 confused on what it is how to use it how 3643 02:39:14,520 --> 02:39:16,520 it's used in realc ations but most 3644 02:39:16,520 --> 02:39:18,040 importantly they kind of don't quite 3645 02:39:18,040 --> 02:39:19,200 understand what's the purpose of it so 3646 02:39:19,200 --> 02:39:20,920 I'll do my best to explain it to you all 3647 02:39:20,920 --> 02:39:23,800 in a simple way so cookies or HTTP 3648 02:39:23,800 --> 02:39:25,920 cookies they're literally just small 3649 02:39:25,920 --> 02:39:28,200 pieces of data that your web server 3650 02:39:28,200 --> 02:39:30,880 sends to the browser in our case since 3651 02:39:30,880 --> 02:39:32,560 we're using Thunder client you can 3652 02:39:32,560 --> 02:39:34,200 pretend that this is our web browser so 3653 02:39:34,200 --> 02:39:37,560 whenever I make a request to this URL it 3654 02:39:37,560 --> 02:39:39,680 will send me back a cookie but currently 3655 02:39:39,680 --> 02:39:41,920 our server is not sending any cookies 3656 02:39:41,920 --> 02:39:44,840 but you can see over here I can whenever 3657 02:39:44,840 --> 02:39:46,760 I uh make make a request I can go ahead 3658 02:39:46,760 --> 02:39:48,960 and click on this response section over 3659 02:39:48,960 --> 02:39:50,840 here and then select cookies and see if 3660 02:39:50,840 --> 02:39:52,359 there were any cookies sent back from 3661 02:39:52,359 --> 02:39:54,920 the server so the server sends a cookie 3662 02:39:54,920 --> 02:39:57,359 to the user's web browser or the client 3663 02:39:57,359 --> 02:39:59,720 or whatever and then the web browser or 3664 02:39:59,720 --> 02:40:01,359 in this case our Thunder client can 3665 02:40:01,359 --> 02:40:02,840 store the cookie if you actually go into 3666 02:40:02,840 --> 02:40:04,960 your browser right now and if you were 3667 02:40:04,960 --> 02:40:06,720 to open up the dev tools so let me 3668 02:40:06,720 --> 02:40:08,279 actually do this let me go to Local Host 3669 02:40:08,279 --> 02:40:09,960 Port 3000 and let me Zoom a little bit 3670 02:40:09,960 --> 02:40:12,880 and let me open up my Dev tool so right 3671 02:40:12,880 --> 02:40:15,880 over here and if you actually were to go 3672 02:40:15,880 --> 02:40:19,080 to uh this Chevron this this double 3673 02:40:19,080 --> 02:40:21,479 Chevron arrow and click on application 3674 02:40:21,479 --> 02:40:23,040 you can see this section where it says 3675 02:40:23,040 --> 02:40:24,479 cookies now currently I don't have any 3676 02:40:24,479 --> 02:40:27,359 cookie stored on this domain but we can 3677 02:40:27,359 --> 02:40:29,359 change that we can make our web server 3678 02:40:29,359 --> 02:40:30,880 send the cookie back to the web browser 3679 02:40:30,880 --> 02:40:32,640 the web browser can store that cookie 3680 02:40:32,640 --> 02:40:35,359 and then what happens next is the web 3681 02:40:35,359 --> 02:40:37,840 browser can actually send that cookie 3682 02:40:37,840 --> 02:40:40,520 back to the server on any request you 3683 02:40:40,520 --> 02:40:41,640 need to make now you're probably 3684 02:40:41,640 --> 02:40:43,240 wondering well why is this important 3685 02:40:43,240 --> 02:40:45,520 well it's important because by default h 3686 02:40:45,520 --> 02:40:48,880 HTTP is stateless and what that means is 3687 02:40:48,880 --> 02:40:50,479 that whenever you make a request the 3688 02:40:50,479 --> 02:40:52,600 server doesn't know who that request is 3689 02:40:52,600 --> 02:40:54,160 coming from it doesn't know who the user 3690 02:40:54,160 --> 02:40:56,000 is and knows nothing so if you wanted to 3691 02:40:56,000 --> 02:40:58,120 build let's say an e-commerce website 3692 02:40:58,120 --> 02:40:59,920 and you wanted to implement some kind of 3693 02:40:59,920 --> 02:41:02,439 cart system where you can add items to a 3694 02:41:02,439 --> 02:41:03,840 cart and delete them from a cart and you 3695 02:41:03,840 --> 02:41:05,720 want it to make it functional where when 3696 02:41:05,720 --> 02:41:07,640 the user adds items to the cart and they 3697 02:41:07,640 --> 02:41:09,040 close a website and go back on the 3698 02:41:09,040 --> 02:41:10,720 website that those items are still in 3699 02:41:10,720 --> 02:41:12,720 the cart after they leave and come back 3700 02:41:12,720 --> 02:41:14,479 you need to use something like cookies 3701 02:41:14,479 --> 02:41:16,240 because the server doesn't know who the 3702 02:41:16,240 --> 02:41:17,840 user is it doesn't know what items they 3703 02:41:17,840 --> 02:41:20,000 added but when you use cookies you can 3704 02:41:20,000 --> 02:41:21,960 send the cookie back to the server and 3705 02:41:21,960 --> 02:41:23,720 the server will then know who that user 3706 02:41:23,720 --> 02:41:26,720 is and so the next time the user comes 3707 02:41:26,720 --> 02:41:28,800 back the user can have all of their 3708 02:41:28,800 --> 02:41:30,840 items on the cart displayed so they 3709 02:41:30,840 --> 02:41:32,399 don't have to re everything all over 3710 02:41:32,399 --> 02:41:36,040 again now most of the time in realistic 3711 02:41:36,040 --> 02:41:38,240 large applications where you have 3712 02:41:38,240 --> 02:41:41,240 authentication you use cookies alongside 3713 02:41:41,240 --> 02:41:42,720 with sessions but we're not going to get 3714 02:41:42,720 --> 02:41:45,080 into sessions just yet we're primarily 3715 02:41:45,080 --> 02:41:47,960 only going to focus on cookies right now 3716 02:41:47,960 --> 02:41:50,120 okay but hopefully that explanation 3717 02:41:50,120 --> 02:41:53,840 makes sense just remember that HTTP is 3718 02:41:53,840 --> 02:41:57,319 stateless and using cookies enables the 3719 02:41:57,319 --> 02:41:59,279 server to send a cookie to the web 3720 02:41:59,279 --> 02:42:01,720 browser and that cookie typically is 3721 02:42:01,720 --> 02:42:03,960 going to be some unique value so that 3722 02:42:03,960 --> 02:42:05,680 way the server when they receive it they 3723 02:42:05,680 --> 02:42:07,359 can distinguish whose cookie this 3724 02:42:07,359 --> 02:42:09,200 belongs to and then they can send 3725 02:42:09,200 --> 02:42:12,399 Dynamic data based on the cookie value 3726 02:42:12,399 --> 02:42:14,880 so I'll show you an example so let's go 3727 02:42:14,880 --> 02:42:19,439 into our code and what I'll do is inside 3728 02:42:19,439 --> 02:42:22,520 the base URL I'm going to go ahead and 3729 02:42:22,520 --> 02:42:24,800 set the cookie whenever the user visits 3730 02:42:24,800 --> 02:42:25,640 this 3731 02:42:25,640 --> 02:42:28,800 endpoint to do that we have to modify 3732 02:42:28,800 --> 02:42:31,120 the response object by 3733 02:42:31,120 --> 02:42:33,640 calling this cookie method on the 3734 02:42:33,640 --> 02:42:37,920 response object and this takes in uh a 3735 02:42:37,920 --> 02:42:40,439 total of three arguments options is 3736 02:42:40,439 --> 02:42:42,720 optional but you can pass in a name for 3737 02:42:42,720 --> 02:42:44,760 the cookie so I'm going to call this 3738 02:42:44,760 --> 02:42:47,120 hello and and then I'm going to set the 3739 02:42:47,120 --> 02:42:49,920 value of this cookie to be world and 3740 02:42:49,920 --> 02:42:52,080 then you can pass in options so let's 3741 02:42:52,080 --> 02:42:53,479 say if you want to have the cookie 3742 02:42:53,479 --> 02:42:57,399 expire after a minute then you have to 3743 02:42:57,399 --> 02:43:00,439 specify that in milliseconds 3744 02:43:00,439 --> 02:43:05,240 so 60,000 M milliseconds is 1 minute and 3745 02:43:05,240 --> 02:43:07,319 let's go ahead and test this out so I'm 3746 02:43:07,319 --> 02:43:08,560 going to go into the browser and if I 3747 02:43:08,560 --> 02:43:10,880 refresh you can see that I now have this 3748 02:43:10,880 --> 02:43:12,840 cookie and I can see that every time I 3749 02:43:12,840 --> 02:43:14,640 refresh you can see the number just 3750 02:43:14,640 --> 02:43:19,520 jumps up to 49 50 well now 51 53 54 so 3751 02:43:19,520 --> 02:43:22,680 it's going to expire after 1 minute okay 3752 02:43:22,680 --> 02:43:25,520 I can have it expire after an hour so 3753 02:43:25,520 --> 02:43:29,720 let's do 1 minute time 60 so that's 1 3754 02:43:29,720 --> 02:43:31,040 hour right there so notice have I 3755 02:43:31,040 --> 02:43:33,560 refresh you're going to see that let me 3756 02:43:33,560 --> 02:43:35,520 kind of zoom in a little bit right over 3757 02:43:35,520 --> 02:43:38,640 here so you'll notice if I refresh it 3758 02:43:38,640 --> 02:43:41,279 goes from uh I guess the 14th hour to 3759 02:43:41,279 --> 02:43:44,319 the 15th hour this is in uh 24hour time 3760 02:43:44,319 --> 02:43:46,000 clock 3761 02:43:46,000 --> 02:43:47,520 and uh I think it's in UTC time 3762 02:43:47,520 --> 02:43:48,840 currently it's 3763 02:43:48,840 --> 02:43:53,240 7:24 so I think plus 7 hours gets 3764 02:43:53,240 --> 02:43:58,640 you um I think 15 is what 3:00 I think 3765 02:43:58,640 --> 02:44:00,640 so plus 7 hours yeah yep that's right 3766 02:44:00,640 --> 02:44:03,920 plus 7 hours yep okay and if I were to 3767 02:44:03,920 --> 02:44:06,680 multiply this by two so expires in 2 3768 02:44:06,680 --> 02:44:09,600 hours if I refresh you'll see now it 3769 02:44:09,600 --> 02:44:13,399 expires at the 16th hour which is 4:00 3770 02:44:13,399 --> 02:44:17,160 p.m. 400 p.m. so hopefully this makes 3771 02:44:17,160 --> 02:44:19,960 sense okay and let me just also go into 3772 02:44:19,960 --> 02:44:23,520 my thunder client and if I click 3773 02:44:23,520 --> 02:44:27,000 Send uh let's 3774 02:44:27,760 --> 02:44:30,240 see uh oh I'm visiting I'm visiting the 3775 02:44:30,240 --> 02:44:32,439 wrong endpoint let me visit the slash 3776 02:44:32,439 --> 02:44:35,680 endpoint and see how now I can see that 3777 02:44:35,680 --> 02:44:38,520 I have a cookie right over 3778 02:44:38,520 --> 02:44:41,120 here and you can see that it shows you 3779 02:44:41,120 --> 02:44:44,240 the domain the cookie name the value all 3780 02:44:44,240 --> 02:44:45,680 the stuff 3781 02:44:45,680 --> 02:44:49,560 okay so hopefully this shows you how the 3782 02:44:49,560 --> 02:44:51,680 browser and how your clients can store 3783 02:44:51,680 --> 02:44:54,760 cookies but now let's actually see how 3784 02:44:54,760 --> 02:44:56,520 it's being used like how do we actually 3785 02:44:56,520 --> 02:45:00,800 make sure that we are sending the cookie 3786 02:45:00,800 --> 02:45:04,200 back to the server because right now 3787 02:45:04,200 --> 02:45:06,760 we're only receiving the cookie that is 3788 02:45:06,760 --> 02:45:08,680 stored in the browser but we're not 3789 02:45:08,680 --> 02:45:11,840 really doing anything to actually reuse 3790 02:45:11,840 --> 02:45:14,600 that cookie so the next thing that we're 3791 02:45:14,600 --> 02:45:16,160 going to do 3792 02:45:16,160 --> 02:45:20,920 is this what I'm going to do is I will 3793 02:45:20,920 --> 02:45:22,920 go 3794 02:45:22,920 --> 02:45:26,160 into um any endpoint so let's just use 3795 02:45:26,160 --> 02:45:27,319 the products endpoint because we haven't 3796 02:45:27,319 --> 02:45:31,399 really done much with this and what I'll 3797 02:45:31,399 --> 02:45:34,760 do is I want to go ahead and grab the 3798 02:45:34,760 --> 02:45:38,600 cookie from the request 3799 02:45:39,160 --> 02:45:41,880 object okay so I'll console log this 3800 02:45:41,880 --> 02:45:44,080 right now and I'm going to visit SL API 3801 02:45:44,080 --> 02:45:48,760 products so let's go to the 3802 02:45:48,760 --> 02:45:53,040 browser and since our cookie uh expires 3803 02:45:53,040 --> 02:45:55,160 in 2 hours we're going to be fine so 3804 02:45:55,160 --> 02:45:57,240 we'll still have the cookie if it 3805 02:45:57,240 --> 02:45:58,479 expires though and your browser doesn't 3806 02:45:58,479 --> 02:45:59,880 have the cookie then of course that 3807 02:45:59,880 --> 02:46:01,920 cookie is not going to be sent but let 3808 02:46:01,920 --> 02:46:05,479 me go ahead and close this and let's go 3809 02:46:05,479 --> 02:46:08,680 to whoops didn't mean to open that let's 3810 02:46:08,680 --> 02:46:11,399 go to SL API 3811 02:46:11,399 --> 02:46:14,000 products and let's look at the 3812 02:46:14,000 --> 02:46:17,200 console and you can see right now uh 3813 02:46:17,200 --> 02:46:20,160 whenever I visit that 3814 02:46:20,160 --> 02:46:23,279 endpoint it says undefined now you're 3815 02:46:23,279 --> 02:46:24,640 probably wondering well why is that the 3816 02:46:24,640 --> 02:46:29,040 case are there really no cookies well 3817 02:46:29,040 --> 02:46:30,880 let me show you 3818 02:46:30,880 --> 02:46:35,479 this okay if I were to log it from the 3819 02:46:35,479 --> 02:46:38,720 headers and if I refresh that page you 3820 02:46:38,720 --> 02:46:41,120 can see now we actually have the cookies 3821 02:46:41,120 --> 02:46:43,279 and this is access through the headers 3822 02:46:43,279 --> 02:46:44,600 but the problem here is that it's not 3823 02:46:44,600 --> 02:46:46,080 pars 3824 02:46:46,080 --> 02:46:49,240 okay we have it in this string format so 3825 02:46:49,240 --> 02:46:50,560 we have two options we would have to 3826 02:46:50,560 --> 02:46:52,080 parse it ourselves 3827 02:46:52,080 --> 02:46:54,680 manually or we can actually use a 3828 02:46:54,680 --> 02:46:57,359 third-party package called cookie parser 3829 02:46:57,359 --> 02:46:59,520 to parse the cookies for us so just to 3830 02:46:59,520 --> 02:47:01,840 confirm with you all the cookies are in 3831 02:47:01,840 --> 02:47:04,560 fact being sent to the server it's just 3832 02:47:04,560 --> 02:47:06,279 that they are not being parsed the way 3833 02:47:06,279 --> 02:47:09,520 that we expected it to so let's go into 3834 02:47:09,520 --> 02:47:10,960 let's see our terminal we're going to 3835 02:47:10,960 --> 02:47:13,960 install cookie parser so cookie hyphen 3836 02:47:13,960 --> 02:47:16,479 parser and you can see that it has 3837 02:47:16,479 --> 02:47:17,880 literally like almost 3 million 3838 02:47:17,880 --> 02:47:20,040 downloads so it is a very popular 3839 02:47:20,040 --> 02:47:23,319 package in the node.js 3840 02:47:23,600 --> 02:47:25,840 ecosystem and let's 3841 02:47:25,840 --> 02:47:29,240 install cookie hyphen 3842 02:47:29,240 --> 02:47:32,359 parser and then what we'll do is we'll 3843 02:47:32,359 --> 02:47:35,240 run our server 3844 02:47:35,920 --> 02:47:38,479 again and now we need to actually enable 3845 02:47:38,479 --> 02:47:41,840 cookie parer remember that a lot of 3846 02:47:41,840 --> 02:47:42,880 things that you're going to be 3847 02:47:42,880 --> 02:47:44,560 installing as third party packages in 3848 02:47:44,560 --> 02:47:48,279 Express is going to be middleware okay 3849 02:47:48,279 --> 02:47:50,120 so in this case cookie parer is also 3850 02:47:50,120 --> 02:47:52,920 middleware as well so we'll import that 3851 02:47:52,920 --> 02:47:56,520 middleware so import cookie parser from 3852 02:47:56,520 --> 02:47:57,479 cookie 3853 02:47:57,479 --> 02:48:00,840 parser and then this is a default export 3854 02:48:00,840 --> 02:48:03,359 so what we'll do next is we'll simply 3855 02:48:03,359 --> 02:48:04,760 just enable it but we want to make sure 3856 02:48:04,760 --> 02:48:06,600 we enable it before all of our routes 3857 02:48:06,600 --> 02:48:08,720 are being registered so you see how over 3858 02:48:08,720 --> 02:48:11,600 here where I have app.use and then I 3859 02:48:11,600 --> 02:48:15,200 pass in uh that router that router from 3860 02:48:15,200 --> 02:48:16,760 this file over here and this is what 3861 02:48:16,760 --> 02:48:18,920 registers all of our routes we want to 3862 02:48:18,920 --> 02:48:21,560 make sure that we register it before 3863 02:48:21,560 --> 02:48:25,000 otherwise it won't parse the cookies for 3864 02:48:25,000 --> 02:48:26,279 those routes and then we won't be able 3865 02:48:26,279 --> 02:48:28,439 to use it so right 3866 02:48:28,439 --> 02:48:32,760 before um we'll do app.use and then 3867 02:48:32,760 --> 02:48:35,319 cookie parser it's a function and you 3868 02:48:35,319 --> 02:48:37,479 can also pass in additional arguments 3869 02:48:37,479 --> 02:48:39,439 into the cookie parser function call if 3870 02:48:39,439 --> 02:48:41,960 you need them so for example let's say 3871 02:48:41,960 --> 02:48:45,120 if you needed to uh parse a signed 3872 02:48:45,120 --> 02:48:47,040 cookie which is really just a cookie 3873 02:48:47,040 --> 02:48:48,760 that has a signature then you can pass 3874 02:48:48,760 --> 02:48:51,240 in a secret and this secret is used to 3875 02:48:51,240 --> 02:48:53,439 actually parse the signed cookie so that 3876 02:48:53,439 --> 02:48:54,720 would just be like a regular string like 3877 02:48:54,720 --> 02:48:56,240 this or whatever the secret is but we'll 3878 02:48:56,240 --> 02:48:58,160 leave it alone for now cuz we don't have 3879 02:48:58,160 --> 02:48:59,720 any signed cookies okay so now that 3880 02:48:59,720 --> 02:49:01,240 we've enabled the middleware let's go 3881 02:49:01,240 --> 02:49:05,240 back to products. MJS inside our routes 3882 02:49:05,240 --> 02:49:07,600 folder and I'll go ahead and console log 3883 02:49:07,600 --> 02:49:10,240 both request. headers do cookies and 3884 02:49:10,240 --> 02:49:13,439 request. cookies like this and you can 3885 02:49:13,439 --> 02:49:15,800 see there's also a sign cookies property 3886 02:49:15,800 --> 02:49:18,399 as well for sign cookies so let's go 3887 02:49:18,399 --> 02:49:21,279 into Postman or not Postman what I'm 3888 02:49:21,279 --> 02:49:22,920 talking about thunder client and let's 3889 02:49:22,920 --> 02:49:25,399 go to API products so we still have that 3890 02:49:25,399 --> 02:49:28,680 cookie because it has not 3891 02:49:28,680 --> 02:49:30,520 expired I wonder if there's a way to 3892 02:49:30,520 --> 02:49:32,000 actually see the expiration date of 3893 02:49:32,000 --> 02:49:34,399 these cookies on the Thunder client 3894 02:49:34,399 --> 02:49:37,600 perhaps not I don't 3895 02:49:37,640 --> 02:49:39,960 know but it's okay we know that it is 3896 02:49:39,960 --> 02:49:41,680 not expired though so I'm going to go 3897 02:49:41,680 --> 02:49:46,439 ahead and do this I'm going to send the 3898 02:49:46,439 --> 02:49:49,080 request okay and let's look at the 3899 02:49:49,080 --> 02:49:51,279 server and or let's look at the 3900 02:49:51,279 --> 02:49:54,080 console and you can see that we log both 3901 02:49:54,080 --> 02:49:56,160 the header cookies and the parsed 3902 02:49:56,160 --> 02:49:57,640 cookies right over here and you can see 3903 02:49:57,640 --> 02:49:59,520 that it was correctly parsed hello is 3904 02:49:59,520 --> 02:50:01,600 the field name and then world is the 3905 02:50:01,600 --> 02:50:03,840 value so that is how we can use cookie 3906 02:50:03,840 --> 02:50:05,319 parsers to parse the cookie let's say 3907 02:50:05,319 --> 02:50:07,960 for example I wanted to check the value 3908 02:50:07,960 --> 02:50:09,120 I wanted to check the field and the 3909 02:50:09,120 --> 02:50:11,760 value of a cookie and based on that 3910 02:50:11,760 --> 02:50:15,359 field and the value of that field I can 3911 02:50:15,359 --> 02:50:17,680 determine the response that I want to 3912 02:50:17,680 --> 02:50:20,200 give back to the user so let's just very 3913 02:50:20,200 --> 02:50:23,040 quickly pretend like this hello world 3914 02:50:23,040 --> 02:50:25,239 Cookie is required in order to actually 3915 02:50:25,239 --> 02:50:27,279 retrieve all the products so what you 3916 02:50:27,279 --> 02:50:29,920 would do is something like this you 3917 02:50:29,920 --> 02:50:32,160 would check to see if request. cookies 3918 02:50:32,160 --> 02:50:33,760 Dot and then the field name which would 3919 02:50:33,760 --> 02:50:34,760 be 3920 02:50:34,760 --> 02:50:36,760 hello and then you so you want to make 3921 02:50:36,760 --> 02:50:38,479 sure this is defined and then you would 3922 02:50:38,479 --> 02:50:40,000 want to make sure 3923 02:50:40,000 --> 02:50:43,040 that hello is equal to some certain 3924 02:50:43,040 --> 02:50:45,880 value in in our case since we set the 3925 02:50:45,880 --> 02:50:48,120 cookie on the server side and the value 3926 02:50:48,120 --> 02:50:52,279 that we set was world we can have it 3927 02:50:52,279 --> 02:50:54,479 check to see if it's equal to world and 3928 02:50:54,479 --> 02:50:56,200 if it is then we'll send back the 3929 02:50:56,200 --> 02:50:58,680 response of all the cook of all the 3930 02:50:58,680 --> 02:51:00,479 products and if it's not then what we'll 3931 02:51:00,479 --> 02:51:03,040 do let me actually use the return 3932 02:51:03,040 --> 02:51:06,439 keyword and if it's not I'll just return 3933 02:51:06,439 --> 02:51:11,439 a response that says message sorry you 3934 02:51:11,439 --> 02:51:14,880 need the correct cookie and this is just 3935 02:51:14,880 --> 02:51:17,279 to show you how you can use 3936 02:51:17,279 --> 02:51:20,359 cookies okay or how you can use cookies 3937 02:51:20,359 --> 02:51:22,840 to determine the output of your server 3938 02:51:22,840 --> 02:51:25,279 response so if I click Send right now 3939 02:51:25,279 --> 02:51:26,800 okay nothing 3940 02:51:26,800 --> 02:51:29,200 special but now watch this I'm going to 3941 02:51:29,200 --> 02:51:31,760 go ahead and uh let's see I'm going to 3942 02:51:31,760 --> 02:51:35,399 remove this cookie right over here and 3943 02:51:35,399 --> 02:51:36,880 I'm going to go ahead and click Send and 3944 02:51:36,880 --> 02:51:39,439 watch what happens you see how now since 3945 02:51:39,439 --> 02:51:40,760 I don't have that cookie it says sorry 3946 02:51:40,760 --> 02:51:42,479 you need the correct cookie now if I go 3947 02:51:42,479 --> 02:51:44,640 back to the browser the browser is a 3948 02:51:44,640 --> 02:51:46,239 different client okay so right now we're 3949 02:51:46,239 --> 02:51:47,319 using two different clients we're using 3950 02:51:47,319 --> 02:51:49,040 the web browser and we're using our 3951 02:51:49,040 --> 02:51:50,960 Thunder client so since the browser 3952 02:51:50,960 --> 02:51:53,680 still has a valid cookie I can still get 3953 02:51:53,680 --> 02:51:55,239 this data but let me go back into my 3954 02:51:55,239 --> 02:51:57,880 browsers Dev tools and let me clear the 3955 02:51:57,880 --> 02:52:00,479 Cookie by simply just deleting 3956 02:52:00,479 --> 02:52:03,800 it and watch this if I refresh the page 3957 02:52:03,800 --> 02:52:07,080 you see how now it says sorry you need 3958 02:52:07,080 --> 02:52:09,319 the correct cookie so this is how you 3959 02:52:09,319 --> 02:52:11,000 can start to understand how 3960 02:52:11,000 --> 02:52:13,000 authentication begins to work the server 3961 02:52:13,000 --> 02:52:15,720 sends a cookie back to the client so 3962 02:52:15,720 --> 02:52:17,399 that's basically saying hey when I send 3963 02:52:17,399 --> 02:52:19,000 this cookie back to you you need to send 3964 02:52:19,000 --> 02:52:21,760 this back to me in order for you to make 3965 02:52:21,760 --> 02:52:25,600 future requests to access any data or 3966 02:52:25,600 --> 02:52:27,880 any endpoint at all the browser stores 3967 02:52:27,880 --> 02:52:30,760 that cookie and then any future request 3968 02:52:30,760 --> 02:52:32,160 that you want to make from the browser 3969 02:52:32,160 --> 02:52:34,359 or the client from Thunder client you 3970 02:52:34,359 --> 02:52:37,000 need to pass the cookies from the 3971 02:52:37,000 --> 02:52:38,680 browser or the client or wherever when 3972 02:52:38,680 --> 02:52:40,120 you send the cookies the server will 3973 02:52:40,120 --> 02:52:41,880 check for the cookies if you have the 3974 02:52:41,880 --> 02:52:44,439 cookies and it's a valid value then you 3975 02:52:44,439 --> 02:52:47,120 will send the data back to the user if 3976 02:52:47,120 --> 02:52:50,200 the request does not have the cookie or 3977 02:52:50,200 --> 02:52:52,200 if they do but it's not a valid value 3978 02:52:52,200 --> 02:52:53,840 then you're going to send them a 3979 02:52:53,840 --> 02:52:56,279 different response so hopefully you can 3980 02:52:56,279 --> 02:52:59,319 start to see how cookies can change the 3981 02:52:59,319 --> 02:53:00,920 output of your application and how you 3982 02:53:00,920 --> 02:53:02,640 can use it for you know authentication 3983 02:53:02,640 --> 02:53:04,479 and authorization so one more thing that 3984 02:53:04,479 --> 02:53:07,399 I will show you is this I'm going to go 3985 02:53:07,399 --> 02:53:09,279 ahead and set the max age of the cookie 3986 02:53:09,279 --> 02:53:13,040 to 10 seconds and so what I'll do is 3987 02:53:13,040 --> 02:53:16,120 this so since this route is the route 3988 02:53:16,120 --> 02:53:17,720 that actually sets the cookie itself you 3989 02:53:17,720 --> 02:53:19,600 can pretend like this is the route that 3990 02:53:19,600 --> 02:53:23,880 you must visit first in order for you to 3991 02:53:23,880 --> 02:53:25,960 kind of like authenticate have the 3992 02:53:25,960 --> 02:53:28,600 cookies set on the server side and sent 3993 02:53:28,600 --> 02:53:31,000 back to the browser and then be able to 3994 02:53:31,000 --> 02:53:33,920 access the quote unquote protected route 3995 02:53:33,920 --> 02:53:36,560 hopefully that makes sense okay so right 3996 02:53:36,560 --> 02:53:39,880 now if I try to go to API products I 3997 02:53:39,880 --> 02:53:43,520 cannot access uh the resource over here 3998 02:53:43,520 --> 02:53:44,680 and you know what let me do one one more 3999 02:53:44,680 --> 02:53:47,520 thing I'll also send a 4000 02:53:47,520 --> 02:53:50,439 403 which means unauthorized or you can 4001 02:53:50,439 --> 02:53:51,880 send a 401 4002 02:53:51,880 --> 02:53:54,239 back so you see 403 4003 02:53:54,239 --> 02:53:55,800 Forbidden 4004 02:53:55,800 --> 02:53:58,960 okay so now let's do this let's go back 4005 02:53:58,960 --> 02:54:02,200 to the base route and kind of like quote 4006 02:54:02,200 --> 02:54:04,160 unquote authenticate because this is 4007 02:54:04,160 --> 02:54:05,600 what gives us the 4008 02:54:05,600 --> 02:54:07,920 cookie we can we we can see the cookie 4009 02:54:07,920 --> 02:54:10,439 sent back and now watch 4010 02:54:10,439 --> 02:54:14,359 this if I try to go to slash API 4011 02:54:14,359 --> 02:54:16,439 products I can get this I can get this 4012 02:54:16,439 --> 02:54:20,239 response back but notice how after that 4013 02:54:20,239 --> 02:54:23,200 10 seconds pass it says sorry you need 4014 02:54:23,200 --> 02:54:26,840 the correct cookie so the cookie 4015 02:54:26,840 --> 02:54:30,200 expires after 10 seconds and you can see 4016 02:54:30,200 --> 02:54:31,399 that we don't even have any cookies 4017 02:54:31,399 --> 02:54:33,160 anymore CU it automatically clears it 4018 02:54:33,160 --> 02:54:35,720 for us so I can go back to the base 4019 02:54:35,720 --> 02:54:37,399 route 4020 02:54:37,399 --> 02:54:40,120 reauthenticate call the API products 4021 02:54:40,120 --> 02:54:42,560 route and I can make as many requests as 4022 02:54:42,560 --> 02:54:45,319 I want to this endpoint 4023 02:54:45,319 --> 02:54:47,279 until once the cookie has expired and 4024 02:54:47,279 --> 02:54:49,439 once it expires it gives me the 4025 02:54:49,439 --> 02:54:52,439 alternate response okay so hopefully 4026 02:54:52,439 --> 02:54:56,399 this makes sense now since we are 4027 02:54:56,399 --> 02:54:58,120 already talking about cookie parser and 4028 02:54:58,120 --> 02:54:59,920 using cookies I might as well just talk 4029 02:54:59,920 --> 02:55:02,359 about this as well if you ever need to 4030 02:55:02,359 --> 02:55:04,920 set sign cookies which like I said 4031 02:55:04,920 --> 02:55:07,880 earlier it has a signature uh you can go 4032 02:55:07,880 --> 02:55:10,000 ahead right over here and set signed to 4033 02:55:10,000 --> 02:55:12,840 True Whenever you set the cookie but in 4034 02:55:12,840 --> 02:55:15,399 order for you to actually uh parse 4035 02:55:15,399 --> 02:55:18,479 cookies that are signed you must you 4036 02:55:18,479 --> 02:55:20,239 must you must provide a secret like 4037 02:55:20,239 --> 02:55:21,520 right now if I try to 4038 02:55:21,520 --> 02:55:24,600 refresh or if I try to go to the base 4039 02:55:24,600 --> 02:55:27,040 URL you'll see that it says cookie 4040 02:55:27,040 --> 02:55:30,200 parser secret required for signed 4041 02:55:30,200 --> 02:55:32,359 cookies so let's go ahead and pass a 4042 02:55:32,359 --> 02:55:35,520 secret right over here let's just do 4043 02:55:35,520 --> 02:55:38,239 Hello World literally it can just be any 4044 02:55:38,239 --> 02:55:40,279 string value you want that is just going 4045 02:55:40,279 --> 02:55:44,880 to be used for signing cookies okay so 4046 02:55:44,880 --> 02:55:50,160 now watch this okay before we added this 4047 02:55:50,160 --> 02:55:52,600 signed property in the options when we 4048 02:55:52,600 --> 02:55:55,840 set the cookie we were just able to see 4049 02:55:55,840 --> 02:55:57,399 the Value World so let me just show you 4050 02:55:57,399 --> 02:55:58,560 this real quick let me just kind of 4051 02:55:58,560 --> 02:56:00,840 delete this and delete that and just 4052 02:56:00,840 --> 02:56:02,920 show you very quickly see how the value 4053 02:56:02,920 --> 02:56:06,800 is just World okay now let me go back 4054 02:56:06,800 --> 02:56:11,319 and set sign to true and put in that 4055 02:56:11,319 --> 02:56:13,239 secret so now watch this let me just 4056 02:56:13,239 --> 02:56:16,040 delete this and if I refresh the page 4057 02:56:16,040 --> 02:56:17,840 you see how now the value isn't just 4058 02:56:17,840 --> 02:56:19,800 world you can actually see the value is 4059 02:56:19,800 --> 02:56:22,680 here but because now it's signed it has 4060 02:56:22,680 --> 02:56:25,040 the signature and in order for you to 4061 02:56:25,040 --> 02:56:27,359 actually use the sign cookie well 4062 02:56:27,359 --> 02:56:29,120 because we already are enabling the 4063 02:56:29,120 --> 02:56:30,760 middle already it will just parse the 4064 02:56:30,760 --> 02:56:33,479 cookies for us but remember how earlier 4065 02:56:33,479 --> 02:56:35,840 we took a look at the request object and 4066 02:56:35,840 --> 02:56:37,760 there were two properties related to 4067 02:56:37,760 --> 02:56:39,920 cookies there was cookies and then sign 4068 02:56:39,920 --> 02:56:42,239 cookies right over here so we want to 4069 02:56:42,239 --> 02:56:44,840 reference sign cookies so watch this let 4070 02:56:44,840 --> 02:56:47,200 me just Refresh on this page let me 4071 02:56:47,200 --> 02:56:49,720 change the max age of the cookie to 30 4072 02:56:49,720 --> 02:56:52,520 seconds so three so 30,000 milliseconds 4073 02:56:52,520 --> 02:56:55,720 and let me go back here so now let me go 4074 02:56:55,720 --> 02:56:59,439 to/ API products you see how now it says 4075 02:56:59,439 --> 02:57:01,040 sorry you need the correct cookie this 4076 02:57:01,040 --> 02:57:03,359 is because we are checking just the 4077 02:57:03,359 --> 02:57:05,800 cookies and not sign cookies if I look 4078 02:57:05,800 --> 02:57:09,040 at the console right now you'll see 4079 02:57:09,040 --> 02:57:12,080 that this is the raw cookie over here 4080 02:57:12,080 --> 02:57:14,080 that comes from the headers that is not 4081 02:57:14,080 --> 02:57:16,399 parsed into an object so you see you 4082 02:57:16,399 --> 02:57:17,920 have the key and then the value right 4083 02:57:17,920 --> 02:57:19,279 over here exactly what you saw in the 4084 02:57:19,279 --> 02:57:21,600 browser then you have this object an 4085 02:57:21,600 --> 02:57:24,960 empty object which is the request. 4086 02:57:24,960 --> 02:57:27,640 cookies property so an unsigned cookie 4087 02:57:27,640 --> 02:57:30,080 but because now we have signed cookies 4088 02:57:30,080 --> 02:57:32,880 all of those parsed sign cookies will go 4089 02:57:32,880 --> 02:57:36,160 into um that sign cookies object in the 4090 02:57:36,160 --> 02:57:38,520 request object so you have this object 4091 02:57:38,520 --> 02:57:39,720 right over 4092 02:57:39,720 --> 02:57:42,359 here okay so you can actually just 4093 02:57:42,359 --> 02:57:45,439 reference hello on the sign cookie like 4094 02:57:45,439 --> 02:57:48,920 that so if I refresh this page uh well 4095 02:57:48,920 --> 02:57:51,960 we don't have a cookie so we need to go 4096 02:57:51,960 --> 02:57:53,760 back to the base URL quote unquote 4097 02:57:53,760 --> 02:57:56,319 reauthenticate get the cookie set and 4098 02:57:56,319 --> 02:57:57,960 then now let's go back to the API 4099 02:57:57,960 --> 02:58:00,680 products route and if you look at the 4100 02:58:00,680 --> 02:58:02,560 logs you can see that whenever I 4101 02:58:02,560 --> 02:58:04,359 reference the sign 4102 02:58:04,359 --> 02:58:07,359 cookies object and reference hello it 4103 02:58:07,359 --> 02:58:10,680 gives me this value of world so we can 4104 02:58:10,680 --> 02:58:12,439 just very easily 4105 02:58:12,439 --> 02:58:16,160 modify um this part over here if 4106 02:58:16,160 --> 02:58:18,319 request. cookies instead of that we can 4107 02:58:18,319 --> 02:58:21,239 just do if request. sign cookies. hello 4108 02:58:21,239 --> 02:58:22,880 and of course you would need to keep 4109 02:58:22,880 --> 02:58:24,239 track of what cookies are signed and 4110 02:58:24,239 --> 02:58:26,920 what cookies are not signed so that will 4111 02:58:26,920 --> 02:58:29,040 indicate which property whether cookies 4112 02:58:29,040 --> 02:58:30,359 or signed cookies that you would that 4113 02:58:30,359 --> 02:58:33,120 you would reference so if request. sign 4114 02:58:33,120 --> 02:58:36,279 cookies. hello and request. sign 4115 02:58:36,279 --> 02:58:38,800 cookies. hello is equal to world then 4116 02:58:38,800 --> 02:58:41,279 we'll send back this response so let's 4117 02:58:41,279 --> 02:58:45,160 save and now our cookie likely likely 4118 02:58:45,160 --> 02:58:48,279 expired by now so let's go back to the 4119 02:58:48,279 --> 02:58:51,239 base URL get that cookie again go back 4120 02:58:51,239 --> 02:58:53,960 to API products and now we have access 4121 02:58:53,960 --> 02:58:57,960 to the resource so hopefully this all 4122 02:58:57,960 --> 02:58:59,680 makes 4123 02:58:59,680 --> 02:59:02,760 sense in this part of our tutorial we 4124 02:59:02,760 --> 02:59:04,479 are going to talk about sessions and 4125 02:59:04,479 --> 02:59:06,720 Implement them in our Express server 4126 02:59:06,720 --> 02:59:08,479 sessions represent the duration of a 4127 02:59:08,479 --> 02:59:11,760 user on a website by default HTTP is 4128 02:59:11,760 --> 02:59:14,040 stateless we don't know who is making 4129 02:59:14,040 --> 02:59:16,600 requests to our server so we need to be 4130 02:59:16,600 --> 02:59:18,439 able to track requests and know where 4131 02:59:18,439 --> 02:59:21,080 they are coming from one common usage of 4132 02:59:21,080 --> 02:59:23,239 sessions is to manage user 4133 02:59:23,239 --> 02:59:25,359 authentication sessions are created on 4134 02:59:25,359 --> 02:59:27,200 the server by generating an object with 4135 02:59:27,200 --> 02:59:30,640 a session ID when an HTTP request is 4136 02:59:30,640 --> 02:59:33,000 sent to the server from the web browser 4137 02:59:33,000 --> 02:59:34,520 the response can return with 4138 02:59:34,520 --> 02:59:36,239 instructions to set a cookie with the 4139 02:59:36,239 --> 02:59:38,200 session ID so that it can be saved in 4140 02:59:38,200 --> 02:59:40,479 the browser this allows the browser to 4141 02:59:40,479 --> 02:59:42,720 send the cookie on subsequent requests 4142 02:59:42,720 --> 02:59:44,399 to the server the the server can then 4143 02:59:44,399 --> 02:59:46,960 parse the cookies from text to Json then 4144 02:59:46,960 --> 02:59:48,880 verify the session ID was sent from the 4145 02:59:48,880 --> 02:59:51,120 client and determine who the request was 4146 02:59:51,120 --> 02:59:54,080 sent from whenever the browser sends the 4147 02:59:54,080 --> 02:59:56,319 cookies on each request the server can 4148 02:59:56,319 --> 02:59:58,120 look up which user pertains to the 4149 02:59:58,120 --> 02:59:59,880 session as the server maintains a 4150 02:59:59,880 --> 03:00:02,359 mapping of each session ID to the user 4151 03:00:02,359 --> 03:00:03,760 we'll get started in implementing 4152 03:00:03,760 --> 03:00:05,560 sessions using the express session 4153 03:00:05,560 --> 03:00:07,560 Library so the first thing that we're 4154 03:00:07,560 --> 03:00:09,880 going to do is install the express 4155 03:00:09,880 --> 03:00:12,760 session package so in my project 4156 03:00:12,760 --> 03:00:16,640 terminal I'm going to type npmi Express 4157 03:00:16,640 --> 03:00:18,800 hyphen 4158 03:00:18,800 --> 03:00:22,000 session and then we need to go into our 4159 03:00:22,000 --> 03:00:25,880 index. MJS file or whatever root file 4160 03:00:25,880 --> 03:00:27,479 you have where you create the app 4161 03:00:27,479 --> 03:00:29,720 instance or the express 4162 03:00:29,720 --> 03:00:32,000 instance and then you're going to 4163 03:00:32,000 --> 03:00:33,520 import 4164 03:00:33,520 --> 03:00:36,319 session and then from 4165 03:00:36,319 --> 03:00:39,319 Express pyph session like 4166 03:00:39,319 --> 03:00:42,120 that so this session import is a 4167 03:00:42,120 --> 03:00:44,960 middleware function and we can register 4168 03:00:44,960 --> 03:00:49,000 it by simply just using app.use so 4169 03:00:49,000 --> 03:00:52,880 app.use and then session just like this 4170 03:00:52,880 --> 03:00:54,239 and you want to make sure that you are 4171 03:00:54,239 --> 03:00:57,520 calling app.use right before you are 4172 03:00:57,520 --> 03:01:00,040 registering any endpoints in your 4173 03:01:00,040 --> 03:01:02,439 application so remember that I using 4174 03:01:02,439 --> 03:01:06,080 this router that is imported from uh 4175 03:01:06,080 --> 03:01:09,479 routes right over here and this file 4176 03:01:09,479 --> 03:01:12,040 over here has all of my individual 4177 03:01:12,040 --> 03:01:14,680 routers for products and users 4178 03:01:14,680 --> 03:01:17,040 okay so what I'm making sure is I'm 4179 03:01:17,040 --> 03:01:18,239 making sure I'm calling the session 4180 03:01:18,239 --> 03:01:20,200 middle work function before my routes 4181 03:01:20,200 --> 03:01:21,120 are being 4182 03:01:21,120 --> 03:01:23,080 registered so now the next thing that we 4183 03:01:23,080 --> 03:01:25,200 want to do is configure some options for 4184 03:01:25,200 --> 03:01:28,920 Express session so the first thing that 4185 03:01:28,920 --> 03:01:32,040 we will do is set a 4186 03:01:32,040 --> 03:01:34,600 secret so I'm going to pass in an object 4187 03:01:34,600 --> 03:01:37,040 and so for the secret this is going to 4188 03:01:37,040 --> 03:01:39,319 be a string now ideally you would want 4189 03:01:39,319 --> 03:01:41,000 this to be something that is 4190 03:01:41,000 --> 03:01:42,880 sophisticated because it is used to 4191 03:01:42,880 --> 03:01:44,960 actually sign the cookie and if you have 4192 03:01:44,960 --> 03:01:46,680 it something guessable someone can 4193 03:01:46,680 --> 03:01:49,760 easily use that value to decode signed 4194 03:01:49,760 --> 03:01:51,600 cookies for now just for development 4195 03:01:51,600 --> 03:01:54,120 purposes I'm just going to use anev but 4196 03:01:54,120 --> 03:01:55,439 just keep in mind you would want this to 4197 03:01:55,439 --> 03:01:57,319 be something more complicated and not 4198 03:01:57,319 --> 03:01:59,040 guessable kind of like a password I'm 4199 03:01:59,040 --> 03:02:01,760 going to set this property called save 4200 03:02:01,760 --> 03:02:03,720 uninitialized now this property 4201 03:02:03,720 --> 03:02:05,000 alongside with the next property I'm 4202 03:02:05,000 --> 03:02:07,479 going to set called resave both of these 4203 03:02:07,479 --> 03:02:09,880 have to do with session stores we 4204 03:02:09,880 --> 03:02:12,439 currently don't have any session stores 4205 03:02:12,439 --> 03:02:14,600 configured by default Express session 4206 03:02:14,600 --> 03:02:17,080 does use an inmemory store but you want 4207 03:02:17,080 --> 03:02:19,080 save uninitialized sets of false when 4208 03:02:19,080 --> 03:02:21,200 you don't want to save unmodified 4209 03:02:21,200 --> 03:02:23,319 session data to the session store so 4210 03:02:23,319 --> 03:02:25,200 what that means is by default if you 4211 03:02:25,200 --> 03:02:26,920 have every single user visiting your 4212 03:02:26,920 --> 03:02:28,399 website and they're not doing anything 4213 03:02:28,399 --> 03:02:30,200 they're just visiting it it will if you 4214 03:02:30,200 --> 03:02:32,319 have save initialized set to true it 4215 03:02:32,319 --> 03:02:35,040 will actually save a session object to 4216 03:02:35,040 --> 03:02:36,640 the store even if the session object has 4217 03:02:36,640 --> 03:02:38,479 nothing at all and that can actually 4218 03:02:38,479 --> 03:02:41,160 take up memory and you probably won't 4219 03:02:41,160 --> 03:02:43,319 want to have a bunch of random session 4220 03:02:43,319 --> 03:02:45,439 object objects living in your session 4221 03:02:45,439 --> 03:02:48,600 store so ideally you would want this set 4222 03:02:48,600 --> 03:02:50,920 to be set to false and it's recommended 4223 03:02:50,920 --> 03:02:52,800 if you're building something like uh 4224 03:02:52,800 --> 03:02:54,640 user authentication managing user 4225 03:02:54,640 --> 03:02:57,040 sessions things like that the other 4226 03:02:57,040 --> 03:02:59,520 property that we want to set is resave 4227 03:02:59,520 --> 03:03:02,680 okay and this really has to do with um 4228 03:03:02,680 --> 03:03:05,120 forcing a session to be saved back to 4229 03:03:05,120 --> 03:03:07,560 the session store even if the session 4230 03:03:07,560 --> 03:03:10,520 itself was never modified at all you'll 4231 03:03:10,520 --> 03:03:12,120 better understand what I mean by 4232 03:03:12,120 --> 03:03:14,399 modifying the session once I show you 4233 03:03:14,399 --> 03:03:16,560 how to actually do it and what it 4234 03:03:16,560 --> 03:03:18,680 affects how it how it affects the actual 4235 03:03:18,680 --> 03:03:21,000 session itself but for now don't worry 4236 03:03:21,000 --> 03:03:23,000 about these two properties just set to 4237 03:03:23,000 --> 03:03:25,200 false the other property that I want to 4238 03:03:25,200 --> 03:03:27,600 set is this cookie property and this is 4239 03:03:27,600 --> 03:03:29,800 where we can actually configure how long 4240 03:03:29,800 --> 03:03:31,640 we want the cookie to live because in 4241 03:03:31,640 --> 03:03:33,120 case if you didn't know cookies can 4242 03:03:33,120 --> 03:03:35,960 actually expire so this is good if you 4243 03:03:35,960 --> 03:03:38,760 have a user login system where let's say 4244 03:03:38,760 --> 03:03:40,479 you want the user to be logged in to 4245 03:03:40,479 --> 03:03:43,120 your website a Max of 24 hours so you 4246 03:03:43,120 --> 03:03:46,359 can set the this Max H property and uh 4247 03:03:46,359 --> 03:03:49,000 the value is a number and it's measured 4248 03:03:49,000 --> 03:03:49,880 in 4249 03:03:49,880 --> 03:03:54,319 milliseconds um so for example 60,000 4250 03:03:54,319 --> 03:03:58,800 milliseconds that is 60 seconds uh times 4251 03:03:58,800 --> 03:04:01,960 let's see 60 so that's 1 hour so let's 4252 03:04:01,960 --> 03:04:03,880 say if I only wanted the user to be 4253 03:04:03,880 --> 03:04:07,479 logged in for exactly 1 hour then the 4254 03:04:07,479 --> 03:04:10,120 cookie will expire after 1 hour and that 4255 03:04:10,120 --> 03:04:12,120 cookie will no longer be valid and so 4256 03:04:12,120 --> 03:04:13,800 whenever they send it back to the server 4257 03:04:13,800 --> 03:04:15,720 the server will see that the cookie is 4258 03:04:15,720 --> 03:04:17,680 not a valid one at all and remember that 4259 03:04:17,680 --> 03:04:19,720 the cookie that is sent from the browser 4260 03:04:19,720 --> 03:04:22,439 to the server is what is being used by 4261 03:04:22,439 --> 03:04:25,640 the server to identify who the user is 4262 03:04:25,640 --> 03:04:27,880 so I'll keep everything simple and use 4263 03:04:27,880 --> 03:04:30,239 my base URL endpoint and what I want to 4264 03:04:30,239 --> 03:04:33,160 do is inside the request Handler and you 4265 03:04:33,160 --> 03:04:34,960 can use any endpoint you want I'm just 4266 03:04:34,960 --> 03:04:36,359 going to go ahead and console log 4267 03:04:36,359 --> 03:04:37,960 request. 4268 03:04:37,960 --> 03:04:41,239 session and I want to conso log the 4269 03:04:41,239 --> 03:04:43,960 session ID as well so you can either 4270 03:04:43,960 --> 03:04:45,640 reference session ID or you can 4271 03:04:45,640 --> 03:04:47,479 reference session. 4272 03:04:47,479 --> 03:04:50,840 ID and I'm going to go and make one API 4273 03:04:50,840 --> 03:04:53,439 request to this endpoint and you'll see 4274 03:04:53,439 --> 03:04:56,120 that when I make a request I can see the 4275 03:04:56,120 --> 03:04:57,960 session object as well as the session ID 4276 03:04:57,960 --> 03:05:00,479 being logged and you can see the session 4277 03:05:00,479 --> 03:05:02,439 object has this cookie property which is 4278 03:05:02,439 --> 03:05:05,040 an object and you can see that the 4279 03:05:05,040 --> 03:05:08,520 expiration date is over here so it's in 4280 03:05:08,520 --> 03:05:13,040 UTC time currently it's 2:44 a.m. so uh 4281 03:05:13,040 --> 03:05:15,479 I think I'm 7 hours behind that time 4282 03:05:15,479 --> 03:05:18,359 zone so that would be 9:44 and then 4283 03:05:18,359 --> 03:05:20,319 expires in 1 hour so the date the 4284 03:05:20,319 --> 03:05:23,640 expiration date time is correct but 4285 03:05:23,640 --> 03:05:25,520 let's focus on this session ID right 4286 03:05:25,520 --> 03:05:27,800 over here okay so I'm going to make 4287 03:05:27,800 --> 03:05:30,160 another request and notice how when I 4288 03:05:30,160 --> 03:05:32,640 make another request you can actually 4289 03:05:32,640 --> 03:05:35,120 see that not only does the expiration 4290 03:05:35,120 --> 03:05:37,359 date update but 4291 03:05:37,359 --> 03:05:41,319 also this session ID is regenerated I 4292 03:05:41,319 --> 03:05:44,439 can create another one one and another 4293 03:05:44,439 --> 03:05:46,399 one so every single time I click Send 4294 03:05:46,399 --> 03:05:47,399 it's going 4295 03:05:47,399 --> 03:05:50,479 to pretty much like create a new cookie 4296 03:05:50,479 --> 03:05:51,960 and also create a new 4297 03:05:51,960 --> 03:05:55,560 session and this is actually not 4298 03:05:55,560 --> 03:05:57,880 necessarily good because we never can 4299 03:05:57,880 --> 03:06:00,680 actually track who the user is that's 4300 03:06:00,680 --> 03:06:01,760 using our 4301 03:06:01,760 --> 03:06:04,000 application so this is where you need to 4302 03:06:04,000 --> 03:06:08,279 modify the session data object when you 4303 03:06:08,279 --> 03:06:10,960 modify the session data object Express 4304 03:06:10,960 --> 03:06:14,160 session will create a cook 4305 03:06:14,160 --> 03:06:16,160 or it will set the cookie and then that 4306 03:06:16,160 --> 03:06:18,479 cookie will be sent to the browser or 4307 03:06:18,479 --> 03:06:20,760 the client side the client side will 4308 03:06:20,760 --> 03:06:23,640 store that cookie and then on subsequent 4309 03:06:23,640 --> 03:06:26,479 requests or future requests that cookie 4310 03:06:26,479 --> 03:06:28,680 will be sent to the server assuming it 4311 03:06:28,680 --> 03:06:32,239 has not been expired okay the server of 4312 03:06:32,239 --> 03:06:34,479 course will go through the express 4313 03:06:34,479 --> 03:06:35,960 session middleware because remember 4314 03:06:35,960 --> 03:06:38,239 Express session is a middleware so it'll 4315 03:06:38,239 --> 03:06:40,880 go through this whole session middleware 4316 03:06:40,880 --> 03:06:44,479 and then it'll validate that cookie make 4317 03:06:44,479 --> 03:06:46,439 sure it's not an invalid cookie so if 4318 03:06:46,439 --> 03:06:49,279 the cookie is not expired or if it's not 4319 03:06:49,279 --> 03:06:51,520 invalid then Express session actually 4320 03:06:51,520 --> 03:06:54,880 won't generate a new session or session 4321 03:06:54,880 --> 03:06:57,880 ID at all okay so hopefully that part 4322 03:06:57,880 --> 03:06:59,600 makes sense because by modifying the 4323 03:06:59,600 --> 03:07:02,279 session data object we can begin 4324 03:07:02,279 --> 03:07:04,760 tracking users that are using our 4325 03:07:04,760 --> 03:07:07,560 website our API if we don't then we'll 4326 03:07:07,560 --> 03:07:11,200 just always have generated sessions all 4327 03:07:11,200 --> 03:07:14,080 the time brand new sessions all the time 4328 03:07:14,080 --> 03:07:16,800 okay and remember if you had save 4329 03:07:16,800 --> 03:07:19,319 uninitialized set to true it would save 4330 03:07:19,319 --> 03:07:21,200 all of these sessions even if it's not 4331 03:07:21,200 --> 03:07:23,279 modified to the session store so that 4332 03:07:23,279 --> 03:07:26,040 would use up a lot of memory for no no 4333 03:07:26,040 --> 03:07:28,800 good reason whatsoever so what I'll do 4334 03:07:28,800 --> 03:07:30,600 right now is I'm going to modify the 4335 03:07:30,600 --> 03:07:31,680 session 4336 03:07:31,680 --> 03:07:34,200 object by just simply referencing 4337 03:07:34,200 --> 03:07:36,720 request. session and I can attach 4338 03:07:36,720 --> 03:07:39,239 Dynamic properties onto this session 4339 03:07:39,239 --> 03:07:41,680 object so let's do a simple one I'm 4340 03:07:41,680 --> 03:07:44,560 going to go ahead and reference visited 4341 03:07:44,560 --> 03:07:47,040 and I'll set this to true and I'm just 4342 03:07:47,040 --> 03:07:49,359 going to remove uh I'm just gonna remove 4343 03:07:49,359 --> 03:07:50,880 the session oh I'll leave I'll leave 4344 03:07:50,880 --> 03:07:53,160 everything alone for now and now watch 4345 03:07:53,160 --> 03:07:55,600 this okay let me go into my thunder 4346 03:07:55,600 --> 03:07:58,040 client and I want you to take a look at 4347 03:07:58,040 --> 03:08:00,319 what happens now when I make my first 4348 03:08:00,319 --> 03:08:02,600 request so the server just restarted 4349 03:08:02,600 --> 03:08:03,800 because we're using node modon so 4350 03:08:03,800 --> 03:08:05,680 whenever I make changes it'll restart 4351 03:08:05,680 --> 03:08:07,840 the server but I just made one request 4352 03:08:07,840 --> 03:08:10,000 and you can see this is my cookie and 4353 03:08:10,000 --> 03:08:12,399 this is my session ID notice how now 4354 03:08:12,399 --> 03:08:14,800 when I make a new request every single 4355 03:08:14,800 --> 03:08:18,120 time you see how now the session ID is 4356 03:08:18,120 --> 03:08:21,279 the same every single 4357 03:08:21,279 --> 03:08:24,720 time okay every single time I make a 4358 03:08:24,720 --> 03:08:28,200 request session ID is the 4359 03:08:28,200 --> 03:08:30,760 same and the reason why this is good is 4360 03:08:30,760 --> 03:08:33,000 because now I can actually track the 4361 03:08:33,000 --> 03:08:35,200 user you can see that we have this 4362 03:08:35,200 --> 03:08:38,880 visited property on the session object 4363 03:08:38,880 --> 03:08:42,279 and since the ID is not going to be 4364 03:08:42,279 --> 03:08:44,479 generated every every single time the 4365 03:08:44,479 --> 03:08:46,680 server itself 4366 03:08:46,680 --> 03:08:49,120 knows what the session ID is they can 4367 03:08:49,120 --> 03:08:51,720 look up the session ID and attach the 4368 03:08:51,720 --> 03:08:54,600 correct session data to the incoming 4369 03:08:54,600 --> 03:08:58,120 request object so we'll know who which 4370 03:08:58,120 --> 03:09:00,359 which user is so let me show you an 4371 03:09:00,359 --> 03:09:04,040 example okay so what I'll do is I'm 4372 03:09:04,040 --> 03:09:05,640 going to go ahead 4373 03:09:05,640 --> 03:09:08,160 into any endpoint right now I can go 4374 03:09:08,160 --> 03:09:10,319 into any endpoint and what I want to 4375 03:09:10,319 --> 03:09:13,960 show you is if I go into let's it's 4376 03:09:13,960 --> 03:09:17,120 let's do SL API users so this endpoint 4377 03:09:17,120 --> 03:09:19,200 gets a list of all the users and I'll 4378 03:09:19,200 --> 03:09:23,760 just console log request. session and 4379 03:09:23,760 --> 03:09:26,640 request. session 4380 03:09:28,600 --> 03:09:31,800 ID and I'm going to go back into my 4381 03:09:31,800 --> 03:09:33,720 thunder client so remember the server 4382 03:09:33,720 --> 03:09:35,720 just restarted so because we are not 4383 03:09:35,720 --> 03:09:40,880 using a persistent data store for our 4384 03:09:40,880 --> 03:09:43,319 sessions everything gets wiped out 4385 03:09:43,319 --> 03:09:44,319 because everything is currently in 4386 03:09:44,319 --> 03:09:45,880 memory so I'm going to go ahead and 4387 03:09:45,880 --> 03:09:48,880 click Send for our base URL I'm going to 4388 03:09:48,880 --> 03:09:51,160 make a request to the base URL first 4389 03:09:51,160 --> 03:09:53,520 because that is the endpoint that will 4390 03:09:53,520 --> 03:09:56,640 actually set the cookie for us okay 4391 03:09:56,640 --> 03:09:58,319 it'll it'll modify the session data 4392 03:09:58,319 --> 03:10:00,760 which will set the cookie and then send 4393 03:10:00,760 --> 03:10:03,520 it back to the client so if I click 4394 03:10:03,520 --> 03:10:06,920 Send okay you can see that I have my 4395 03:10:06,920 --> 03:10:09,359 cookie generator right over here and 4396 03:10:09,359 --> 03:10:12,319 also if I click on cookies on Thunder 4397 03:10:12,319 --> 03:10:13,800 client and if if you're using the 4398 03:10:13,800 --> 03:10:15,840 browser or if you're using Postman you 4399 03:10:15,840 --> 03:10:18,080 can also look at the cookies yourself 4400 03:10:18,080 --> 03:10:19,920 but you see how I have this cookie right 4401 03:10:19,920 --> 03:10:23,040 over here and you'll notice how uh 4402 03:10:23,040 --> 03:10:24,720 everything after these let me see if I 4403 03:10:24,720 --> 03:10:26,560 can zoom in a little bit you'll see that 4404 03:10:26,560 --> 03:10:27,720 everything 4405 03:10:27,720 --> 03:10:31,040 after these first four characters all 4406 03:10:31,040 --> 03:10:32,880 the way up until this 4407 03:10:32,880 --> 03:10:35,160 dot so this whole thing right over here 4408 03:10:35,160 --> 03:10:37,359 is actually our session ID you can see 4409 03:10:37,359 --> 03:10:39,640 that they match if I put them side by 4410 03:10:39,640 --> 03:10:41,720 side you can see that they match right 4411 03:10:41,720 --> 03:10:43,359 over here and then and of course 4412 03:10:43,359 --> 03:10:45,000 everything over here is the signature of 4413 03:10:45,000 --> 03:10:47,080 the cookie because it's signed All right 4414 03:10:47,080 --> 03:10:48,560 so now that we have our cookie from the 4415 03:10:48,560 --> 03:10:49,920 server now I want to show you what 4416 03:10:49,920 --> 03:10:51,640 happens when I visit a completely 4417 03:10:51,640 --> 03:10:53,840 different endpoint so let's go to API 4418 03:10:53,840 --> 03:10:55,080 users because that's where we're also 4419 03:10:55,080 --> 03:10:57,680 logging the session data as well okay so 4420 03:10:57,680 --> 03:10:59,319 right now we have a cookie we're sending 4421 03:10:59,319 --> 03:11:01,239 that cookie to the server the server is 4422 03:11:01,239 --> 03:11:03,160 going to make sure that cookie is a 4423 03:11:03,160 --> 03:11:07,720 valid cookie and if there's any data for 4424 03:11:07,720 --> 03:11:10,640 that session ID that was retrieved from 4425 03:11:10,640 --> 03:11:12,279 the cookie that was sent to the server 4426 03:11:12,279 --> 03:11:13,880 if there's any any data in the session 4427 03:11:13,880 --> 03:11:16,600 store it's going to map it to that 4428 03:11:16,600 --> 03:11:19,200 incoming request object okay so 4429 03:11:19,200 --> 03:11:21,800 basically this session object comes from 4430 03:11:21,800 --> 03:11:23,680 the session store essentially it get it 4431 03:11:23,680 --> 03:11:25,760 kind of gets restored but let me make a 4432 03:11:25,760 --> 03:11:27,359 request so I just made a request just 4433 03:11:27,359 --> 03:11:30,640 now and notice how when I made that 4434 03:11:30,640 --> 03:11:35,160 request okay I get the same exact cookie 4435 03:11:35,160 --> 03:11:38,680 right over here okay you see how I have 4436 03:11:38,680 --> 03:11:40,239 the same session 4437 03:11:40,239 --> 03:11:43,279 ID same session ID in a completely 4438 03:11:43,279 --> 03:11:44,080 different 4439 03:11:44,080 --> 03:11:47,520 endpoint you see how this object now 4440 03:11:47,520 --> 03:11:50,359 logs visited true whereas before the 4441 03:11:50,359 --> 03:11:53,720 previous one didn't obviously because we 4442 03:11:53,720 --> 03:11:56,399 didn't have a cookie set properly when 4443 03:11:56,399 --> 03:11:59,200 we visited this base 4444 03:11:59,200 --> 03:12:02,520 URL okay but when I visit this base URL 4445 03:12:02,520 --> 03:12:05,239 we modify the session 4446 03:12:05,239 --> 03:12:07,560 object so then that will set the cookie 4447 03:12:07,560 --> 03:12:09,880 and send it to the browser we make 4448 03:12:09,880 --> 03:12:13,040 another request to the user's API so so 4449 03:12:13,040 --> 03:12:15,800 when we do make that request request. 4450 03:12:15,800 --> 03:12:19,000 session now has that visited true 4451 03:12:19,000 --> 03:12:22,080 property set and it has the same exact 4452 03:12:22,080 --> 03:12:24,319 session ID so it's important to 4453 03:12:24,319 --> 03:12:26,720 understand this because now we can 4454 03:12:26,720 --> 03:12:28,279 actually build out some kind of 4455 03:12:28,279 --> 03:12:31,200 authentication system and know who each 4456 03:12:31,200 --> 03:12:33,479 user is one more thing that I do want to 4457 03:12:33,479 --> 03:12:36,439 show you is I want to show you the 4458 03:12:36,439 --> 03:12:39,520 session store currently so remember that 4459 03:12:39,520 --> 03:12:41,760 by default the session store is in 4460 03:12:41,760 --> 03:12:44,359 memory so it saves everything in some 4461 03:12:44,359 --> 03:12:46,479 data structure and it lives on the 4462 03:12:46,479 --> 03:12:48,600 server and of course this is not good 4463 03:12:48,600 --> 03:12:49,920 because when it's volatile so if the 4464 03:12:49,920 --> 03:12:52,359 server goes down and you may have seen 4465 03:12:52,359 --> 03:12:53,920 this already we have 4466 03:12:53,920 --> 03:12:56,439 to visit the website again and have a 4467 03:12:56,439 --> 03:12:59,439 new session created for us so I just 4468 03:12:59,439 --> 03:13:01,160 want to show you this real quick so let 4469 03:13:01,160 --> 03:13:02,080 me 4470 03:13:02,080 --> 03:13:04,560 reference 4471 03:13:04,560 --> 03:13:07,640 console.log request. session 4472 03:13:07,640 --> 03:13:09,760 store and I'm going to call this get 4473 03:13:09,760 --> 03:13:13,520 method and I can pass in uh any session 4474 03:13:13,520 --> 03:13:16,520 ID okay and then uh let's see we have to 4475 03:13:16,520 --> 03:13:18,600 pass in a callback function so that's 4476 03:13:18,600 --> 03:13:22,000 okay let me actually do this instead so 4477 03:13:22,000 --> 03:13:25,080 I'm going to pass in request. session. 4478 03:13:25,080 --> 03:13:30,279 ID and a callback function so let's see 4479 03:13:30,279 --> 03:13:33,600 error and session 4480 03:13:34,200 --> 03:13:39,040 data and I'll just do if 4481 03:13:40,239 --> 03:13:43,239 error throw error and and then I want to 4482 03:13:43,239 --> 03:13:45,600 console log the session 4483 03:13:45,600 --> 03:13:48,600 data just so you all can see what this 4484 03:13:48,600 --> 03:13:50,399 looks 4485 03:13:50,399 --> 03:13:53,160 like all right so 4486 03:13:53,160 --> 03:13:56,479 now let me do this in my 4487 03:13:56,479 --> 03:14:00,399 browser okay so I have these cookies set 4488 03:14:00,399 --> 03:14:02,600 which are not going to be valid anymore 4489 03:14:02,600 --> 03:14:06,279 because we just cleared we just uh 4490 03:14:06,279 --> 03:14:07,560 restarted 4491 03:14:07,560 --> 03:14:11,239 database so now if I go to the base URL 4492 03:14:11,239 --> 03:14:13,680 first we have have the cookie 4493 03:14:13,680 --> 03:14:15,840 set and let me just kind of go to my 4494 03:14:15,840 --> 03:14:17,560 console and scroll down okay so this is 4495 03:14:17,560 --> 03:14:21,840 our session ID now if I'll go to the 4496 03:14:21,840 --> 03:14:25,319 user Point okay same 4497 03:14:25,319 --> 03:14:30,239 cookies but notice how this is coming 4498 03:14:30,239 --> 03:14:33,920 from this part right over here line 29 4499 03:14:33,920 --> 03:14:36,640 so notice how when I called request. 4500 03:14:36,640 --> 03:14:38,680 session store.get I can pass in any 4501 03:14:38,680 --> 03:14:41,359 session ID and of course if it exists 4502 03:14:41,359 --> 03:14:42,840 then it'll give it to us which I'm 4503 03:14:42,840 --> 03:14:45,760 logging it right over here on line 29 so 4504 03:14:45,760 --> 03:14:47,680 that's where it's coming from okay so 4505 03:14:47,680 --> 03:14:51,479 that is how the session store stores 4506 03:14:51,479 --> 03:14:55,040 these session object data so I hope that 4507 03:14:55,040 --> 03:14:56,120 makes sense because I really want you 4508 03:14:56,120 --> 03:14:58,160 all to see this visually because just by 4509 03:14:58,160 --> 03:14:59,880 saying it it kind of doesn't really help 4510 03:14:59,880 --> 03:15:01,520 you understand how things are being 4511 03:15:01,520 --> 03:15:03,960 saved underneath the hood but at least 4512 03:15:03,960 --> 03:15:06,080 it helps you better understand that now 4513 03:15:06,080 --> 03:15:08,760 we can see that this session data is 4514 03:15:08,760 --> 03:15:11,080 stored in some data structure on the 4515 03:15:11,080 --> 03:15:13,359 server in memory 4516 03:15:13,359 --> 03:15:15,080 but in later parts of this tutorial I'm 4517 03:15:15,080 --> 03:15:16,720 going to show you how to use an actual 4518 03:15:16,720 --> 03:15:19,800 database to store the session data so 4519 03:15:19,800 --> 03:15:22,279 that way when your server goes down the 4520 03:15:22,279 --> 03:15:25,199 session data gets 4521 03:15:26,760 --> 03:15:29,640 restored so I did want to provide 4522 03:15:29,640 --> 03:15:31,680 another example with how to use sessions 4523 03:15:31,680 --> 03:15:34,239 and express so what I'm going to do is 4524 03:15:34,239 --> 03:15:36,640 show you all how to set up this fake 4525 03:15:36,640 --> 03:15:38,199 authentication system it's not like a 4526 03:15:38,199 --> 03:15:41,439 real authentication mechanism but the 4527 03:15:41,439 --> 03:15:43,640 goal of this is to help you understand 4528 03:15:43,640 --> 03:15:48,359 how we can map a single session ID to a 4529 03:15:48,359 --> 03:15:51,319 realistic user in our application so I'm 4530 03:15:51,319 --> 03:15:54,239 going to go into my main index file and 4531 03:15:54,239 --> 03:15:56,840 I'll just set up the route inside here 4532 03:15:56,840 --> 03:15:58,560 instead of having to create like a new 4533 03:15:58,560 --> 03:15:59,920 router and such because I want to keep 4534 03:15:59,920 --> 03:16:03,199 this simple so I'm going to reference 4535 03:16:03,199 --> 03:16:05,800 app and whoops sorry about that I'm 4536 03:16:05,800 --> 03:16:07,840 going to reference app and I'm going to 4537 03:16:07,840 --> 03:16:09,640 call the Post method to set a post 4538 03:16:09,640 --> 03:16:11,439 request and the endpoint for this will 4539 03:16:11,439 --> 03:16:14,319 be/ AP 4540 03:16:15,000 --> 03:16:17,840 SLO and then we will 4541 03:16:17,840 --> 03:16:21,600 need a request Handler I'm not going 4542 03:16:21,600 --> 03:16:25,720 to do any validation on the request body 4543 03:16:25,720 --> 03:16:26,760 um just because like I said I want to 4544 03:16:26,760 --> 03:16:28,439 keep it simple we'll just assume that 4545 03:16:28,439 --> 03:16:30,120 the payload that we're sending the 4546 03:16:30,120 --> 03:16:32,359 request body that we're sending to this 4547 03:16:32,359 --> 03:16:34,279 endpoint is 4548 03:16:34,279 --> 03:16:37,040 correct all right so let's go 4549 03:16:37,040 --> 03:16:40,319 ahead and grab the request 4550 03:16:40,319 --> 03:16:44,120 body and then what we we want to do next 4551 03:16:44,120 --> 03:16:47,600 is uh let's go into our constants MJS 4552 03:16:47,600 --> 03:16:50,080 file if you don't have uh a list of mock 4553 03:16:50,080 --> 03:16:51,479 users then you could just simply create 4554 03:16:51,479 --> 03:16:54,120 one or like I said you can just pull the 4555 03:16:54,120 --> 03:16:57,279 code from GitHub but I did actually do 4556 03:16:57,279 --> 03:16:59,640 this earlier I provided a bunch of 4557 03:16:59,640 --> 03:17:02,920 passwords for each user object so that 4558 03:17:02,920 --> 03:17:04,720 way we can actually use this example so 4559 03:17:04,720 --> 03:17:06,439 go ahead and make sure you have a 4560 03:17:06,439 --> 03:17:08,279 username and a password these two are 4561 03:17:08,279 --> 03:17:12,359 important okay so what I'm going to do 4562 03:17:12,359 --> 03:17:15,239 is whenever we we make a request to this 4563 03:17:15,239 --> 03:17:16,840 endpoint we're going to go ahead and 4564 03:17:16,840 --> 03:17:19,359 look up the user by the username and we 4565 03:17:19,359 --> 03:17:21,720 can assume that the username will be 4566 03:17:21,720 --> 03:17:24,439 unique which is pretty standard in 4567 03:17:24,439 --> 03:17:27,160 applications so I'm going to go ahead 4568 03:17:27,160 --> 03:17:27,960 and 4569 03:17:27,960 --> 03:17:30,279 destructure from the request body and 4570 03:17:30,279 --> 03:17:34,199 I'll do this all on this one single line 4571 03:17:34,199 --> 03:17:35,960 I'll D structure username and password 4572 03:17:35,960 --> 03:17:38,640 and of course we are assuming that these 4573 03:17:38,640 --> 03:17:42,439 fields do exist okay again I I would 4574 03:17:42,439 --> 03:17:46,239 recommend you yourself uh validate your 4575 03:17:46,239 --> 03:17:49,560 actual uh request bodies and if you 4576 03:17:49,560 --> 03:17:50,720 don't know how to do that check out the 4577 03:17:50,720 --> 03:17:53,199 section where we talked about validating 4578 03:17:53,199 --> 03:17:56,479 post requests or validation in general 4579 03:17:56,479 --> 03:17:58,760 and you'll learn about that so what I'm 4580 03:17:58,760 --> 03:18:01,640 going to do is now I'm going to 4581 03:18:01,640 --> 03:18:03,319 reference mock users but I do need to 4582 03:18:03,319 --> 03:18:07,600 import that into this index. MJS file so 4583 03:18:07,600 --> 03:18:09,479 let me go up top here and import mock 4584 03:18:09,479 --> 03:18:13,680 users from utils consents JS and then 4585 03:18:13,680 --> 03:18:15,479 I'll simply just do a search on the user 4586 03:18:15,479 --> 03:18:19,640 so const find user equals mock users 4587 03:18:19,640 --> 03:18:21,680 find and then we're going to pass in the 4588 03:18:21,680 --> 03:18:23,319 predicate so we want to search for the 4589 03:18:23,319 --> 03:18:26,120 user by username so I'm going to pass in 4590 03:18:26,120 --> 03:18:29,000 this predicate function and that the 4591 03:18:29,000 --> 03:18:30,239 argument for that function is going to 4592 03:18:30,239 --> 03:18:32,040 be the user object so I'm going to 4593 03:18:32,040 --> 03:18:34,600 reference user. username and compare it 4594 03:18:34,600 --> 03:18:36,439 to the username that we're sending to 4595 03:18:36,439 --> 03:18:38,439 this post request so this will search 4596 03:18:38,439 --> 03:18:40,720 for the user by the username and if it 4597 03:18:40,720 --> 03:18:44,160 does not return a user fine user will be 4598 03:18:44,160 --> 03:18:48,120 undefined so from here let's set this up 4599 03:18:48,120 --> 03:18:50,279 so what we'll do is we'll check to see 4600 03:18:50,279 --> 03:18:53,279 if find user is undefined so if if not 4601 03:18:53,279 --> 03:18:56,640 find user okay then what will happen is 4602 03:18:56,640 --> 03:18:58,680 we'll return a 4603 03:18:58,680 --> 03:19:02,800 401 return response that's status 4604 03:19:02,800 --> 03:19:07,160 41 which just stands for uh 4605 03:19:07,160 --> 03:19:10,239 unauthenticated so let's just do 4606 03:19:10,239 --> 03:19:13,120 send message you can also just send the 4607 03:19:13,120 --> 03:19:15,279 message as bad 4608 03:19:15,279 --> 03:19:17,120 credentials like 4609 03:19:17,120 --> 03:19:20,080 that so of course if the user is found 4610 03:19:20,080 --> 03:19:22,359 then this whole if statement would not 4611 03:19:22,359 --> 03:19:23,239 be 4612 03:19:23,239 --> 03:19:25,760 executed but if it is not found then it 4613 03:19:25,760 --> 03:19:26,800 would return a 4614 03:19:26,800 --> 03:19:29,840 401 next check that we want to perform 4615 03:19:29,840 --> 03:19:32,640 is checking the passwords making sure 4616 03:19:32,640 --> 03:19:35,040 that they match and like I said we are 4617 03:19:35,040 --> 03:19:36,840 checking raw passwords that's okay for 4618 03:19:36,840 --> 03:19:38,520 now later on you'll learn how to 4619 03:19:38,520 --> 03:19:40,080 actually hash the passwords and save it 4620 03:19:40,080 --> 03:19:43,319 to the database and then also be able to 4621 03:19:43,319 --> 03:19:46,399 compare hash passwords so I'm going to 4622 03:19:46,399 --> 03:19:49,840 go ahead and write another if so if find 4623 03:19:49,840 --> 03:19:52,239 user do username oops sorry not using 4624 03:19:52,239 --> 03:19:54,000 password I'm going to do if it's not 4625 03:19:54,000 --> 03:19:55,920 equal to password then I'm going to 4626 03:19:55,920 --> 03:19:58,399 return the same exact response right 4627 03:19:58,399 --> 03:20:00,680 over 4628 03:20:00,840 --> 03:20:04,640 here um okay let's 4629 03:20:04,640 --> 03:20:08,399 see okay that's fine and then I could 4630 03:20:08,399 --> 03:20:09,479 probably also just you know what I could 4631 03:20:09,479 --> 03:20:12,960 probably just combine this into one 4632 03:20:12,960 --> 03:20:16,600 single statement so if there's no user 4633 03:20:16,600 --> 03:20:18,680 or if the passwords don't match then 4634 03:20:18,680 --> 03:20:20,080 we'll just send this back I guess that 4635 03:20:20,080 --> 03:20:21,760 can work fine now remember the main 4636 03:20:21,760 --> 03:20:23,800 purpose of this endpoint is to modify 4637 03:20:23,800 --> 03:20:27,199 the session object because we want to 4638 03:20:27,199 --> 03:20:29,920 stop generating new session IDs every 4639 03:20:29,920 --> 03:20:32,199 single time we want to be able to allow 4640 03:20:32,199 --> 03:20:35,439 Express session to generate that session 4641 03:20:35,439 --> 03:20:38,080 ID once set the cookie and then send 4642 03:20:38,080 --> 03:20:41,000 that cookie back to the browser or the 4643 03:20:41,000 --> 03:20:43,960 client so that way when the client sends 4644 03:20:43,960 --> 03:20:46,560 another request we can use that session 4645 03:20:46,560 --> 03:20:49,560 ID and look up the session data and from 4646 03:20:49,560 --> 03:20:52,399 the session data we can see which user 4647 03:20:52,399 --> 03:20:55,359 actually belongs to this session ID so 4648 03:20:55,359 --> 03:20:57,120 what I'll do here is whoops what I'll do 4649 03:20:57,120 --> 03:20:58,600 here is I'm going to 4650 03:20:58,600 --> 03:21:01,600 reference 4651 03:21:01,680 --> 03:21:04,479 request. session and then I'm going to 4652 03:21:04,479 --> 03:21:07,960 attach this Dynamic Property called 4653 03:21:07,960 --> 03:21:10,680 user and I'm going to attach the fine 4654 03:21:10,680 --> 03:21:13,279 user value to this user property so 4655 03:21:13,279 --> 03:21:15,720 remember we can attach Dynamic 4656 03:21:15,720 --> 03:21:18,680 properties to objects in JavaScript so 4657 03:21:18,680 --> 03:21:21,160 I'm going to do that right over here and 4658 03:21:21,160 --> 03:21:23,720 then this should modify the session 4659 03:21:23,720 --> 03:21:27,680 object and that will have Express 4660 03:21:27,680 --> 03:21:30,279 session set a cookie and send it back to 4661 03:21:30,279 --> 03:21:32,359 the browser or the client as a 4662 03:21:32,359 --> 03:21:34,359 response okay and then the browser will 4663 03:21:34,359 --> 03:21:36,439 have that cookie and now they can use 4664 03:21:36,439 --> 03:21:39,080 that cookie to send it to the server and 4665 03:21:39,080 --> 03:21:43,640 the server will know who that user is so 4666 03:21:43,640 --> 03:21:47,960 watch this uh let me go ahead and return 4667 03:21:47,960 --> 03:21:52,239 response. status I guess 200 would be 4668 03:21:52,239 --> 03:21:55,560 fine for logging in and then send let's 4669 03:21:55,560 --> 03:21:57,680 send back the user that was 4670 03:21:57,680 --> 03:21:59,680 authenticated okay so this 4671 03:21:59,680 --> 03:22:03,600 endpoint should work let's just test it 4672 03:22:03,600 --> 03:22:06,520 out so let me go ahead and make a new 4673 03:22:06,520 --> 03:22:12,000 request post request Local Host port th/ 4674 03:22:12,000 --> 03:22:16,279 / o and we'll pass in a 4675 03:22:16,279 --> 03:22:19,000 username so let's actually uh let me go 4676 03:22:19,000 --> 03:22:22,120 into my mock users array so I'll do 4677 03:22:22,120 --> 03:22:25,120 Anson and then the password for Anson is 4678 03:22:25,120 --> 03:22:26,800 hello 4679 03:22:26,800 --> 03:22:30,760 123 so Anson and 4680 03:22:30,760 --> 03:22:33,640 then 4681 03:22:33,640 --> 03:22:36,319 password okay so now you can see that 4682 03:22:36,319 --> 03:22:39,279 right over here I have logged in okay 4683 03:22:39,279 --> 03:22:40,479 obviously you wouldn't want to return 4684 03:22:40,479 --> 03:22:41,880 the password back but like I said just a 4685 03:22:41,880 --> 03:22:44,359 simple example if I look at the cookies 4686 03:22:44,359 --> 03:22:45,600 I can see that I have my cookie right 4687 03:22:45,600 --> 03:22:46,359 over 4688 03:22:46,359 --> 03:22:49,199 here okay this is my session ID right 4689 03:22:49,199 --> 03:22:51,439 over here so now that I have logged in 4690 03:22:51,439 --> 03:22:54,479 and I have a cookie set on this client 4691 03:22:54,479 --> 03:22:56,439 side right over here I can now make 4692 03:22:56,439 --> 03:23:00,319 requests to the server and the server 4693 03:23:00,319 --> 03:23:03,479 will receive that cookie it's going to 4694 03:23:03,479 --> 03:23:06,640 parse it and it's going to take that 4695 03:23:06,640 --> 03:23:09,080 session ID that I got from the cookie 4696 03:23:09,080 --> 03:23:13,120 and then map the session data object to 4697 03:23:13,120 --> 03:23:16,000 that session ID itself so what I'm what 4698 03:23:16,000 --> 03:23:19,199 I'm going to do now is this I'm going to 4699 03:23:19,199 --> 03:23:20,199 go 4700 03:23:20,199 --> 03:23:24,040 ahead and set up one more endpoint 4701 03:23:24,040 --> 03:23:28,000 app.get API status and this is just 4702 03:23:28,000 --> 03:23:29,640 going to show the authenticated status 4703 03:23:29,640 --> 03:23:32,880 of the user so whether the user is 4704 03:23:32,880 --> 03:23:36,279 authenticated or not in in this context 4705 03:23:36,279 --> 03:23:39,880 we can tell if the user is authenticated 4706 03:23:39,880 --> 03:23:42,239 by checking to see if request. session. 4707 03:23:42,239 --> 03:23:45,160 users defined because remember this 4708 03:23:45,160 --> 03:23:47,560 right over here is what actually 4709 03:23:47,560 --> 03:23:50,120 indicates that the user is quote unquote 4710 03:23:50,120 --> 03:23:51,920 logged in the moment that we modify the 4711 03:23:51,920 --> 03:23:55,120 session and we're only doing it when we 4712 03:23:55,120 --> 03:23:57,479 find the user and the passwords match 4713 03:23:57,479 --> 03:24:00,040 okay so what I can do is I can just Che 4714 03:24:00,040 --> 03:24:03,239 simply check uh let's do 4715 03:24:03,239 --> 03:24:06,800 this I can use a turn operator so return 4716 03:24:06,800 --> 03:24:11,000 request. session if user is defined at 4717 03:24:11,000 --> 03:24:12,800 all 4718 03:24:12,800 --> 03:24:14,520 then what I'll do is I'll returnal 4719 03:24:14,520 --> 03:24:18,680 response status code of 200 and I'll 4720 03:24:18,680 --> 03:24:22,319 just send back request. session. 4721 03:24:22,319 --> 03:24:25,800 user and then if the user is not defined 4722 03:24:25,800 --> 03:24:27,680 on the session object then I'll just 4723 03:24:27,680 --> 03:24:30,040 send back a 401 so I'm just going to 4724 03:24:30,040 --> 03:24:32,880 copy what I have up here but I'll just 4725 03:24:32,880 --> 03:24:37,600 simply say un uh not 4726 03:24:37,600 --> 03:24:39,960 authenticated okay hopefully this get 4727 03:24:39,960 --> 03:24:41,920 request that I'm setting up makes sense 4728 03:24:41,920 --> 03:24:43,520 so we have one endpoint that actually 4729 03:24:43,520 --> 03:24:45,760 does the core authentication the other 4730 03:24:45,760 --> 03:24:48,439 endpoint gets the authentication status 4731 03:24:48,439 --> 03:24:52,840 hence why I call this endpoint uh SL API 4732 03:24:52,840 --> 03:24:55,760 status so our server just restarted so 4733 03:24:55,760 --> 03:24:57,760 again everything is in memory so it gets 4734 03:24:57,760 --> 03:25:00,319 cleared so we do need to reauthenticate 4735 03:25:00,319 --> 03:25:01,880 again so I'll just show you real quick 4736 03:25:01,880 --> 03:25:04,040 let me delete all the 4737 03:25:04,040 --> 03:25:08,600 cookies okay me 4738 03:25:08,640 --> 03:25:12,600 just okay so let's click send to the 4739 03:25:12,600 --> 03:25:15,600 same endpoint for/ API we're going to 4740 03:25:15,600 --> 03:25:18,160 send this username and password 4741 03:25:18,160 --> 03:25:20,960 payload okay so I just got back a cookie 4742 03:25:20,960 --> 03:25:21,880 right over 4743 03:25:21,880 --> 03:25:27,560 here and now watch this if I were to now 4744 03:25:27,560 --> 03:25:29,880 visit the status 4745 03:25:29,880 --> 03:25:33,800 endpoint API status endpoint and make a 4746 03:25:33,800 --> 03:25:36,399 get request to it you're going to see 4747 03:25:36,399 --> 03:25:39,120 that the response is going to be the 4748 03:25:39,120 --> 03:25:42,120 actual off user okay 4749 03:25:42,120 --> 03:25:44,560 the response is the actual off user 4750 03:25:44,560 --> 03:25:47,680 watch what happens if I clear my 4751 03:25:47,680 --> 03:25:50,000 cookie okay so if I clear my cookie 4752 03:25:50,000 --> 03:25:52,399 right now and if I try to access this 4753 03:25:52,399 --> 03:25:55,560 endpoint you see how it gives me a 4754 03:25:55,560 --> 03:25:58,640 401 unauthorized error see not 4755 03:25:58,640 --> 03:26:00,359 authenticated right over 4756 03:26:00,359 --> 03:26:03,239 here and remember that I cleared the 4757 03:26:03,239 --> 03:26:06,680 cookie from my client so the server 4758 03:26:06,680 --> 03:26:08,160 received the request and received this 4759 03:26:08,160 --> 03:26:12,080 get request without any valid cookie 4760 03:26:12,080 --> 03:26:14,560 okay and since there was no valid cookie 4761 03:26:14,560 --> 03:26:16,399 there was no mapping between that 4762 03:26:16,399 --> 03:26:19,760 session ID from the cookie to a user 4763 03:26:19,760 --> 03:26:21,800 that's in the session 4764 03:26:21,800 --> 03:26:25,279 store so hopefully that makes sense and 4765 03:26:25,279 --> 03:26:27,120 additionally what I'll do is let me just 4766 03:26:27,120 --> 03:26:31,960 quickly reference request. session store 4767 03:26:31,960 --> 03:26:33,760 get and let me call the get method and 4768 03:26:33,760 --> 03:26:36,920 let me pass in that session 4769 03:26:36,920 --> 03:26:41,920 ID session ID and then error session I'm 4770 03:26:41,920 --> 03:26:44,040 just going to log the 4771 03:26:44,040 --> 03:26:47,319 session just to show you all how 4772 03:26:47,319 --> 03:26:49,160 everything again is stored in 4773 03:26:49,160 --> 03:26:52,920 memory so let's go ahead and do this 4774 03:26:52,920 --> 03:26:57,040 let's make a post request to SL API of 4775 03:26:57,040 --> 03:26:59,080 course if I pass an invalid password 4776 03:26:59,080 --> 03:27:00,080 it'll say bad 4777 03:27:00,080 --> 03:27:02,160 credentials if the 4778 03:27:02,160 --> 03:27:04,239 user is not 4779 03:27:04,239 --> 03:27:06,520 found same 4780 03:27:06,520 --> 03:27:10,319 thing so let's log in 4781 03:27:10,319 --> 03:27:13,840 okay I have my cookie right over 4782 03:27:13,840 --> 03:27:17,560 here and now let's visit the status 4783 03:27:17,560 --> 03:27:21,960 endpoint for SL API status okay we're 4784 03:27:21,960 --> 03:27:23,720 good to go with that and if we look at 4785 03:27:23,720 --> 03:27:24,640 the 4786 03:27:24,640 --> 03:27:26,840 console I want to show you that right 4787 03:27:26,840 --> 03:27:28,160 over 4788 03:27:28,160 --> 03:27:32,160 here we are logging the user in the 4789 03:27:32,160 --> 03:27:35,439 session store okay right over 4790 03:27:35,439 --> 03:27:38,840 here so if I go back and click Send 4791 03:27:38,840 --> 03:27:40,920 you'll see that it's logged every single 4792 03:27:40,920 --> 03:27:43,319 time we're grabbing the session data 4793 03:27:43,319 --> 03:27:45,239 from the session store and the session 4794 03:27:45,239 --> 03:27:47,880 data object that we retrieve by that 4795 03:27:47,880 --> 03:27:50,920 session ID has this cookie property that 4796 03:27:50,920 --> 03:27:53,040 gives us information about the cookie 4797 03:27:53,040 --> 03:27:55,520 and then it has this user property which 4798 03:27:55,520 --> 03:27:58,720 is the user object itself so that is how 4799 03:27:58,720 --> 03:28:01,720 the session ID maps to a user on the 4800 03:28:01,720 --> 03:28:03,479 server side so I hope I really hope this 4801 03:28:03,479 --> 03:28:06,399 makes sense because if you understand 4802 03:28:06,399 --> 03:28:08,840 this then you can understand really 4803 03:28:08,840 --> 03:28:12,359 anything with sessions and I really hope 4804 03:28:12,359 --> 03:28:14,080 that all of this makes sense because I 4805 03:28:14,080 --> 03:28:16,000 can understand sessions are kind of a 4806 03:28:16,000 --> 03:28:18,080 little bit difficult to understand but 4807 03:28:18,080 --> 03:28:21,279 by seeing how the data is saved on the 4808 03:28:21,279 --> 03:28:23,359 server side and how we can actually 4809 03:28:23,359 --> 03:28:25,359 retrieve it so easily like this and map 4810 03:28:25,359 --> 03:28:28,319 it to the session ID that can help you 4811 03:28:28,319 --> 03:28:30,520 understand sessions a lot better now 4812 03:28:30,520 --> 03:28:33,279 what I'll do is I will authenticate on 4813 03:28:33,279 --> 03:28:35,359 Thunder client with this set of 4814 03:28:35,359 --> 03:28:37,239 credentials and then I'm also going to 4815 03:28:37,239 --> 03:28:39,279 authenticate on Postman with a different 4816 03:28:39,279 --> 03:28:41,439 set of credentials just so that you can 4817 03:28:41,439 --> 03:28:44,600 see how with different clients we can 4818 03:28:44,600 --> 03:28:46,880 authenticate with different credentials 4819 03:28:46,880 --> 03:28:49,279 and we can see the status of our 4820 03:28:49,279 --> 03:28:50,560 authentication and then you'll actually 4821 03:28:50,560 --> 03:28:52,680 see how the server manages multiple 4822 03:28:52,680 --> 03:28:55,479 different sessions so let's just very 4823 03:28:55,479 --> 03:28:57,880 quickly uh let's see okay so we're not 4824 03:28:57,880 --> 03:28:59,319 authenticated so let's make a post 4825 03:28:59,319 --> 03:29:04,239 request to SL API with uh username Anson 4826 03:29:04,239 --> 03:29:06,640 and the password hello 123 okay so we're 4827 03:29:06,640 --> 03:29:08,640 authenticated here uh let me grab 4828 03:29:08,640 --> 03:29:12,600 another set of credentials so Jack and 4829 03:29:12,600 --> 03:29:16,760 hello 124 so let me go into post man 4830 03:29:16,760 --> 03:29:20,319 make a post request 4831 03:29:20,319 --> 03:29:25,920 HTTP Local Host Port 3000 API off let's 4832 03:29:25,920 --> 03:29:30,000 click on body Tab and then select raw 4833 03:29:30,000 --> 03:29:32,720 and then make sure it's set to Json 4834 03:29:32,720 --> 03:29:34,319 whoops and then so 4835 03:29:34,319 --> 03:29:38,399 now set username to Jack and the 4836 03:29:38,399 --> 03:29:39,399 password 4837 03:29:39,399 --> 03:29:41,680 was hello 4838 03:29:41,680 --> 03:29:44,880 12 4 okay and remember we're in a 4839 03:29:44,880 --> 03:29:47,680 completely different client right now 4840 03:29:47,680 --> 03:29:50,720 Postman is its own environment obviously 4841 03:29:50,720 --> 03:29:52,160 and just to show you this we don't have 4842 03:29:52,160 --> 03:29:55,120 any cookies set right now so the moment 4843 03:29:55,120 --> 03:29:59,840 that I click uh send right over here so 4844 03:29:59,840 --> 03:30:01,680 I just made a request and you can see 4845 03:30:01,680 --> 03:30:03,760 it's right over here if I click cookies 4846 03:30:03,760 --> 03:30:06,640 now you'll notice how on my Postman 4847 03:30:06,640 --> 03:30:09,760 client I now have this cookie and of 4848 03:30:09,760 --> 03:30:11,640 course this cookie is different 4849 03:30:11,640 --> 03:30:13,960 than the cookie that we have on Thunder 4850 03:30:13,960 --> 03:30:15,640 client because we're two different users 4851 03:30:15,640 --> 03:30:17,359 now that's the thing that I'm trying to 4852 03:30:17,359 --> 03:30:18,720 show you we're two different users you 4853 03:30:18,720 --> 03:30:22,080 can see that the cookies are 4854 03:30:22,080 --> 03:30:27,439 different okay so if I were to 4855 03:30:27,880 --> 03:30:30,479 visit this 4856 03:30:30,479 --> 03:30:34,520 API status endpoint with a get request 4857 03:30:34,520 --> 03:30:38,040 you can see that uh it tells me um let 4858 03:30:38,040 --> 03:30:39,439 me 4859 03:30:39,439 --> 03:30:42,319 see here we go I don't know why that was 4860 03:30:42,319 --> 03:30:44,279 formatted like that okay so you can see 4861 03:30:44,279 --> 03:30:47,600 that in on Postman I am logged in as 4862 03:30:47,600 --> 03:30:50,560 Jack and then on the Thunder 4863 03:30:50,560 --> 03:30:55,319 client I am logged in as 4864 03:30:55,319 --> 03:30:58,520 Anson see uh whoops right over here look 4865 03:30:58,520 --> 03:31:00,920 at the response I'm logged in as Anson 4866 03:31:00,920 --> 03:31:02,920 so now we have two users that are quote 4867 03:31:02,920 --> 03:31:05,520 unquote logged in and you can see that 4868 03:31:05,520 --> 03:31:08,840 the session store contains both users 4869 03:31:08,840 --> 03:31:12,640 they contain Jack and and Anson so 4870 03:31:12,640 --> 03:31:14,560 hopefully this also helps you understand 4871 03:31:14,560 --> 03:31:17,040 how the server can manage not only one 4872 03:31:17,040 --> 03:31:19,640 but many sessions and map each session 4873 03:31:19,640 --> 03:31:22,279 to different users now I'll show you 4874 03:31:22,279 --> 03:31:24,279 even one more example where we can set 4875 03:31:24,279 --> 03:31:26,239 up a virtual cart system for 4876 03:31:26,239 --> 03:31:28,680 authenticated users so to keep things 4877 03:31:28,680 --> 03:31:30,680 simple I'll set up a simple post 4878 03:31:30,680 --> 03:31:32,600 request and then the end point will be 4879 03:31:32,600 --> 03:31:35,800 SL API SLC cart pass in our request 4880 03:31:35,800 --> 03:31:38,560 Handler and then we only want the user 4881 03:31:38,560 --> 03:31:40,760 to be able to add items to a cart if 4882 03:31:40,760 --> 03:31:44,680 they are authenticated so if let's say 4883 03:31:44,680 --> 03:31:49,279 request if there's no request. session. 4884 03:31:49,279 --> 03:31:50,920 user 4885 03:31:50,920 --> 03:31:52,479 return 4886 03:31:52,479 --> 03:31:55,840 response I'll just send status of 4887 03:31:55,840 --> 03:31:58,840 401 okay however the user is 4888 03:31:58,840 --> 03:32:00,600 authenticated the next thing that we 4889 03:32:00,600 --> 03:32:02,439 want to do is of course check to see if 4890 03:32:02,439 --> 03:32:05,199 the cart already exists on the session 4891 03:32:05,199 --> 03:32:06,640 object because if it doesn't we need to 4892 03:32:06,640 --> 03:32:09,279 add it if it does we can just simply add 4893 03:32:09,279 --> 03:32:13,080 the item to the session cart now of 4894 03:32:13,080 --> 03:32:15,160 course the request body itself will just 4895 03:32:15,160 --> 03:32:17,920 pretty much be data relating to an item 4896 03:32:17,920 --> 03:32:20,080 that we're adding to the cart so just to 4897 03:32:20,080 --> 03:32:22,040 keep things simple the request body for 4898 03:32:22,040 --> 03:32:24,640 this endpoint will just have a name for 4899 03:32:24,640 --> 03:32:26,279 the item and the price for the item 4900 03:32:26,279 --> 03:32:29,399 that's it so let me just destructure the 4901 03:32:29,399 --> 03:32:30,960 request 4902 03:32:30,960 --> 03:32:35,160 body from the request object and I'm 4903 03:32:35,160 --> 03:32:38,080 going to rename this to item and we're 4904 03:32:38,080 --> 03:32:40,680 going to assume that the field and all 4905 03:32:40,680 --> 03:32:43,120 the the values for our request body are 4906 03:32:43,120 --> 03:32:46,120 valid but I encourage you to practice 4907 03:32:46,120 --> 03:32:48,960 and actually validate your request 4908 03:32:48,960 --> 03:32:51,760 bodies so first thing we need to do is 4909 03:32:51,760 --> 03:32:53,720 check to see if the cart exists on the 4910 03:32:53,720 --> 03:32:56,080 session object and that's pretty easy to 4911 03:32:56,080 --> 03:32:59,600 do I'm going to go ahead and destructure 4912 03:32:59,600 --> 03:33:02,640 the cart property from the session 4913 03:33:02,640 --> 03:33:05,840 object so cons cart and then inside 4914 03:33:05,840 --> 03:33:07,199 these curly braces where the cart 4915 03:33:07,199 --> 03:33:10,279 variable goes equals request. session so 4916 03:33:10,279 --> 03:33:12,800 if C is already defined I could just 4917 03:33:12,800 --> 03:33:15,160 simply do cart. push item and this 4918 03:33:15,160 --> 03:33:17,640 should be able to just modify the 4919 03:33:17,640 --> 03:33:21,800 cart and then if the cart doesn't exist 4920 03:33:21,800 --> 03:33:24,359 then what I can do is I can reference 4921 03:33:24,359 --> 03:33:27,800 request. session. cart 4922 03:33:27,800 --> 03:33:30,600 equals an array and then pass in that 4923 03:33:30,600 --> 03:33:33,399 item as the first element in this card 4924 03:33:33,399 --> 03:33:36,080 array just like that and once we're done 4925 03:33:36,080 --> 03:33:37,840 we can just return a 4926 03:33:37,840 --> 03:33:41,279 response status of 2011 4927 03:33:41,279 --> 03:33:42,720 and I'll just send back the original 4928 03:33:42,720 --> 03:33:45,880 request body so let's test out this 4929 03:33:45,880 --> 03:33:49,120 logic let me go into Postman so I'm 4930 03:33:49,120 --> 03:33:51,600 currently not authenticated that's okay 4931 03:33:51,600 --> 03:33:55,359 if I try to make a post request to API 4932 03:33:55,359 --> 03:33:57,840 slart it's going to give me unauthorized 4933 03:33:57,840 --> 03:34:00,960 that's perfectly fine so let's first 4934 03:34:00,960 --> 03:34:03,720 authorize so make a post request with 4935 03:34:03,720 --> 03:34:08,600 our credentials to SL API off so we are 4936 03:34:08,600 --> 03:34:11,359 authenticated and now let's go ahead and 4937 03:34:11,359 --> 03:34:15,199 add items to our cart so name 4938 03:34:15,199 --> 03:34:18,040 orange and then 4939 03:34:18,040 --> 03:34:21,439 price let's do oops that should be a 4940 03:34:21,439 --> 03:34:25,920 number let's do 199 click Send okay so 4941 03:34:25,920 --> 03:34:27,120 that's our 4942 03:34:27,120 --> 03:34:30,359 cart okay so now I can see that this was 4943 03:34:30,359 --> 03:34:31,720 the item that was returned so that's 4944 03:34:31,720 --> 03:34:33,560 good so what I want to do now is I want 4945 03:34:33,560 --> 03:34:36,680 to actually see the cart so what we can 4946 03:34:36,680 --> 03:34:41,080 do is make a get request to off SL 4947 03:34:41,080 --> 03:34:43,359 status and this should return I guess 4948 03:34:43,359 --> 03:34:44,800 only the user but we want to get the 4949 03:34:44,800 --> 03:34:48,080 cart so let me do this uh let me set up 4950 03:34:48,080 --> 03:34:49,479 a 4951 03:34:49,479 --> 03:34:54,279 quick app.get route for the cart 4952 03:34:55,920 --> 03:34:58,040 itself so I'm just simply setting up a 4953 03:34:58,040 --> 03:35:02,359 get request for/ apsc cart and then I'm 4954 03:35:02,359 --> 03:35:03,760 going to do the same thing up top over 4955 03:35:03,760 --> 03:35:06,640 here if there's no user defined in the 4956 03:35:06,640 --> 03:35:08,520 session object then they are 4957 03:35:08,520 --> 03:35:09,840 unauthorized so we're going to return a 4958 03:35:09,840 --> 03:35:11,239 401 4959 03:35:11,239 --> 03:35:15,880 and then I'm just simply going to return 4960 03:35:15,880 --> 03:35:17,399 response. 4961 03:35:17,399 --> 03:35:20,800 send request. 4962 03:35:20,800 --> 03:35:22,680 session. 4963 03:35:22,680 --> 03:35:25,880 cart and if cart is undefined we'll just 4964 03:35:25,880 --> 03:35:28,680 return an empty array like this using 4965 03:35:28,680 --> 03:35:31,800 this double double question mark 4966 03:35:31,800 --> 03:35:34,760 operator okay so if cart is undefined 4967 03:35:34,760 --> 03:35:37,080 will return empty array so let's test 4968 03:35:37,080 --> 03:35:38,520 this out again so we need to 4969 03:35:38,520 --> 03:35:41,439 reauthenticate so let me do do 4970 03:35:41,439 --> 03:35:44,359 this okay so make a post 4971 03:35:44,359 --> 03:35:46,880 request to the Au end point so now we 4972 03:35:46,880 --> 03:35:48,040 are logged 4973 03:35:48,040 --> 03:35:50,800 in and now I can verify I am logged in 4974 03:35:50,800 --> 03:35:53,640 so that's good so if I try to get my 4975 03:35:53,640 --> 03:35:55,359 cart you can see that I have this empty 4976 03:35:55,359 --> 03:35:57,720 array but if I try to add items to the 4977 03:35:57,720 --> 03:36:00,199 cart and if I make a get request you can 4978 03:36:00,199 --> 03:36:01,640 see that I actually have items in the 4979 03:36:01,640 --> 03:36:04,600 cart so pretend like this is the API 4980 03:36:04,600 --> 03:36:06,680 that your front is calling and then as 4981 03:36:06,680 --> 03:36:08,120 you want to add items to the cart for 4982 03:36:08,120 --> 03:36:10,160 the user you would just pretty much add 4983 03:36:10,160 --> 03:36:12,520 it to the session data and then whenever 4984 03:36:12,520 --> 03:36:14,880 you want to get all the users items in 4985 03:36:14,880 --> 03:36:16,479 the cart you can just grab it from the 4986 03:36:16,479 --> 03:36:19,040 back end like this and I can add more 4987 03:36:19,040 --> 03:36:20,760 items let's 4988 03:36:20,760 --> 03:36:23,279 do uh 4989 03:36:23,279 --> 03:36:25,199 Gatorade and 4990 03:36:25,199 --> 03:36:27,479 299 and let's make a get request and you 4991 03:36:27,479 --> 03:36:30,199 can see now I have two items in my cart 4992 03:36:30,199 --> 03:36:32,080 and let's go back to postman and 4993 03:36:32,080 --> 03:36:35,399 authenticate as Jack So currently on 4994 03:36:35,399 --> 03:36:37,800 Postman we are unauthenticated I'm going 4995 03:36:37,800 --> 03:36:40,800 to make a post request to the/ API SL 4996 03:36:40,800 --> 03:36:44,120 off endpoint and pass my credentials so 4997 03:36:44,120 --> 03:36:46,720 now we are authenticated so that's 4998 03:36:46,720 --> 03:36:49,920 good and if I try to get my cart for 4999 03:36:49,920 --> 03:36:51,760 Jack you can see it's just an empty 5000 03:36:51,760 --> 03:36:53,399 array because we don't have any items in 5001 03:36:53,399 --> 03:36:56,279 our cart so let's fix that let's add 5002 03:36:56,279 --> 03:36:58,399 some items to the 5003 03:36:58,399 --> 03:37:01,359 cart so let's 5004 03:37:01,359 --> 03:37:03,680 do 5005 03:37:03,680 --> 03:37:06,520 name 5006 03:37:06,520 --> 03:37:09,399 broccoli and then 5007 03:37:09,399 --> 03:37:12,920 $4.99 okay so now I added broccoli to my 5008 03:37:12,920 --> 03:37:15,760 cart as Jack when I make a get request 5009 03:37:15,760 --> 03:37:19,359 this is my virtual cart that comes from 5010 03:37:19,359 --> 03:37:22,520 the back end and then on Thunder client 5011 03:37:22,520 --> 03:37:25,560 where I am authenticated as anen this is 5012 03:37:25,560 --> 03:37:28,840 my own virtual card so each user each 5013 03:37:28,840 --> 03:37:32,040 session has their own data okay each 5014 03:37:32,040 --> 03:37:33,840 session maps to its own user and then it 5015 03:37:33,840 --> 03:37:36,080 maps to its own virtual cart for that 5016 03:37:36,080 --> 03:37:38,479 user and whatever other session data 5017 03:37:38,479 --> 03:37:40,560 that we need to add we can just very 5018 03:37:40,560 --> 03:37:44,160 easily attach it to the session object 5019 03:37:44,160 --> 03:37:46,720 itself so I hope all this makes sense 5020 03:37:46,720 --> 03:37:48,359 and like I said I wanted to give you all 5021 03:37:48,359 --> 03:37:50,560 multiple examples because I understand 5022 03:37:50,560 --> 03:37:52,160 sessions can be a little bit tricky to 5023 03:37:52,160 --> 03:37:53,880 understand sometimes but what we're 5024 03:37:53,880 --> 03:37:56,640 going to do is move on to actually 5025 03:37:56,640 --> 03:37:59,520 learning how to set up authentication 5026 03:37:59,520 --> 03:38:03,960 with a library such as passportjs 5027 03:38:05,760 --> 03:38:08,000 in this part of our tutorial I'm going 5028 03:38:08,000 --> 03:38:10,439 to teach you how to use passportjs 5029 03:38:10,439 --> 03:38:12,720 to integrate authentication for your 5030 03:38:12,720 --> 03:38:14,479 express application we're going to be 5031 03:38:14,479 --> 03:38:17,000 using a local strategy for passport 5032 03:38:17,000 --> 03:38:19,000 which just means that instead of using a 5033 03:38:19,000 --> 03:38:21,239 thirdparty provider like Google Facebook 5034 03:38:21,239 --> 03:38:24,279 Twitter we're actually going to be using 5035 03:38:24,279 --> 03:38:26,600 credentials that are saved on the actual 5036 03:38:26,600 --> 03:38:28,920 applications database in our case we 5037 03:38:28,920 --> 03:38:31,080 don't have a database so it'll be saved 5038 03:38:31,080 --> 03:38:33,239 in an array but the idea is for local 5039 03:38:33,239 --> 03:38:35,800 authentication the application saves 5040 03:38:35,800 --> 03:38:37,960 that information compared to using just 5041 03:38:37,960 --> 03:38:40,359 a third party provider to log into their 5042 03:38:40,359 --> 03:38:41,680 application I'll show you how to use 5043 03:38:41,680 --> 03:38:43,960 local authentication and then in later 5044 03:38:43,960 --> 03:38:45,600 parts of the tutorial I'll show you how 5045 03:38:45,600 --> 03:38:47,960 to use OA 2 so that you can learn how to 5046 03:38:47,960 --> 03:38:50,600 integrate thirdparty providers to log 5047 03:38:50,600 --> 03:38:52,239 into your application using something 5048 03:38:52,239 --> 03:38:54,479 like Discord or GitHub or Twitter so 5049 03:38:54,479 --> 03:38:55,920 let's go ahead and get started the first 5050 03:38:55,920 --> 03:38:57,279 thing that we're going to do is install 5051 03:38:57,279 --> 03:39:00,720 passport so I'll type npmi passport and 5052 03:39:00,720 --> 03:39:02,800 since we're using a local strategy we 5053 03:39:02,800 --> 03:39:04,279 need to make sure we install the correct 5054 03:39:04,279 --> 03:39:06,560 strategy package so aside from passport 5055 03:39:06,560 --> 03:39:08,840 we need to also install passport hyphen 5056 03:39:08,840 --> 03:39:11,000 local again if you were using 5057 03:39:11,000 --> 03:39:14,279 using let's say Facebook as the way to 5058 03:39:14,279 --> 03:39:16,720 authenticate users to your platform then 5059 03:39:16,720 --> 03:39:18,279 you would have to install passport 5060 03:39:18,279 --> 03:39:20,239 hyphen Facebook as the strategy and all 5061 03:39:20,239 --> 03:39:22,560 it is is just a package that you install 5062 03:39:22,560 --> 03:39:24,720 with passport so let's go ahead and hit 5063 03:39:24,720 --> 03:39:26,399 enter and this will install the packages 5064 03:39:26,399 --> 03:39:28,479 for us and let me just go ahead and run 5065 03:39:28,479 --> 03:39:29,520 my 5066 03:39:29,520 --> 03:39:32,040 server and now we'll go ahead and 5067 03:39:32,040 --> 03:39:34,520 configure everything in our application 5068 03:39:34,520 --> 03:39:36,319 so what I need to do is I first need to 5069 03:39:36,319 --> 03:39:37,920 import 5070 03:39:37,920 --> 03:39:41,439 passport so import passport from 5071 03:39:41,439 --> 03:39:43,960 passport now passport integrates really 5072 03:39:43,960 --> 03:39:46,279 well with Express session and many times 5073 03:39:46,279 --> 03:39:48,520 you will actually use express session 5074 03:39:48,520 --> 03:39:50,560 with passport or vice versa passport 5075 03:39:50,560 --> 03:39:52,960 with Express session you don't need 5076 03:39:52,960 --> 03:39:55,199 Express session with passport but it is 5077 03:39:55,199 --> 03:39:57,399 highly recommended and many people do in 5078 03:39:57,399 --> 03:39:59,479 fact use both of them together because 5079 03:39:59,479 --> 03:40:02,080 passport will take care of mapping that 5080 03:40:02,080 --> 03:40:05,000 user that was just logging in with the 5081 03:40:05,000 --> 03:40:06,960 session ID if you need to recap on 5082 03:40:06,960 --> 03:40:08,399 sessions check out the previous section 5083 03:40:08,399 --> 03:40:10,000 of the tutorial where I actually showed 5084 03:40:10,000 --> 03:40:11,560 you multiple examples of how to use 5085 03:40:11,560 --> 03:40:13,319 express session so what I'm going to do 5086 03:40:13,319 --> 03:40:15,040 after importing passport is I'm going to 5087 03:40:15,040 --> 03:40:17,960 enable it so where I am registering the 5088 03:40:17,960 --> 03:40:19,160 session middleware which is right over 5089 03:40:19,160 --> 03:40:20,760 here I want to make sure I'm registering 5090 03:40:20,760 --> 03:40:22,600 passport after I enable the session 5091 03:40:22,600 --> 03:40:24,479 middleware because that needs to be done 5092 03:40:24,479 --> 03:40:25,800 before and then we also need to make 5093 03:40:25,800 --> 03:40:27,439 sure we enable password before we 5094 03:40:27,439 --> 03:40:30,560 register our routes so in between the 5095 03:40:30,560 --> 03:40:32,319 session middleware being registered and 5096 03:40:32,319 --> 03:40:33,600 where we are registering our routes I'm 5097 03:40:33,600 --> 03:40:36,279 going to go ahead and call app.use and 5098 03:40:36,279 --> 03:40:38,960 I'm going to reference passport and call 5099 03:40:38,960 --> 03:40:41,239 the initialize method 5100 03:40:41,239 --> 03:40:45,199 just like that okay and then since we 5101 03:40:45,199 --> 03:40:47,640 are using sessions I'm going to call app 5102 03:40:47,640 --> 03:40:50,840 values again and pass passport. 5103 03:40:50,840 --> 03:40:53,359 session primarily this will take care of 5104 03:40:53,359 --> 03:40:56,640 actually attaching a dynamic user 5105 03:40:56,640 --> 03:41:00,000 property to the request object called 5106 03:41:00,000 --> 03:41:02,080 user and then you can actually access 5107 03:41:02,080 --> 03:41:04,239 that user object by simply just 5108 03:41:04,239 --> 03:41:06,279 referencing it from the user or from the 5109 03:41:06,279 --> 03:41:08,560 from the request object and then you can 5110 03:41:08,560 --> 03:41:10,439 know who the actual user is that is 5111 03:41:10,439 --> 03:41:12,000 making the request I'll show you how 5112 03:41:12,000 --> 03:41:13,920 that works later on so that's all we 5113 03:41:13,920 --> 03:41:17,920 need to do inside our index. MJS file so 5114 03:41:17,920 --> 03:41:20,439 next thing I will do is I'm going to go 5115 03:41:20,439 --> 03:41:21,920 into my source 5116 03:41:21,920 --> 03:41:24,000 folder I'll create a new folder and I'll 5117 03:41:24,000 --> 03:41:26,199 call it strategies so this is where I 5118 03:41:26,199 --> 03:41:27,920 will have all of my strategies so right 5119 03:41:27,920 --> 03:41:31,319 now we currently will only have a local 5120 03:41:31,319 --> 03:41:33,239 strategy so I'll create a file called 5121 03:41:33,239 --> 03:41:35,640 local strategy. 5122 03:41:35,640 --> 03:41:39,560 MJS and what I'm going to do is import 5123 03:41:39,560 --> 03:41:42,040 passport 5124 03:41:42,960 --> 03:41:44,319 let me zoom in a little bit so import 5125 03:41:44,319 --> 03:41:46,439 passport from passport and then we want 5126 03:41:46,439 --> 03:41:49,359 to import this strategy class and it 5127 03:41:49,359 --> 03:41:52,560 comes from the passport local package 5128 03:41:52,560 --> 03:41:55,560 now all of the strategies will have this 5129 03:41:55,560 --> 03:41:57,159 strategy class so if you installed the 5130 03:41:57,159 --> 03:41:59,279 passport Facebook package it would have 5131 03:41:59,279 --> 03:42:01,159 its own strategy 5132 03:42:01,159 --> 03:42:04,120 class just wanted to mention that and 5133 03:42:04,120 --> 03:42:05,399 then what I'm going to do is I'm going 5134 03:42:05,399 --> 03:42:08,399 to call passport. 5135 03:42:08,399 --> 03:42:10,880 use and this is where we tell passport 5136 03:42:10,880 --> 03:42:14,319 to actually use our strategy okay so we 5137 03:42:14,319 --> 03:42:16,319 need to pass in an instance of this 5138 03:42:16,319 --> 03:42:18,960 local strategy right over here of this 5139 03:42:18,960 --> 03:42:22,359 strategy class so I just need to Simply 5140 03:42:22,359 --> 03:42:24,239 create a new instance so new strategy 5141 03:42:24,239 --> 03:42:27,439 like this now the strategy Constructor 5142 03:42:27,439 --> 03:42:29,800 can take in uh actually two arguments I 5143 03:42:29,800 --> 03:42:31,319 don't know if you saw right over here 5144 03:42:31,319 --> 03:42:33,560 you can pass in options or you can pass 5145 03:42:33,560 --> 03:42:36,000 in this verify function for now I'm 5146 03:42:36,000 --> 03:42:37,960 going to just ignore the options cuz you 5147 03:42:37,960 --> 03:42:40,120 won't really need this and later on on 5148 03:42:40,120 --> 03:42:41,479 in the video I'll explain when you'll 5149 03:42:41,479 --> 03:42:43,279 actually need this options but I'm just 5150 03:42:43,279 --> 03:42:46,120 going to specify this verify function so 5151 03:42:46,120 --> 03:42:47,239 that's just going to be this callback 5152 03:42:47,239 --> 03:42:48,920 function right over here and this 5153 03:42:48,920 --> 03:42:50,319 callback function takes in three 5154 03:42:50,319 --> 03:42:52,239 arguments it takes in the 5155 03:42:52,239 --> 03:42:55,560 username password and it takes in this 5156 03:42:55,560 --> 03:42:59,120 done function now since my application 5157 03:42:59,120 --> 03:43:01,159 takes in a username and a password this 5158 03:43:01,159 --> 03:43:03,520 works perfectly the way that these 5159 03:43:03,520 --> 03:43:05,279 arguments get passed to this callback 5160 03:43:05,279 --> 03:43:07,199 function is whenever you make a post 5161 03:43:07,199 --> 03:43:09,760 request to your endpoint that takes care 5162 03:43:09,760 --> 03:43:11,399 of of handling authentication the 5163 03:43:11,399 --> 03:43:13,560 passport will look for the username and 5164 03:43:13,560 --> 03:43:15,279 the password inside the request body 5165 03:43:15,279 --> 03:43:16,560 that was sent to that post request 5166 03:43:16,560 --> 03:43:18,319 endpoint and then they will it will pass 5167 03:43:18,319 --> 03:43:19,960 it in as arguments to this callback 5168 03:43:19,960 --> 03:43:22,399 function so this is assuming that we do 5169 03:43:22,399 --> 03:43:25,120 in fact use a username to authenticate 5170 03:43:25,120 --> 03:43:26,600 now sometimes you might not use a 5171 03:43:26,600 --> 03:43:27,960 username you might use something like an 5172 03:43:27,960 --> 03:43:30,040 email or maybe the username might be 5173 03:43:30,040 --> 03:43:31,680 named differently so that is where you 5174 03:43:31,680 --> 03:43:33,640 actually need to pass in this options 5175 03:43:33,640 --> 03:43:35,359 right over here because you need to tell 5176 03:43:35,359 --> 03:43:37,760 passport that we're actually not using a 5177 03:43:37,760 --> 03:43:39,880 username field but we're using let's 5178 03:43:39,880 --> 03:43:41,600 let's say the email address field so 5179 03:43:41,600 --> 03:43:42,880 what you would do is you would specify 5180 03:43:42,880 --> 03:43:45,239 this username field and then you'll tell 5181 03:43:45,239 --> 03:43:47,439 passport okay the actual username field 5182 03:43:47,439 --> 03:43:49,439 is the email field like that then 5183 03:43:49,439 --> 03:43:51,880 passport will look for the email field 5184 03:43:51,880 --> 03:43:54,080 in the request body and then send that 5185 03:43:54,080 --> 03:43:56,040 as the argument for username so I just 5186 03:43:56,040 --> 03:43:57,520 wanted to mention that I'm going to 5187 03:43:57,520 --> 03:43:59,399 leave this out for now and then once we 5188 03:43:59,399 --> 03:44:01,439 finish implementing this strategy I'll 5189 03:44:01,439 --> 03:44:03,600 show you how it actually works so what 5190 03:44:03,600 --> 03:44:05,920 we'll do is inside here this verify 5191 03:44:05,920 --> 03:44:07,359 function think of it like this this is 5192 03:44:07,359 --> 03:44:09,840 the function that is responsible for 5193 03:44:09,840 --> 03:44:11,560 doing a couple things but the main thing 5194 03:44:11,560 --> 03:44:14,159 is to validate the user so you need to 5195 03:44:14,159 --> 03:44:15,920 make sure that the user actually exists 5196 03:44:15,920 --> 03:44:17,319 and then you need to check to see if the 5197 03:44:17,319 --> 03:44:19,159 passwords are the same the password that 5198 03:44:19,159 --> 03:44:20,840 we sent to the server and then the 5199 03:44:20,840 --> 03:44:23,199 password that was attached to the user 5200 03:44:23,199 --> 03:44:25,399 object itself from the database or in 5201 03:44:25,399 --> 03:44:27,760 our case an array so those are the two 5202 03:44:27,760 --> 03:44:29,159 main checks of course there might be 5203 03:44:29,159 --> 03:44:30,760 additional checks that you would want to 5204 03:44:30,760 --> 03:44:32,720 perform but generally those are the two 5205 03:44:32,720 --> 03:44:35,159 major ones so again the first thing that 5206 03:44:35,159 --> 03:44:36,960 we need to do is search for the actual 5207 03:44:36,960 --> 03:44:39,439 user right now I don't have a database 5208 03:44:39,439 --> 03:44:41,800 set up set up but if for some reason you 5209 03:44:41,800 --> 03:44:43,960 might have one already configured then 5210 03:44:43,960 --> 03:44:46,199 what you need to do is just use whatever 5211 03:44:46,199 --> 03:44:49,000 database module you're using to to 5212 03:44:49,000 --> 03:44:50,960 interact with the database search for 5213 03:44:50,960 --> 03:44:54,239 the user by the username in this case 5214 03:44:54,239 --> 03:44:56,159 find the user and then once you find the 5215 03:44:56,159 --> 03:44:57,520 user check the 5216 03:44:57,520 --> 03:44:59,840 passwords so let's do that so I'll go 5217 03:44:59,840 --> 03:45:02,560 ahead and declare a variable called find 5218 03:45:02,560 --> 03:45:04,560 user and I'm going to import that mock 5219 03:45:04,560 --> 03:45:08,239 users array so this array just has a 5220 03:45:08,239 --> 03:45:09,439 bunch of users right over here with a 5221 03:45:09,439 --> 03:45:11,720 user Name ID display name and 5222 03:45:11,720 --> 03:45:13,960 password and I'm going to use the find 5223 03:45:13,960 --> 03:45:16,760 method to search for the user by the 5224 03:45:16,760 --> 03:45:20,479 username so user. username is equal to 5225 03:45:20,479 --> 03:45:23,359 username okay so this will search for 5226 03:45:23,359 --> 03:45:26,080 the user by the username if the user is 5227 03:45:26,080 --> 03:45:27,680 not 5228 03:45:27,680 --> 03:45:30,760 found then we actually want to throw an 5229 03:45:30,760 --> 03:45:33,279 error but before we throw any errors let 5230 03:45:33,279 --> 03:45:36,920 me actually wrap all this inside a TR 5231 03:45:36,920 --> 03:45:40,520 catch just like this so so that way now 5232 03:45:40,520 --> 03:45:43,800 when I throw my error right over here 5233 03:45:43,800 --> 03:45:46,439 and I'll say for the message user not 5234 03:45:46,439 --> 03:45:49,159 found that error will be caught inside 5235 03:45:49,159 --> 03:45:52,319 this catch right over here 5236 03:45:52,319 --> 03:45:56,800 okay now let me go ahead and do this if 5237 03:45:56,800 --> 03:45:58,479 the user is not found we'll throw an 5238 03:45:58,479 --> 03:46:01,000 error I'll handle the error case in just 5239 03:46:01,000 --> 03:46:03,439 a bit but I want to move on so let's say 5240 03:46:03,439 --> 03:46:04,800 if the user is found we want to check 5241 03:46:04,800 --> 03:46:05,520 the 5242 03:46:05,520 --> 03:46:09,680 password so if find user. password is 5243 03:46:09,680 --> 03:46:11,840 not equal to the password that was sent 5244 03:46:11,840 --> 03:46:12,880 to the 5245 03:46:12,880 --> 03:46:15,359 server then we're going to throw another 5246 03:46:15,359 --> 03:46:19,680 error we'll just say user or password 5247 03:46:19,680 --> 03:46:21,680 I'll say invalid 5248 03:46:21,680 --> 03:46:23,640 credentials 5249 03:46:23,640 --> 03:46:27,279 okay okay so now let's handle the errors 5250 03:46:27,279 --> 03:46:29,600 that that could be thrown so we're going 5251 03:46:29,600 --> 03:46:32,359 to catch those errors inside this catch 5252 03:46:32,359 --> 03:46:33,920 block right over 5253 03:46:33,920 --> 03:46:37,199 here and what we want to do is we want 5254 03:46:37,199 --> 03:46:40,199 to go ahead and call this done function 5255 03:46:40,199 --> 03:46:42,560 when we handle the error so notice how 5256 03:46:42,560 --> 03:46:43,920 this done function it takes in two 5257 03:46:43,920 --> 03:46:46,399 arguments it takes in an error and it 5258 03:46:46,399 --> 03:46:49,000 also takes in a user which could be a 5259 03:46:49,000 --> 03:46:51,720 falsy value so of course when an error 5260 03:46:51,720 --> 03:46:54,920 happens that means the user itself uh 5261 03:46:54,920 --> 03:46:58,640 was not validated correctly okay so that 5262 03:46:58,640 --> 03:47:00,359 could be either the user was not found 5263 03:47:00,359 --> 03:47:03,399 or the passwords did not match so in 5264 03:47:03,399 --> 03:47:04,800 this case we want to make sure we do 5265 03:47:04,800 --> 03:47:06,720 pass in that error object and for the 5266 03:47:06,720 --> 03:47:09,840 user we can just pass in null like that 5267 03:47:09,840 --> 03:47:11,520 and then this will just pretty much move 5268 03:47:11,520 --> 03:47:14,239 on from this verify function into the 5269 03:47:14,239 --> 03:47:16,600 next step which we will get into okay so 5270 03:47:16,600 --> 03:47:18,640 that's what we have to do for the catch 5271 03:47:18,640 --> 03:47:20,600 of course when we are done validating 5272 03:47:20,600 --> 03:47:24,520 the user if both of these uh cases are 5273 03:47:24,520 --> 03:47:27,439 false so if the user actually is found 5274 03:47:27,439 --> 03:47:30,439 and the passwords are actually correct 5275 03:47:30,439 --> 03:47:32,359 they do actually match then we want to 5276 03:47:32,359 --> 03:47:34,199 make sure we also call the done function 5277 03:47:34,199 --> 03:47:36,439 but this time there are no errors so 5278 03:47:36,439 --> 03:47:38,640 we'll pass in null and then for the user 5279 03:47:38,640 --> 03:47:40,760 we'll pass in the US user that we found 5280 03:47:40,760 --> 03:47:43,960 like this and that's it for the verify 5281 03:47:43,960 --> 03:47:47,640 function okay so like I said for the 5282 03:47:47,640 --> 03:47:50,080 database you would query the database 5283 03:47:50,080 --> 03:47:53,040 find the user check the password if the 5284 03:47:53,040 --> 03:47:54,960 user exists of course and then any 5285 03:47:54,960 --> 03:47:56,800 errors that are that could be thrown 5286 03:47:56,800 --> 03:47:58,960 would be caught inside this catch block 5287 03:47:58,960 --> 03:48:00,399 right over here hence why we're using 5288 03:48:00,399 --> 03:48:02,680 try and catch and then if everything is 5289 03:48:02,680 --> 03:48:04,920 good you'll call the done function 5290 03:48:04,920 --> 03:48:06,439 passing null for the error cuz there 5291 03:48:06,439 --> 03:48:08,319 were no errors and then passing that 5292 03:48:08,319 --> 03:48:10,520 user instance any errors would be caught 5293 03:48:10,520 --> 03:48:12,399 with this catch block and then you would 5294 03:48:12,399 --> 03:48:14,920 call done passing that error instance 5295 03:48:14,920 --> 03:48:17,080 and then uh passport would take care of 5296 03:48:17,080 --> 03:48:20,399 handling that error for us okay so the 5297 03:48:20,399 --> 03:48:22,720 next thing that we need to do is we need 5298 03:48:22,720 --> 03:48:26,359 to actually register our middleware our 5299 03:48:26,359 --> 03:48:27,640 passport middleware because right now 5300 03:48:27,640 --> 03:48:30,319 all this does is it validates the user 5301 03:48:30,319 --> 03:48:31,399 but there's still a whole bunch of 5302 03:48:31,399 --> 03:48:33,319 things that we have to do especially 5303 03:48:33,319 --> 03:48:34,399 since we are 5304 03:48:34,399 --> 03:48:38,279 using passport with Express session okay 5305 03:48:38,279 --> 03:48:41,239 so we need to import this uh local 5306 03:48:41,239 --> 03:48:44,479 strategy file into our index file but 5307 03:48:44,479 --> 03:48:46,000 first let's actually export this 5308 03:48:46,000 --> 03:48:48,199 password. use call so I'm going to do 5309 03:48:48,199 --> 03:48:51,560 export default password. use and I'll 5310 03:48:51,560 --> 03:48:53,800 import it right over here like this so 5311 03:48:53,800 --> 03:48:57,439 import and then the path to the file so 5312 03:48:57,439 --> 03:48:58,720 this will literally import this whole 5313 03:48:58,720 --> 03:49:02,239 thing right over here into our index 5314 03:49:02,239 --> 03:49:07,080 file okay and then now we need to set up 5315 03:49:07,080 --> 03:49:10,319 an endpoint that we can actually use as 5316 03:49:10,319 --> 03:49:12,040 our authentication endpoint where users 5317 03:49:12,040 --> 03:49:14,800 will make a request to or the HTT client 5318 03:49:14,800 --> 03:49:16,840 will make a request to it'll pass in the 5319 03:49:16,840 --> 03:49:19,319 user and password in the request body 5320 03:49:19,319 --> 03:49:21,840 and then that endpoint will take care of 5321 03:49:21,840 --> 03:49:24,760 invoking our passport middleware so 5322 03:49:24,760 --> 03:49:26,560 it'll actually invoke this verifi 5323 03:49:26,560 --> 03:49:29,680 function let me just quickly log out the 5324 03:49:29,680 --> 03:49:32,279 username and password let me just do 5325 03:49:32,279 --> 03:49:34,640 this 5326 03:49:34,840 --> 03:49:36,640 username and let me do the same thing 5327 03:49:36,640 --> 03:49:39,040 for password so that you all can 5328 03:49:39,040 --> 03:49:41,640 actually see what's happening underneath 5329 03:49:41,640 --> 03:49:46,239 the hood okay so let's go ahead and set 5330 03:49:46,239 --> 03:49:48,720 up our endpoint I'll just do this inside 5331 03:49:48,720 --> 03:49:52,159 the index. MJS file um just to keep it 5332 03:49:52,159 --> 03:49:54,199 everything simple if you've been 5333 03:49:54,199 --> 03:49:56,080 following along the entire series I 5334 03:49:56,080 --> 03:50:00,040 recommend you put everything in its own 5335 03:50:00,040 --> 03:50:02,920 router but let's go inside our index 5336 03:50:02,920 --> 03:50:04,399 file and let's set 5337 03:50:04,399 --> 03:50:07,399 up let's do this app. 5338 03:50:07,399 --> 03:50:11,319 poost so the path will be SL API 5339 03:50:11,319 --> 03:50:14,040 sloth and then we need to pass in 5340 03:50:14,040 --> 03:50:16,720 passport. authenticate like this and 5341 03:50:16,720 --> 03:50:18,680 we're going to call the function like 5342 03:50:18,680 --> 03:50:21,600 that and then we want to also pass in 5343 03:50:21,600 --> 03:50:23,680 our final request Handler so let's do 5344 03:50:23,680 --> 03:50:26,479 that request and 5345 03:50:26,600 --> 03:50:29,880 response and then uh let's go ahead 5346 03:50:29,880 --> 03:50:32,120 inside this authenticate function call 5347 03:50:32,120 --> 03:50:33,960 we need to specify what strategy we want 5348 03:50:33,960 --> 03:50:35,199 to use and it's literally just going to 5349 03:50:35,199 --> 03:50:37,920 be the name of our strategy in our case 5350 03:50:37,920 --> 03:50:40,520 the strategy for Loc local is just local 5351 03:50:40,520 --> 03:50:41,680 so we're just going to pass in local 5352 03:50:41,680 --> 03:50:44,840 like this okay that's literally all we 5353 03:50:44,840 --> 03:50:46,000 have to 5354 03:50:46,000 --> 03:50:48,680 do of course if you're using Discord 5355 03:50:48,680 --> 03:50:51,960 strategy you would pass in 5356 03:50:51,960 --> 03:50:54,239 Discord for GitHub you would pass in 5357 03:50:54,239 --> 03:50:56,159 GitHub for Google you would pass on 5358 03:50:56,159 --> 03:50:58,520 Google like that and vice versa so 5359 03:50:58,520 --> 03:51:02,800 hopefully that makes sense okay so now 5360 03:51:02,800 --> 03:51:03,960 what I'm going to do is I'm going to go 5361 03:51:03,960 --> 03:51:07,560 ahead and make a request to uh SL API o 5362 03:51:07,560 --> 03:51:09,520 making a post request to it and you'll 5363 03:51:09,520 --> 03:51:10,720 see how it's going to go ahead and 5364 03:51:10,720 --> 03:51:12,359 invoke this passport. authenticate 5365 03:51:12,359 --> 03:51:14,439 middleware function and then it will 5366 03:51:14,439 --> 03:51:17,279 call this verify function so let's do 5367 03:51:17,279 --> 03:51:18,840 that let me go to Thunder 5368 03:51:18,840 --> 03:51:23,520 client Let's do let's do this okay so 5369 03:51:23,520 --> 03:51:26,640 post request to our API off endpoint 5370 03:51:26,640 --> 03:51:30,560 that we just set up click Send okay so 5371 03:51:30,560 --> 03:51:32,399 ignore this error for now I'll address 5372 03:51:32,399 --> 03:51:34,319 that in just a bit but let's take a look 5373 03:51:34,319 --> 03:51:36,399 at the console and let's see what's 5374 03:51:36,399 --> 03:51:38,120 going on you can see right over here 5375 03:51:38,120 --> 03:51:40,760 username and password are being logged 5376 03:51:40,760 --> 03:51:42,680 so that verifies that we are actually 5377 03:51:42,680 --> 03:51:44,800 inside this callback function so 5378 03:51:44,800 --> 03:51:46,920 passport actually called this callback 5379 03:51:46,920 --> 03:51:49,000 function right over here and then all of 5380 03:51:49,000 --> 03:51:52,880 this logic is being performed okay so 5381 03:51:52,880 --> 03:51:53,880 it's going to go ahead and search for 5382 03:51:53,880 --> 03:51:55,920 the user if the user not found it'll 5383 03:51:55,920 --> 03:51:57,920 throw a user not found error so let me 5384 03:51:57,920 --> 03:51:59,560 type in a username that doesn't exist in 5385 03:51:59,560 --> 03:52:00,760 our 5386 03:52:00,760 --> 03:52:04,279 array so when I pass in this username an 5387 03:52:04,279 --> 03:52:06,520 nansen you see how it says user not 5388 03:52:06,520 --> 03:52:08,159 found that's the error that I manually 5389 03:52:08,159 --> 03:52:10,640 throw the that L thrown over 5390 03:52:10,640 --> 03:52:12,840 here and then it gets caught right over 5391 03:52:12,840 --> 03:52:15,600 here and it calls the done function okay 5392 03:52:15,600 --> 03:52:17,399 notice how if I actually don't even pass 5393 03:52:17,399 --> 03:52:19,720 in if I pass in a null value for error 5394 03:52:19,720 --> 03:52:21,600 we actually won't even get an error this 5395 03:52:21,600 --> 03:52:23,040 is a completely different error I'm not 5396 03:52:23,040 --> 03:52:26,680 sure why it says 41 5397 03:52:26,680 --> 03:52:29,960 unauthorized but um yeah see it doesn't 5398 03:52:29,960 --> 03:52:31,760 throw that error but it gives us that 5399 03:52:31,760 --> 03:52:34,920 instead okay but of 5400 03:52:34,920 --> 03:52:38,760 course let me pass the error back click 5401 03:52:38,760 --> 03:52:40,800 Send and pass in invalid username it 5402 03:52:40,800 --> 03:52:42,600 will say user not found let's say if the 5403 03:52:42,600 --> 03:52:44,439 user is found but the credentials are 5404 03:52:44,439 --> 03:52:46,640 invalid and so I'm going to pass in the 5405 03:52:46,640 --> 03:52:48,600 wrong password it'll say invalid 5406 03:52:48,600 --> 03:52:50,520 credentials so before I move on let me 5407 03:52:50,520 --> 03:52:52,800 just show you real quick about the 5408 03:52:52,800 --> 03:52:55,040 username field that I mentioned earlier 5409 03:52:55,040 --> 03:52:56,520 so let's say for example you are 5410 03:52:56,520 --> 03:52:58,880 expecting the user to provide the email 5411 03:52:58,880 --> 03:53:00,520 address as their username so let's say 5412 03:53:00,520 --> 03:53:02,600 the we have email let's just do an@ 5413 03:53:02,600 --> 03:53:05,279 gmail.com as an example so watch this 5414 03:53:05,279 --> 03:53:07,319 what we need to do is we need to pass in 5415 03:53:07,319 --> 03:53:08,760 that options right over here and then we 5416 03:53:08,760 --> 03:53:10,399 need to pass in 5417 03:53:10,399 --> 03:53:14,600 this username field and just specify the 5418 03:53:14,600 --> 03:53:16,800 field that's going to be our username 5419 03:53:16,800 --> 03:53:18,080 which in this case it's going to be 5420 03:53:18,080 --> 03:53:20,359 email and if I click 5421 03:53:20,359 --> 03:53:22,720 Send you'll see now I can actually use 5422 03:53:22,720 --> 03:53:25,760 the email field as my username and if 5423 03:53:25,760 --> 03:53:27,680 you look at the logs let's show you 5424 03:53:27,680 --> 03:53:30,520 right over here okay right over here 5425 03:53:30,520 --> 03:53:33,479 username an@ gmail.com okay you can of 5426 03:53:33,479 --> 03:53:35,359 course change in you can change up the 5427 03:53:35,359 --> 03:53:38,040 arguments change the email like that if 5428 03:53:38,040 --> 03:53:39,600 you want to 5429 03:53:39,600 --> 03:53:41,359 so hopefully that makes sense and that 5430 03:53:41,359 --> 03:53:42,840 shows you how you can use the username 5431 03:53:42,840 --> 03:53:44,680 field if let's say the username field 5432 03:53:44,680 --> 03:53:46,199 was something else let's say if it was 5433 03:53:46,199 --> 03:53:48,560 something like user uncore name for 5434 03:53:48,560 --> 03:53:50,159 whatever reason then you can just 5435 03:53:50,159 --> 03:53:53,000 specify that okay and of course if I try 5436 03:53:53,000 --> 03:53:55,399 to send this this one no longer work if 5437 03:53:55,399 --> 03:53:58,000 I try to use username like this that 5438 03:53:58,000 --> 03:53:59,960 will not work I have to explicitly say 5439 03:53:59,960 --> 03:54:03,199 user uncore name so that way passport 5440 03:54:03,199 --> 03:54:06,239 knows which field to look for so I hope 5441 03:54:06,239 --> 03:54:08,319 that part makes sense okay let me just 5442 03:54:08,319 --> 03:54:09,720 remove this part 5443 03:54:09,720 --> 03:54:14,000 part okay so now the other thing that I 5444 03:54:14,000 --> 03:54:16,080 wanted to talk about was this error that 5445 03:54:16,080 --> 03:54:17,720 I just that I just 5446 03:54:17,720 --> 03:54:20,040 encountered so you see how it says fail 5447 03:54:20,040 --> 03:54:21,960 to serialize user intercession so here's 5448 03:54:21,960 --> 03:54:24,239 the thing we actually successfully were 5449 03:54:24,239 --> 03:54:26,359 able to 5450 03:54:26,359 --> 03:54:28,359 authenticate by checking all these 5451 03:54:28,359 --> 03:54:29,760 credentials and verifying that it was 5452 03:54:29,760 --> 03:54:32,359 correct but because passport needs to 5453 03:54:32,359 --> 03:54:33,880 actually serialize the user into the 5454 03:54:33,880 --> 03:54:36,040 session we need to actually Implement 5455 03:54:36,040 --> 03:54:38,560 two functions okay because right now we 5456 03:54:38,560 --> 03:54:39,520 don't have have those functions 5457 03:54:39,520 --> 03:54:40,880 implemented that is why we are getting 5458 03:54:40,880 --> 03:54:44,479 this error so what we need to do is 5459 03:54:44,479 --> 03:54:46,840 inside our strategy file I'm going to go 5460 03:54:46,840 --> 03:54:48,840 ahead and reference passport and I'm 5461 03:54:48,840 --> 03:54:51,399 going to go ahead and look for this 5462 03:54:51,399 --> 03:54:53,120 serialized user function so I'm going to 5463 03:54:53,120 --> 03:54:56,239 call that and this function takes in an 5464 03:54:56,239 --> 03:54:58,239 argument which is a callback function 5465 03:54:58,239 --> 03:55:00,560 like this and this callback function 5466 03:55:00,560 --> 03:55:02,920 takes in two arguments this first 5467 03:55:02,920 --> 03:55:05,120 argument is going to be the actual 5468 03:55:05,120 --> 03:55:07,760 whoops the actual user that we are 5469 03:55:07,760 --> 03:55:10,680 trying to see iiz okay so first of all 5470 03:55:10,680 --> 03:55:13,279 what exactly does this function do well 5471 03:55:13,279 --> 03:55:15,640 this function is responsible for 5472 03:55:15,640 --> 03:55:18,040 actually taking that user object that we 5473 03:55:18,040 --> 03:55:21,120 just validated and then storing it in 5474 03:55:21,120 --> 03:55:23,239 the session okay and that's the reason 5475 03:55:23,239 --> 03:55:24,680 why I mentioned earlier that passport 5476 03:55:24,680 --> 03:55:27,000 integrates very well with Express 5477 03:55:27,000 --> 03:55:29,239 session so it takes that user that we 5478 03:55:29,239 --> 03:55:32,520 just found from this verify function and 5479 03:55:32,520 --> 03:55:35,080 it'll take care of storing it in the 5480 03:55:35,080 --> 03:55:38,439 session data okay the second argument is 5481 03:55:38,439 --> 03:55:40,920 going to be the this done function so 5482 03:55:40,920 --> 03:55:42,359 inside this serialized user function you 5483 03:55:42,359 --> 03:55:44,319 don't really need to do anything but 5484 03:55:44,319 --> 03:55:46,800 just call this done function and it 5485 03:55:46,800 --> 03:55:48,359 takes in two arguments it takes in an 5486 03:55:48,359 --> 03:55:49,680 error in this case we're not really 5487 03:55:49,680 --> 03:55:51,560 doing anything so we just pass a null 5488 03:55:51,560 --> 03:55:53,920 for the error and you'll notice that 5489 03:55:53,920 --> 03:55:57,720 right over here the suggested uh name 5490 03:55:57,720 --> 03:55:59,920 for the field it says ID you can 5491 03:55:59,920 --> 03:56:02,000 actually pass in whatever you want for 5492 03:56:02,000 --> 03:56:03,880 this second argument that relates to the 5493 03:56:03,880 --> 03:56:06,239 user so for example I can just pass in 5494 03:56:06,239 --> 03:56:09,680 user like this okay now depending on 5495 03:56:09,680 --> 03:56:13,640 what you pass in to this second argument 5496 03:56:13,640 --> 03:56:15,880 actually ends up being the argument that 5497 03:56:15,880 --> 03:56:18,760 will be passed into a call function for 5498 03:56:18,760 --> 03:56:21,159 this the serialized user function don't 5499 03:56:21,159 --> 03:56:22,560 worry about that right now I'm going to 5500 03:56:22,560 --> 03:56:25,080 implement it in just a second but what 5501 03:56:25,080 --> 03:56:26,399 what I want to do instead of passing 5502 03:56:26,399 --> 03:56:28,000 just the user object I want to pass in 5503 03:56:28,000 --> 03:56:30,800 the ID of the user like this you want to 5504 03:56:30,800 --> 03:56:33,239 pass in something that is unique okay so 5505 03:56:33,239 --> 03:56:35,600 either the ID or a username something 5506 03:56:35,600 --> 03:56:37,560 that is unique that can be used to 5507 03:56:37,560 --> 03:56:39,199 search for the user 5508 03:56:39,199 --> 03:56:42,000 either in our array or a 5509 03:56:42,000 --> 03:56:45,800 database okay so now let's move on to 5510 03:56:45,800 --> 03:56:47,840 the next function that we need to 5511 03:56:47,840 --> 03:56:50,520 implement so deserialized 5512 03:56:50,520 --> 03:56:53,560 user is the next function and this also 5513 03:56:53,560 --> 03:56:56,040 takes in a callback 5514 03:56:56,040 --> 03:56:58,279 function and this C function takes in 5515 03:56:58,279 --> 03:57:00,960 two arguments it takes in whatever it 5516 03:57:00,960 --> 03:57:03,199 was that you passed in to the done 5517 03:57:03,199 --> 03:57:05,600 function for serialized user so notice 5518 03:57:05,600 --> 03:57:09,080 how I pass in the ID so this actually 5519 03:57:09,080 --> 03:57:11,800 gets passed into this callback function 5520 03:57:11,800 --> 03:57:14,840 for deserialized user okay and let me 5521 03:57:14,840 --> 03:57:17,080 actually show you this stack Overflow 5522 03:57:17,080 --> 03:57:19,040 post because I think this post is very 5523 03:57:19,040 --> 03:57:20,560 helpful for a lot of people so I want to 5524 03:57:20,560 --> 03:57:24,319 show it to you all so right over here 5525 03:57:24,319 --> 03:57:25,279 they kind of give you like an 5526 03:57:25,279 --> 03:57:27,880 explanation to how all this stuff works 5527 03:57:27,880 --> 03:57:29,520 but I want you to pay attention over 5528 03:57:29,520 --> 03:57:31,279 here they have they were able to provide 5529 03:57:31,279 --> 03:57:33,080 the user was able to provide a visual 5530 03:57:33,080 --> 03:57:35,640 flow so you can see over here we have 5531 03:57:35,640 --> 03:57:37,800 both our serialized and deserialized 5532 03:57:37,800 --> 03:57:40,000 user functions so whatever they pass 5533 03:57:40,000 --> 03:57:42,880 into that done function for serialized 5534 03:57:42,880 --> 03:57:46,920 user that pretty much gets saved to the 5535 03:57:46,920 --> 03:57:49,080 session okay and I'll show you later how 5536 03:57:49,080 --> 03:57:52,520 that looks like um in as an object but 5537 03:57:52,520 --> 03:57:54,920 it gets saved and it gets passed right 5538 03:57:54,920 --> 03:57:55,880 over 5539 03:57:55,880 --> 03:57:58,199 here okay well it doesn't really get 5540 03:57:58,199 --> 03:58:00,359 passed per se it's just it's just that 5541 03:58:00,359 --> 03:58:04,000 we the deserialized user will use the ID 5542 03:58:04,000 --> 03:58:06,439 to actually search for the user inside 5543 03:58:06,439 --> 03:58:07,399 that function that we're going to 5544 03:58:07,399 --> 03:58:10,159 implement okay if we were to pass in the 5545 03:58:10,159 --> 03:58:12,640 username then it would look up the 5546 03:58:12,640 --> 03:58:14,479 username in the session data and it 5547 03:58:14,479 --> 03:58:16,239 would use that to search for the user in 5548 03:58:16,239 --> 03:58:18,120 the database okay but I just wanted to 5549 03:58:18,120 --> 03:58:19,399 show you this very quickly because I 5550 03:58:19,399 --> 03:58:21,560 think this is extremely helpful for a 5551 03:58:21,560 --> 03:58:22,359 lot of 5552 03:58:22,359 --> 03:58:25,000 people instead of just not mentioning it 5553 03:58:25,000 --> 03:58:27,479 at all so the second argument for the 5554 03:58:27,479 --> 03:58:29,359 call back function for deserialized user 5555 03:58:29,359 --> 03:58:31,399 will be the done function as well but 5556 03:58:31,399 --> 03:58:34,319 this time inside der serialized user we 5557 03:58:34,319 --> 03:58:37,000 actually need to search for the user 5558 03:58:37,000 --> 03:58:40,239 itself now again serialized user is to 5559 03:58:40,239 --> 03:58:43,359 tell passport how to serialize that user 5560 03:58:43,359 --> 03:58:45,680 data into the session so in other words 5561 03:58:45,680 --> 03:58:48,960 it's going to store that user ID into 5562 03:58:48,960 --> 03:58:51,520 the session data okay and that's really 5563 03:58:51,520 --> 03:58:55,239 all you need deserialized user is how we 5564 03:58:55,239 --> 03:58:59,520 can take that ID and kind of like unpack 5565 03:58:59,520 --> 03:59:02,199 reveal who the actual user is and then 5566 03:59:02,199 --> 03:59:04,040 what happens is it takes that user 5567 03:59:04,040 --> 03:59:06,640 object that we were able to retrieve via 5568 03:59:06,640 --> 03:59:09,720 the ID and it stores that user object 5569 03:59:09,720 --> 03:59:13,520 into the request object itself okay so 5570 03:59:13,520 --> 03:59:15,080 later on you'll see how we can reference 5571 03:59:15,080 --> 03:59:18,439 request. user when we make requests but 5572 03:59:18,439 --> 03:59:20,159 what we need to do inside der serialized 5573 03:59:20,159 --> 03:59:22,040 user is search for the user either in 5574 03:59:22,040 --> 03:59:25,279 our database or in our array so what 5575 03:59:25,279 --> 03:59:28,920 I'll do is let me use a TR 5576 03:59:29,319 --> 03:59:31,720 catch and first I'll search for the user 5577 03:59:31,720 --> 03:59:33,120 so cons find 5578 03:59:33,120 --> 03:59:35,479 user equals mock users and I'm going to 5579 03:59:35,479 --> 03:59:37,439 search by the ID this 5580 03:59:37,439 --> 03:59:42,000 time so user ID is equal to 5581 03:59:42,000 --> 03:59:45,319 ID if the user is not found I'll throw 5582 03:59:45,319 --> 03:59:47,439 an 5583 03:59:48,199 --> 03:59:50,760 error and we'll catch the error inside 5584 03:59:50,760 --> 03:59:52,920 this catch block right over here and 5585 03:59:52,920 --> 03:59:54,279 then we'll call the done function and 5586 03:59:54,279 --> 03:59:57,000 we'll pass in an error we'll pass in the 5587 03:59:57,000 --> 03:59:59,199 error that we caught and then we'll pass 5588 03:59:59,199 --> 04:00:01,080 in null for the user because the user 5589 04:00:01,080 --> 04:00:03,120 was not found and then if the user is 5590 04:00:03,120 --> 04:00:06,120 found we will call done passing null for 5591 04:00:06,120 --> 04:00:08,560 the error and then passing the user 5592 04:00:08,560 --> 04:00:12,199 instance like this okay so again we're 5593 04:00:12,199 --> 04:00:14,840 searching for the user if it's found 5594 04:00:14,840 --> 04:00:16,920 then we will call done and then password 5595 04:00:16,920 --> 04:00:19,080 will take care of taking this user 5596 04:00:19,080 --> 04:00:20,800 object and attaching it to the request 5597 04:00:20,800 --> 04:00:23,000 object itself if the user is not found 5598 04:00:23,000 --> 04:00:24,880 then it will just call the done function 5599 04:00:24,880 --> 04:00:26,840 inside this catch block with the error 5600 04:00:26,840 --> 04:00:28,199 and then password will handle the error 5601 04:00:28,199 --> 04:00:31,840 for us okay so now let me go ahead and 5602 04:00:31,840 --> 04:00:35,040 write some logs real 5603 04:00:37,319 --> 04:00:39,920 quick because I I really want to show 5604 04:00:39,920 --> 04:00:40,960 you 5605 04:00:40,960 --> 04:00:43,040 all how this all works and I want to 5606 04:00:43,040 --> 04:00:44,920 show you the order of 5607 04:00:44,920 --> 04:00:47,560 operations okay because it's very 5608 04:00:47,560 --> 04:00:48,760 important it's it's one thing to 5609 04:00:48,760 --> 04:00:51,359 actually write the 5610 04:00:51,680 --> 04:00:54,120 code but it's another thing to actually 5611 04:00:54,120 --> 04:00:55,560 understand how this all works CU When I 5612 04:00:55,560 --> 04:00:57,760 first was trying to understand all this 5613 04:00:57,760 --> 04:00:58,920 I never I didn't know anything about 5614 04:00:58,920 --> 04:01:01,120 this at all until I really dug deep into 5615 04:01:01,120 --> 04:01:03,399 researching how all this stuff 5616 04:01:03,399 --> 04:01:07,239 works okay so let me write some 5617 04:01:07,239 --> 04:01:09,359 logs 5618 04:01:09,359 --> 04:01:13,000 okay um okay so let's go ahead and do 5619 04:01:13,000 --> 04:01:17,239 this let's make a post request to SL API 5620 04:01:17,239 --> 04:01:19,600 off so notice how right now it's it's 5621 04:01:19,600 --> 04:01:21,080 still processing the request it's not 5622 04:01:21,080 --> 04:01:22,319 going to send a response back don't 5623 04:01:22,319 --> 04:01:24,319 worry about that yet but let me just 5624 04:01:24,319 --> 04:01:26,239 show you what happens right over here 5625 04:01:26,239 --> 04:01:29,319 let me going me go down over 5626 04:01:29,319 --> 04:01:31,720 here restart oops let me just restart my 5627 04:01:31,720 --> 04:01:33,760 server you kind of saw the logs up there 5628 04:01:33,760 --> 04:01:36,239 but let me just resend the request okay 5629 04:01:36,239 --> 04:01:38,840 so you can see right over here uh first 5630 04:01:38,840 --> 04:01:40,279 we're inside the verify function because 5631 04:01:40,279 --> 04:01:41,880 that's where we are logging the username 5632 04:01:41,880 --> 04:01:44,279 and password and then after everything 5633 04:01:44,279 --> 04:01:46,600 is successful okay cuz we did pass in 5634 04:01:46,600 --> 04:01:49,159 the correct credentials you can see how 5635 04:01:49,159 --> 04:01:51,920 we're inside serialized user and it logs 5636 04:01:51,920 --> 04:01:55,520 that user object right over here okay so 5637 04:01:55,520 --> 04:01:57,520 then you'll notice that nothing else 5638 04:01:57,520 --> 04:01:59,319 happens don't worry the reason why 5639 04:01:59,319 --> 04:02:00,720 nothing happens currently is because 5640 04:02:00,720 --> 04:02:02,560 after we call serialized 5641 04:02:02,560 --> 04:02:04,880 user what happens is it's going to go 5642 04:02:04,880 --> 04:02:08,239 ahead and call this next middleware fun 5643 04:02:08,239 --> 04:02:09,319 function which is going to be our 5644 04:02:09,319 --> 04:02:11,800 request Handler function so we aren't 5645 04:02:11,800 --> 04:02:13,159 doing anything inside here so why don't 5646 04:02:13,159 --> 04:02:16,479 we actually just send a response 5647 04:02:16,479 --> 04:02:19,439 back okay and let's try this again okay 5648 04:02:19,439 --> 04:02:23,120 so we're good and everything is good 5649 04:02:23,120 --> 04:02:25,239 whoops let me do send status sorry about 5650 04:02:25,239 --> 04:02:29,040 that send status okay okay so everything 5651 04:02:29,040 --> 04:02:29,840 is 5652 04:02:29,840 --> 04:02:33,479 good uh what's going on here user is not 5653 04:02:33,479 --> 04:02:35,399 defined 5654 04:02:35,399 --> 04:02:37,640 uh seems like we got okay don't worry 5655 04:02:37,640 --> 04:02:38,880 about that 5656 04:02:38,880 --> 04:02:42,080 I will have to address that oh whoops 5657 04:02:42,080 --> 04:02:44,640 I'm sorry let me fix this real quick I 5658 04:02:44,640 --> 04:02:46,680 passed in user instead of fine user let 5659 04:02:46,680 --> 04:02:48,960 me fix that I apologize hopefully you 5660 04:02:48,960 --> 04:02:50,520 all caught the error yourself very 5661 04:02:50,520 --> 04:02:54,199 quickly okay so let me do this again so 5662 04:02:54,199 --> 04:02:57,120 first I'm I'm going to click Send good 5663 04:02:57,120 --> 04:02:59,640 and that error just happened earlier 5664 04:02:59,640 --> 04:03:01,840 because when I made a second request it 5665 04:03:01,840 --> 04:03:03,720 actually tried to call the deserialized 5666 04:03:03,720 --> 04:03:06,040 user function so I'll explain that so 5667 04:03:06,040 --> 04:03:07,720 the first thing that happens is when we 5668 04:03:07,720 --> 04:03:10,720 first log in we call serialized user 5669 04:03:10,720 --> 04:03:12,199 once that's all we need to do is call it 5670 04:03:12,199 --> 04:03:14,479 once this function this call function 5671 04:03:14,479 --> 04:03:16,840 needs to be called when we log in and 5672 04:03:16,840 --> 04:03:18,239 we're only going to log in one time 5673 04:03:18,239 --> 04:03:20,600 obviously until we log out and then we 5674 04:03:20,600 --> 04:03:24,239 log back in okay once we have logged in 5675 04:03:24,239 --> 04:03:26,720 any request that we make later on what 5676 04:03:26,720 --> 04:03:29,279 happens is passport will then call the 5677 04:03:29,279 --> 04:03:32,279 Callback function for deserialized user 5678 04:03:32,279 --> 04:03:33,680 so this callback function is going to 5679 04:03:33,680 --> 04:03:36,279 get called if I make another request 5680 04:03:36,279 --> 04:03:37,840 okay so let's see what happens I'm going 5681 04:03:37,840 --> 04:03:41,120 to click click Send see how now uh let's 5682 04:03:41,120 --> 04:03:44,040 see okay yeah right over here see how 5683 04:03:44,040 --> 04:03:44,880 right 5684 04:03:44,880 --> 04:03:48,560 now it's going to go ahead and call 5685 04:03:48,560 --> 04:03:52,080 deserialize user inside deserializer and 5686 04:03:52,080 --> 04:03:55,520 it says deserializing user ID of one 5687 04:03:55,520 --> 04:03:58,920 okay and uh again we're it's calling uh 5688 04:03:58,920 --> 04:04:00,359 serialized user because we're actually 5689 04:04:00,359 --> 04:04:03,120 trying to call the login endpoint but 5690 04:04:03,120 --> 04:04:04,520 you'll notice that if I try to call 5691 04:04:04,520 --> 04:04:06,239 another endpoint it's not going to call 5692 04:04:06,239 --> 04:04:09,000 serialized user okay but you can see the 5693 04:04:09,000 --> 04:04:11,720 order of how things are working let me 5694 04:04:11,720 --> 04:04:13,319 go ahead and Implement another endpoint 5695 04:04:13,319 --> 04:04:16,479 let me do this let me Implement app.get 5696 04:04:16,479 --> 04:04:19,319 API let's Implement a status endpoint 5697 04:04:19,319 --> 04:04:22,120 and this endpoint is responsible for 5698 04:04:22,120 --> 04:04:23,479 checking to see if the user is 5699 04:04:23,479 --> 04:04:26,560 authenticated or not okay so remember 5700 04:04:26,560 --> 04:04:28,600 how earlier I said that the user object 5701 04:04:28,600 --> 04:04:31,040 will be attached to the request object 5702 04:04:31,040 --> 04:04:34,279 so if I were to console log request. 5703 04:04:34,279 --> 04:04:39,960 user and let me just go ahead and write 5704 04:04:40,720 --> 04:04:43,000 inside because we have a bunch of logs 5705 04:04:43,000 --> 04:04:45,399 everywhere so I want to label 5706 04:04:45,399 --> 04:04:49,640 everything and then what I'll do is this 5707 04:04:49,640 --> 04:04:51,680 so I'm not going to send the response 5708 04:04:51,680 --> 04:04:54,359 back I'll just log this right now okay 5709 04:04:54,359 --> 04:04:57,239 so let's redo everything let's log 5710 04:04:57,239 --> 04:04:59,319 in so let's go ahead and call this 5711 04:04:59,319 --> 04:05:01,800 endpoint so it goes ahead and we log in 5712 04:05:01,800 --> 04:05:04,000 it calls serialize user and then inside 5713 04:05:04,000 --> 04:05:05,720 serialize user we log the user that's 5714 04:05:05,720 --> 04:05:08,080 fine now let's make a get request so 5715 04:05:08,080 --> 04:05:11,120 let's let me hit new 5716 04:05:11,120 --> 04:05:13,000 request okay I'm going to make a get 5717 04:05:13,000 --> 04:05:17,159 request to that new endpoint sl/ 5718 04:05:17,840 --> 04:05:20,640 status and now inside this status 5719 04:05:20,640 --> 04:05:22,920 endpoint uh we want to make sure that we 5720 04:05:22,920 --> 04:05:25,800 are only sending a response back the 5721 04:05:25,800 --> 04:05:28,640 user response back if the user object 5722 04:05:28,640 --> 04:05:30,640 actually exists on the request object 5723 04:05:30,640 --> 04:05:32,120 and that's a good indicator to let you 5724 04:05:32,120 --> 04:05:35,359 know if the user is authenticated or not 5725 04:05:35,359 --> 04:05:38,479 so if request. user 5726 04:05:38,479 --> 04:05:42,279 I'll return a response with the user 5727 04:05:42,279 --> 04:05:44,680 object U and of course if the user is 5728 04:05:44,680 --> 04:05:47,800 not defined if this property is not 5729 04:05:47,800 --> 04:05:50,920 defined and it's there's no value then 5730 04:05:50,920 --> 04:05:53,080 we'll just simply return 5731 04:05:53,080 --> 04:05:56,040 response uh let's do send 5732 04:05:56,040 --> 04:05:59,000 status 401 and let me actually just 5733 04:05:59,000 --> 04:06:00,680 quickly use I'm going to use a turn 5734 04:06:00,680 --> 04:06:03,399 operator instead return request. 5735 04:06:03,399 --> 04:06:05,960 user so let me just copy 5736 04:06:05,960 --> 04:06:10,479 that and then response then status 41 so 5737 04:06:10,479 --> 04:06:13,000 now I can remove this okay hopefully 5738 04:06:13,000 --> 04:06:15,279 that makes sense uh oh whoops sorry 5739 04:06:15,279 --> 04:06:18,199 about that okay should have been colon 5740 04:06:18,199 --> 04:06:20,279 so this is a turning operator over here 5741 04:06:20,279 --> 04:06:21,520 all right so let's go ahead and do this 5742 04:06:21,520 --> 04:06:23,359 let's log in right now so I'm going to 5743 04:06:23,359 --> 04:06:26,600 make a post request to my authentication 5744 04:06:26,600 --> 04:06:29,279 endpoint click Send okay we're good now 5745 04:06:29,279 --> 04:06:30,720 let's go ahead and make a get request to 5746 04:06:30,720 --> 04:06:33,239 the status 5747 04:06:35,399 --> 04:06:39,239 endpoint uh let's see user is not the 5748 04:06:39,239 --> 04:06:43,399 find I might have an error somewhere 5749 04:06:43,399 --> 04:06:44,640 else 5750 04:06:44,640 --> 04:06:47,760 [Music] 5751 04:06:50,760 --> 04:06:54,080 um what's going on over 5752 04:06:54,080 --> 04:06:56,680 here oh whoops I forgot it's not User 5753 04:06:56,680 --> 04:06:58,399 it's request. 5754 04:06:58,399 --> 04:07:02,319 user okay um let's re log in so you'll 5755 04:07:02,319 --> 04:07:05,279 see that when I log in we are inside 5756 04:07:05,279 --> 04:07:06,840 serialized user so that gets called 5757 04:07:06,840 --> 04:07:08,479 that's fine so now we don't need to log 5758 04:07:08,479 --> 04:07:10,000 in anymore because we've already logged 5759 04:07:10,000 --> 04:07:13,279 in one time so we should be logged in so 5760 04:07:13,279 --> 04:07:14,960 if I click this right over here you can 5761 04:07:14,960 --> 04:07:19,960 see that I can access my user reference 5762 04:07:19,960 --> 04:07:22,159 because I'm logged in as ansen and 5763 04:07:22,159 --> 04:07:24,560 notice how when I make a request to this 5764 04:07:24,560 --> 04:07:27,319 endpoint it's going to go ahead and call 5765 04:07:27,319 --> 04:07:30,279 the deserialized user function you can 5766 04:07:30,279 --> 04:07:32,399 see the logs are right over here and 5767 04:07:32,399 --> 04:07:34,720 again what happens is the deserialized 5768 04:07:34,720 --> 04:07:39,080 user function will use that ID 5769 04:07:39,080 --> 04:07:41,520 from the session store okay CU that's 5770 04:07:41,520 --> 04:07:42,960 where the data is really being saved 5771 04:07:42,960 --> 04:07:44,880 inside the session store and also the 5772 04:07:44,880 --> 04:07:47,479 session object itself and then it will 5773 04:07:47,479 --> 04:07:49,760 use that to search for the user in this 5774 04:07:49,760 --> 04:07:52,040 case we're only saving the ID let me 5775 04:07:52,040 --> 04:07:54,399 also show you one more thing that was 5776 04:07:54,399 --> 04:07:55,960 also mentioned in that stack Overflow 5777 04:07:55,960 --> 04:08:02,199 post as well let me console log rec. 5778 04:08:02,239 --> 04:08:05,880 session and let me just do 5779 04:08:05,880 --> 04:08:08,479 this oh whoops it's 5780 04:08:08,479 --> 04:08:11,159 whoops sorry about that it's request. 5781 04:08:11,159 --> 04:08:14,760 session yep 5782 04:08:14,760 --> 04:08:18,319 okay all right so let me do this 5783 04:08:18,319 --> 04:08:22,119 again and let me go down 5784 04:08:22,119 --> 04:08:25,040 here okay you notice how right over here 5785 04:08:25,040 --> 04:08:26,159 and the reason why I'm showing this 5786 04:08:26,159 --> 04:08:28,159 because again I want you to understand 5787 04:08:28,159 --> 04:08:30,359 how all this works notice how now the 5788 04:08:30,359 --> 04:08:33,439 session object has this passport 5789 04:08:33,439 --> 04:08:36,080 property and if you saw the part where 5790 04:08:36,080 --> 04:08:38,640 we talked about Express sessions you'll 5791 04:08:38,640 --> 04:08:40,880 remember that as soon as you modify the 5792 04:08:40,880 --> 04:08:42,479 session object it sets the cookie and 5793 04:08:42,479 --> 04:08:45,040 sends it to the front end or the client 5794 04:08:45,040 --> 04:08:47,720 so if you see right over here if I look 5795 04:08:47,720 --> 04:08:50,680 at cookies I actually have this cookie 5796 04:08:50,680 --> 04:08:52,880 right over here and this is my session 5797 04:08:52,880 --> 04:08:57,119 ID on the server 5798 04:08:57,319 --> 04:08:59,680 okay so as soon as it as soon as we 5799 04:08:59,680 --> 04:09:02,000 modify that session which passport is 5800 04:09:02,000 --> 04:09:04,279 modifying the session for us so it 5801 04:09:04,279 --> 04:09:06,040 attaches this passport object to it and 5802 04:09:06,040 --> 04:09:10,520 then it notice how it takes this ID and 5803 04:09:10,520 --> 04:09:12,479 then Maps it to the user like that and 5804 04:09:12,479 --> 04:09:15,119 that's assigned to the passport object 5805 04:09:15,119 --> 04:09:17,199 and now watch this if I were to go back 5806 04:09:17,199 --> 04:09:20,359 to serialize user and if I were to 5807 04:09:20,359 --> 04:09:21,880 change this to let's 5808 04:09:21,880 --> 04:09:25,119 say username so let me do that real 5809 04:09:25,119 --> 04:09:28,520 quick so let me change it to username 5810 04:09:28,520 --> 04:09:30,399 I'll just remove this log right over 5811 04:09:30,399 --> 04:09:33,479 here so let's search by the 5812 04:09:33,479 --> 04:09:36,720 username okay and now I want you to see 5813 04:09:36,720 --> 04:09:38,479 what happens so I'm going to log in 5814 04:09:38,479 --> 04:09:39,199 right 5815 04:09:39,199 --> 04:09:41,720 now okay so I've just logged in and I'm 5816 04:09:41,720 --> 04:09:44,000 going to make a request to that off/ 5817 04:09:44,000 --> 04:09:46,080 status endpoint and now if you look at 5818 04:09:46,080 --> 04:09:48,680 the logs you can see that we are we 5819 04:09:48,680 --> 04:09:51,319 serialized the user by using that 5820 04:09:51,319 --> 04:09:53,520 username field now some of you might 5821 04:09:53,520 --> 04:09:55,080 wonder well why can't we just pass in 5822 04:09:55,080 --> 04:09:56,680 the whole user object so let's say for 5823 04:09:56,680 --> 04:09:58,479 example instead of just passing in the 5824 04:09:58,479 --> 04:10:00,399 ID or just the username I pass in the 5825 04:10:00,399 --> 04:10:02,760 entire user object right over here so 5826 04:10:02,760 --> 04:10:04,159 since we have the user object I probably 5827 04:10:04,159 --> 04:10:06,040 don't even need to search for it so you 5828 04:10:06,040 --> 04:10:07,560 know what let me just comment all this 5829 04:10:07,560 --> 04:10:10,080 out and I'll pass in I'll just replace 5830 04:10:10,080 --> 04:10:11,960 all this for now I wouldn't recommend 5831 04:10:11,960 --> 04:10:14,399 this but I'll just show you what happens 5832 04:10:14,399 --> 04:10:17,279 so let's reog 5833 04:10:17,279 --> 04:10:21,399 in and you can see how now passport. 5834 04:10:21,399 --> 04:10:23,640 user has the entire user record 5835 04:10:23,640 --> 04:10:26,040 populated okay so let me explain to you 5836 04:10:26,040 --> 04:10:29,000 why you probably don't want to do this 5837 04:10:29,000 --> 04:10:31,319 because your session data you don't 5838 04:10:31,319 --> 04:10:33,960 wanted to have a bunch of 5839 04:10:33,960 --> 04:10:37,760 information that could possibly go stale 5840 04:10:37,760 --> 04:10:39,920 okay because think of it like this right 5841 04:10:39,920 --> 04:10:42,199 something like the ID is never going to 5842 04:10:42,199 --> 04:10:44,560 change okay the username might change 5843 04:10:44,560 --> 04:10:46,000 every once in a while but the ID is 5844 04:10:46,000 --> 04:10:48,040 never going to change if let's say the 5845 04:10:48,040 --> 04:10:49,800 display name changes and then you 5846 04:10:49,800 --> 04:10:52,600 deserialize the user by simply grabbing 5847 04:10:52,600 --> 04:10:54,720 that user from the session data and then 5848 04:10:54,720 --> 04:10:56,080 you grab 5849 04:10:56,080 --> 04:10:58,640 everything including that stale display 5850 04:10:58,640 --> 04:11:01,439 name that's going to be returned back to 5851 04:11:01,439 --> 04:11:04,720 the client if they request it okay so if 5852 04:11:04,720 --> 04:11:06,159 you make changes to the database it's 5853 04:11:06,159 --> 04:11:07,439 not going to be synced up with your 5854 04:11:07,439 --> 04:11:09,319 session data you'll have to manually 5855 04:11:09,319 --> 04:11:10,560 update that yourself and that is going 5856 04:11:10,560 --> 04:11:12,960 to be an additional uh you know scope of 5857 04:11:12,960 --> 04:11:15,239 work that you'll have to take care of 5858 04:11:15,239 --> 04:11:17,159 and you also don't want to just store a 5859 04:11:17,159 --> 04:11:19,680 bunch of properties inside the session 5860 04:11:19,680 --> 04:11:22,880 data that you don't really need because 5861 04:11:22,880 --> 04:11:24,319 it's just going to take up a lot of 5862 04:11:24,319 --> 04:11:26,439 memory and you're going to you know clog 5863 04:11:26,439 --> 04:11:28,479 up your entire database with all of the 5864 04:11:28,479 --> 04:11:29,960 stuff that you don't need all you really 5865 04:11:29,960 --> 04:11:32,840 need is just some unique identifier such 5866 04:11:32,840 --> 04:11:34,359 as the ID or the username and then 5867 04:11:34,359 --> 04:11:36,159 you're good you don't need anything else 5868 04:11:36,159 --> 04:11:38,279 aside from that okay the Der serialized 5869 04:11:38,279 --> 04:11:41,479 user function is responsible for finding 5870 04:11:41,479 --> 04:11:43,239 that user and then attaching it to that 5871 04:11:43,239 --> 04:11:46,159 request. user object that's all it is 5872 04:11:46,159 --> 04:11:48,600 okay so again you could do it but should 5873 04:11:48,600 --> 04:11:50,439 you probably not all right so I'm just 5874 04:11:50,439 --> 04:11:52,560 going to change everything back to using 5875 04:11:52,560 --> 04:11:55,119 the ID and then let's go ahead and take 5876 04:11:55,119 --> 04:11:56,920 a look at this so I'm going to log in 5877 04:11:56,920 --> 04:11:59,040 I'm good here I can log in as any other 5878 04:11:59,040 --> 04:12:01,920 user I want so watch this uh oh and one 5879 04:12:01,920 --> 04:12:03,199 more thing I'm going to show you notice 5880 04:12:03,199 --> 04:12:06,520 how if I delete my cookies and if I try 5881 04:12:06,520 --> 04:12:08,199 to call this endon again is going to 5882 04:12:08,199 --> 04:12:10,119 give me a 401 unauthorized cuz I don't 5883 04:12:10,119 --> 04:12:11,359 have that cookie 5884 04:12:11,359 --> 04:12:14,319 anymore okay which of course makes sense 5885 04:12:14,319 --> 04:12:16,080 but let's try to log in as a different 5886 04:12:16,080 --> 04:12:18,800 user so let's log in 5887 04:12:18,800 --> 04:12:23,119 as Adam so the password is hello 5888 04:12:25,439 --> 04:12:29,119 hello so I am logged in as 5889 04:12:29,119 --> 04:12:31,880 Adam okay so now if I make a get request 5890 04:12:31,880 --> 04:12:34,720 to the status endpoint my credentials 5891 04:12:34,720 --> 04:12:36,760 will be Adam adom so now you'll see in 5892 04:12:36,760 --> 04:12:38,319 the log 5893 04:12:38,319 --> 04:12:42,080 that this is what we have okay so I hope 5894 04:12:42,080 --> 04:12:43,159 all this makes sense and I hope you 5895 04:12:43,159 --> 04:12:45,080 better understand how to use passport 5896 04:12:45,080 --> 04:12:48,159 now um and what I'll do in the next part 5897 04:12:48,159 --> 04:12:49,520 of our tutorial is we're going to go 5898 04:12:49,520 --> 04:12:52,119 ahead and actually set up a database and 5899 04:12:52,119 --> 04:12:54,600 then you're going to learn how to use 5900 04:12:54,600 --> 04:12:57,840 the database to create the user and also 5901 04:12:57,840 --> 04:13:00,159 save the user to the database and then 5902 04:13:00,159 --> 04:13:03,840 we have a fullon login logout mechanism 5903 04:13:03,840 --> 04:13:05,920 before I end this part of the tutorial 5904 04:13:05,920 --> 04:13:06,960 though I do want to show you one more 5905 04:13:06,960 --> 04:13:07,680 thing 5906 04:13:07,680 --> 04:13:09,760 on how to log the user out in other 5907 04:13:09,760 --> 04:13:11,680 words just destroy the 5908 04:13:11,680 --> 04:13:15,520 session okay so what I'll do is WR over 5909 04:13:15,520 --> 04:13:17,960 here so I'm going to set up an endpoint 5910 04:13:17,960 --> 04:13:22,199 it'll be a post request so app. poost 5911 04:13:22,199 --> 04:13:27,520 apith logout let's pass our callback 5912 04:13:28,600 --> 04:13:31,359 function and then first let's just check 5913 04:13:31,359 --> 04:13:36,080 to see if there is uh no user defined 5914 04:13:36,080 --> 04:13:37,399 which means the user is not actually 5915 04:13:37,399 --> 04:13:40,760 logged in then I'll return a response 5916 04:13:40,760 --> 04:13:42,560 and I'll send a status of 5917 04:13:42,560 --> 04:13:45,760 401 which means not 5918 04:13:45,760 --> 04:13:47,960 authenticated and then if they are 5919 04:13:47,960 --> 04:13:49,640 logged in we'll just reference the 5920 04:13:49,640 --> 04:13:51,680 request object and then we can just call 5921 04:13:51,680 --> 04:13:53,239 this logout 5922 04:13:53,239 --> 04:13:55,600 function and then we do need to pass in 5923 04:13:55,600 --> 04:13:58,239 a callback function for 5924 04:13:58,239 --> 04:14:03,560 this okay and then all I'll do is this 5925 04:14:03,560 --> 04:14:06,080 if there's any error I'll just return 5926 04:14:06,080 --> 04:14:07,680 response 5927 04:14:07,680 --> 04:14:08,840 send 5928 04:14:08,840 --> 04:14:11,680 status 400 I 5929 04:14:11,680 --> 04:14:14,920 guess and uh I think there that's really 5930 04:14:14,920 --> 04:14:16,760 all we need to do but if there's no 5931 04:14:16,760 --> 04:14:20,000 errors I'll just send a response back of 5932 04:14:20,000 --> 04:14:22,880 200 okay let's test this out now so 5933 04:14:22,880 --> 04:14:24,000 let's log 5934 04:14:24,000 --> 04:14:26,680 in and then let's see our status so we 5935 04:14:26,680 --> 04:14:29,319 are logged in let me go ahead and make a 5936 04:14:29,319 --> 04:14:31,560 new 5937 04:14:32,359 --> 04:14:36,199 request so it's a post request so SL API 5938 04:14:36,199 --> 04:14:39,040 SL SL 5939 04:14:39,040 --> 04:14:41,439 logout and you can see now we are logged 5940 04:14:41,439 --> 04:14:44,560 out and uh okay so our cookies are still 5941 04:14:44,560 --> 04:14:48,080 going to be present on our client but if 5942 04:14:48,080 --> 04:14:50,279 I try to make a request to the status 5943 04:14:50,279 --> 04:14:52,479 endpoint it's going to say 401 5944 04:14:52,479 --> 04:14:54,479 unauthorized because even though we are 5945 04:14:54,479 --> 04:14:56,920 sending cookies to the server the server 5946 04:14:56,920 --> 04:15:00,040 knows that that cookie is not valid okay 5947 04:15:00,040 --> 04:15:03,279 so even if I try to uh well now if I 5948 04:15:03,279 --> 04:15:04,760 actually try to send a post request to 5949 04:15:04,760 --> 04:15:08,000 here you can see that it gives a 41 5950 04:15:08,000 --> 04:15:09,920 unauthorized because we are not logged 5951 04:15:09,920 --> 04:15:11,640 in okay so it doesn't matter if we have 5952 04:15:11,640 --> 04:15:13,840 the cookie this cookie is no longer 5953 04:15:13,840 --> 04:15:18,319 valid so it's going to give us a 401 5954 04:15:18,319 --> 04:15:21,080 okay so hopefully uh this makes sense 5955 04:15:21,080 --> 04:15:22,800 and in the next part of this whole 5956 04:15:22,800 --> 04:15:24,040 tutorial series I'm going to actually 5957 04:15:24,040 --> 04:15:25,880 set up a database and then we'll 5958 04:15:25,880 --> 04:15:27,760 actually convert everything over and 5959 04:15:27,760 --> 04:15:29,399 then you'll learn how to actually 5960 04:15:29,399 --> 04:15:31,960 encrypt the password as well instead of 5961 04:15:31,960 --> 04:15:34,080 just comparing raw passwords right now 5962 04:15:34,080 --> 04:15:36,439 which is what you don't want to do so 5963 04:15:36,439 --> 04:15:37,760 I'll see you in the the next part of 5964 04:15:37,760 --> 04:15:39,920 this 5965 04:15:41,720 --> 04:15:44,040 tutorial all right so now in this part 5966 04:15:44,040 --> 04:15:46,080 of our tutorial I'm going to show you 5967 04:15:46,080 --> 04:15:48,479 how we can actually connect to a mongodb 5968 04:15:48,479 --> 04:15:52,680 database using uh mongodb as well as 5969 04:15:52,680 --> 04:15:56,600 so what we're going to do is first 5970 04:15:56,600 --> 04:15:58,840 obviously make sure you have mongodb 5971 04:15:58,840 --> 04:16:01,840 installed on your local system I do 5972 04:16:01,840 --> 04:16:04,560 already and I'm going to go ahead and 5973 04:16:04,560 --> 04:16:08,560 install mongus which is is a node 5974 04:16:08,560 --> 04:16:11,720 module so let me go into my 5975 04:16:11,720 --> 04:16:15,720 terminal and let's do npmi whoops 5976 04:16:15,720 --> 04:16:18,640 Mongoose like 5977 04:16:20,279 --> 04:16:24,600 this okay so we're good with that so now 5978 04:16:24,600 --> 04:16:26,840 what we want to do is let's just go 5979 04:16:26,840 --> 04:16:29,359 ahead into our code and we want to 5980 04:16:29,359 --> 04:16:31,640 actually connect to our database so it's 5981 04:16:31,640 --> 04:16:33,720 pretty easy so all we have to do is 5982 04:16:33,720 --> 04:16:36,359 first import so let's do import 5983 04:16:36,359 --> 04:16:40,880 from and I'm inside my index 5984 04:16:40,880 --> 04:16:42,479 file right over 5985 04:16:42,479 --> 04:16:46,920 here okay and then what I'll do is write 5986 04:16:46,920 --> 04:16:49,119 down 5987 04:16:49,119 --> 04:16:53,760 over I guess write over 5988 04:16:53,760 --> 04:16:55,960 here I'll go ahead and do doc 5989 04:16:55,960 --> 04:16:59,080 connect and I'm going to pass in the URI 5990 04:16:59,080 --> 04:17:02,279 so mongodb is the 5991 04:17:02,279 --> 04:17:06,319 protocol and then the host name so Local 5992 04:17:06,319 --> 04:17:07,560 Host 5993 04:17:07,560 --> 04:17:09,880 and then Port 27017 you don't have to 5994 04:17:09,880 --> 04:17:11,920 provide the port if this is the port 5995 04:17:11,920 --> 04:17:13,880 that you use because by default it will 5996 04:17:13,880 --> 04:17:15,760 use port 5997 04:17:15,760 --> 04:17:18,319 27017 but let's say if your mongod DB is 5998 04:17:18,319 --> 04:17:20,000 running on a different port then you 5999 04:17:20,000 --> 04:17:22,080 must specify that Port because the 6000 04:17:22,080 --> 04:17:25,159 package uses this port by default 6001 04:17:25,159 --> 04:17:28,279 okay so I'm going to Omit that but then 6002 04:17:28,279 --> 04:17:29,680 I'm going to specify the database name 6003 04:17:29,680 --> 04:17:31,319 so I'll just call this 6004 04:17:31,319 --> 04:17:34,119 Express uh 6005 04:17:34,119 --> 04:17:36,080 tutorial and then what I'll do is 6006 04:17:36,080 --> 04:17:38,239 disconnect function actually returns a 6007 04:17:38,239 --> 04:17:40,279 promise so I'll just use 6008 04:17:40,279 --> 04:17:43,960 then and I'll conso 6009 04:17:45,040 --> 04:17:49,319 log connected to database and then I'll 6010 04:17:49,319 --> 04:17:52,040 also use a catch if there are any 6011 04:17:52,040 --> 04:17:54,760 errors I'll just simply 6012 04:17:54,760 --> 04:17:58,119 whoops write a log error and then I'll 6013 04:17:58,119 --> 04:17:59,239 log the 6014 04:17:59,239 --> 04:18:03,159 error so let's start up our 6015 04:18:03,159 --> 04:18:05,800 server let's make sure everything is 6016 04:18:05,800 --> 04:18:08,720 working fine 6017 04:18:11,680 --> 04:18:12,920 okay so you can see right over here it 6018 04:18:12,920 --> 04:18:16,080 says connected to database all right 6019 04:18:16,080 --> 04:18:19,399 cool so let me go ahead and open up my 6020 04:18:19,399 --> 04:18:23,319 mongodb GUI my graphical user 6021 04:18:23,319 --> 04:18:25,600 interface let me just zoom in a little 6022 04:18:25,600 --> 04:18:27,640 bit so this is mongodb compass you can 6023 04:18:27,640 --> 04:18:29,319 use whatever you want I'm just going to 6024 04:18:29,319 --> 04:18:31,080 use this for now so I'm going to connect 6025 04:18:31,080 --> 04:18:34,159 to my mongodb database I'll click 6026 04:18:34,159 --> 04:18:36,279 connect okay so we are successfully 6027 04:18:36,279 --> 04:18:37,640 connected to to the database so that's 6028 04:18:37,640 --> 04:18:41,239 great so now let's go ahead and set up 6029 04:18:41,239 --> 04:18:44,319 our very first schema now some people 6030 04:18:44,319 --> 04:18:45,600 might wonder well why are we using 6031 04:18:45,600 --> 04:18:48,279 Mongoose and not using like the regular 6032 04:18:48,279 --> 04:18:51,399 mongodb package for example let me just 6033 04:18:51,399 --> 04:18:52,880 show you this one over here some people 6034 04:18:52,880 --> 04:18:55,119 might wonder why we're not using this 6035 04:18:55,119 --> 04:18:57,840 node.js driver to interact with our 6036 04:18:57,840 --> 04:19:00,359 mongodb database the reason why is 6037 04:19:00,359 --> 04:19:02,560 because realistically in projects you're 6038 04:19:02,560 --> 04:19:05,239 not actually going to be uh writing 6039 04:19:05,239 --> 04:19:07,720 actual query like they do over 6040 04:19:07,720 --> 04:19:10,920 here you're going to be using an OM for 6041 04:19:10,920 --> 04:19:12,840 many realistic projects because they are 6042 04:19:12,840 --> 04:19:15,319 very safe to use for your codebase it's 6043 04:19:15,319 --> 04:19:19,000 the same reason why many people use orms 6044 04:19:19,000 --> 04:19:21,359 for SQL because it just takes care of a 6045 04:19:21,359 --> 04:19:23,439 lot of things for you and it also takes 6046 04:19:23,439 --> 04:19:25,159 care of structuring your data it makes 6047 04:19:25,159 --> 04:19:27,680 it a lot more easier okay so I just 6048 04:19:27,680 --> 04:19:29,040 wanted to mention that in case some of 6049 04:19:29,040 --> 04:19:31,359 you might wonder why we're not using the 6050 04:19:31,359 --> 04:19:34,239 regular mongod DB driver but instead 6051 04:19:34,239 --> 04:19:35,640 we're using 6052 04:19:35,640 --> 04:19:38,479 mongus okay and Mongoose is very popular 6053 04:19:38,479 --> 04:19:40,800 many people use Mongoose if you look at 6054 04:19:40,800 --> 04:19:43,080 the node module on the website you see 6055 04:19:43,080 --> 04:19:45,359 there's 2 million downloads so it's not 6056 04:19:45,359 --> 04:19:47,319 going anywhere anytime 6057 04:19:47,319 --> 04:19:49,399 soon okay anyways let's go ahead and 6058 04:19:49,399 --> 04:19:52,159 create our schema so we're going to 6059 04:19:52,159 --> 04:19:54,359 create a new folder and I'll call this I 6060 04:19:54,359 --> 04:19:55,560 guess 6061 04:19:55,560 --> 04:19:59,279 mongus and really what a schema is is 6062 04:19:59,279 --> 04:20:03,040 it's just a way for you to Define your 6063 04:20:03,040 --> 04:20:05,920 database uh collection you want to you 6064 04:20:05,920 --> 04:20:08,159 want to actually sh sh how your data is 6065 04:20:08,159 --> 04:20:09,760 going to look like and we do that using 6066 04:20:09,760 --> 04:20:11,920 a schema so I'm going to create a folder 6067 04:20:11,920 --> 04:20:13,640 called and I have my folder called 6068 04:20:13,640 --> 04:20:16,040 schemas and inside that schemas folder 6069 04:20:16,040 --> 04:20:17,600 I'm going to create a new file and I'm 6070 04:20:17,600 --> 04:20:18,680 going to call this 6071 04:20:18,680 --> 04:20:21,119 user.js 6072 04:20:21,119 --> 04:20:23,080 okay and this is where I'm going to 6073 04:20:23,080 --> 04:20:25,119 create my user 6074 04:20:25,119 --> 04:20:27,479 schema because what I'm going to do is 6075 04:20:27,479 --> 04:20:31,000 I'm going to replace uh my endpoint 6076 04:20:31,000 --> 04:20:32,640 where I create a user so right over here 6077 04:20:32,640 --> 04:20:34,640 I'm going to replace this so instead of 6078 04:20:34,640 --> 04:20:36,199 actually saving it to the array we're 6079 04:20:36,199 --> 04:20:38,239 going to to just fix this to actually 6080 04:20:38,239 --> 04:20:41,239 save it to the database okay and then 6081 04:20:41,239 --> 04:20:42,880 I'm going to show you how we can replace 6082 04:20:42,880 --> 04:20:44,760 how we can actually use our database to 6083 04:20:44,760 --> 04:20:47,479 perform the uh authentication validation 6084 04:20:47,479 --> 04:20:50,800 instead of using the array so the first 6085 04:20:50,800 --> 04:20:52,920 thing that we need to do is we need to 6086 04:20:52,920 --> 04:20:56,040 go ahead and of course create our schema 6087 04:20:56,040 --> 04:20:58,960 so I'm going to go ahead and import 6088 04:20:58,960 --> 04:21:02,279 schema from like 6089 04:21:02,279 --> 04:21:04,960 this and then I'm going to go ahead and 6090 04:21:04,960 --> 04:21:07,080 create a variable I'll call this us user 6091 04:21:07,080 --> 04:21:11,080 schema equals new schema just like that 6092 04:21:11,080 --> 04:21:13,439 and over here we can Define our Fields 6093 04:21:13,439 --> 04:21:16,359 so what does our user look like so we 6094 04:21:16,359 --> 04:21:19,199 have a username a display name and a 6095 04:21:19,199 --> 04:21:22,880 password so let's set up the username 6096 04:21:22,880 --> 04:21:26,600 field and we need to specify the data 6097 04:21:26,600 --> 04:21:28,119 type okay now obviously we're using 6098 04:21:28,119 --> 04:21:30,880 JavaScript so there's no data types in 6099 04:21:30,880 --> 04:21:32,640 JavaScript or there's no explicit 6100 04:21:32,640 --> 04:21:34,080 typings in JavaScript is what I meant to 6101 04:21:34,080 --> 04:21:36,119 say not data types there's no explicit 6102 04:21:36,119 --> 04:21:36,960 typing 6103 04:21:36,960 --> 04:21:39,960 that we can set to this username field 6104 04:21:39,960 --> 04:21:41,319 so there's actually there's one thing 6105 04:21:41,319 --> 04:21:42,760 that we can use so let me actually do 6106 04:21:42,760 --> 04:21:44,199 this import 6107 04:21:44,199 --> 04:21:47,040 uh let me actually do this instead 6108 04:21:47,040 --> 04:21:49,479 let me import mongus and do mongus do 6109 04:21:49,479 --> 04:21:51,199 schema 6110 04:21:51,199 --> 04:21:54,279 instead and then to set the field for 6111 04:21:54,279 --> 04:21:56,960 username to basically tell mongus what 6112 04:21:56,960 --> 04:21:58,520 data type this is we're going to use 6113 04:21:58,520 --> 04:22:02,800 mongus do schema. types and here we can 6114 04:22:02,800 --> 04:22:05,359 reference which data type we want to 6115 04:22:05,359 --> 04:22:07,040 Define our username as 6116 04:22:07,040 --> 04:22:08,800 so your our username is going to be a 6117 04:22:08,800 --> 04:22:09,920 string so I'm going to Define as a 6118 04:22:09,920 --> 04:22:11,080 string like 6119 04:22:11,080 --> 04:22:13,399 this and then we're going to set the 6120 04:22:13,399 --> 04:22:16,239 display name same exact 6121 04:22:16,239 --> 04:22:19,760 thing oh whoops this should be comma 6122 04:22:19,760 --> 04:22:23,319 sorry about that and then the last thing 6123 04:22:23,319 --> 04:22:26,600 was uh 6124 04:22:27,159 --> 04:22:30,960 password okay let's do that 6125 04:22:30,960 --> 04:22:33,920 password okay great now additionally 6126 04:22:33,920 --> 04:22:35,920 let's say some of your Fields might be 6127 04:22:35,920 --> 04:22:38,920 required ired so what you can do is 6128 04:22:38,920 --> 04:22:41,399 instead of assigning this type to your 6129 04:22:41,399 --> 04:22:43,040 field you can actually assign an object 6130 04:22:43,040 --> 04:22:45,479 to that field and then you can specify 6131 04:22:45,479 --> 04:22:48,520 type the field type on that object and 6132 04:22:48,520 --> 04:22:51,880 set it to that mango. schema. types. 6133 04:22:51,880 --> 04:22:56,000 string and then you can set this uh 6134 04:22:56,000 --> 04:22:57,479 field called 6135 04:22:57,479 --> 04:23:01,279 required like this and set it true true 6136 04:23:01,279 --> 04:23:02,880 like that so I'll do the same thing for 6137 04:23:02,880 --> 04:23:05,439 our password as well uh let me just go 6138 04:23:05,439 --> 04:23:07,399 ahead and 6139 04:23:07,399 --> 04:23:09,439 I'll just copy and paste 6140 04:23:09,439 --> 04:23:13,279 this okay another thing that we can do 6141 04:23:13,279 --> 04:23:15,159 is for our username we can mark this 6142 04:23:15,159 --> 04:23:17,560 field as unique by simply just using 6143 04:23:17,560 --> 04:23:20,399 this unique field and set this a true so 6144 04:23:20,399 --> 04:23:22,720 that way if there is already an existing 6145 04:23:22,720 --> 04:23:26,239 user document in the user collection 6146 04:23:26,239 --> 04:23:29,560 that has this username let's say ansen 6147 04:23:29,560 --> 04:23:31,640 and if we try to save another document 6148 04:23:31,640 --> 04:23:34,119 to the user collection with username set 6149 04:23:34,119 --> 04:23:36,439 to Anon it's going to throw an error 6150 04:23:36,439 --> 04:23:38,640 okay so that's hopefully that makes 6151 04:23:38,640 --> 04:23:42,119 sense okay so we're pretty much done 6152 04:23:42,119 --> 04:23:44,239 with our schema this is a pretty simple 6153 04:23:44,239 --> 04:23:46,760 schema so now what we need to actually 6154 04:23:46,760 --> 04:23:50,040 do after we create this schema instance 6155 04:23:50,040 --> 04:23:52,600 is we need to actually uh compile it 6156 04:23:52,600 --> 04:23:55,479 into a model and that's actually pretty 6157 04:23:55,479 --> 04:23:59,159 easy so what I can do is I can uh create 6158 04:23:59,159 --> 04:24:02,080 a variable I'll call this user and then 6159 04:24:02,080 --> 04:24:03,680 we just need to reference mongus and 6160 04:24:03,680 --> 04:24:06,479 then we just call model and then we need 6161 04:24:06,479 --> 04:24:08,920 to give a name so I'm just call this 6162 04:24:08,920 --> 04:24:11,880 user and you pass in the schema so user 6163 04:24:11,880 --> 04:24:15,439 schema like this okay and you use this 6164 04:24:15,439 --> 04:24:19,840 model to actually perform operations for 6165 04:24:19,840 --> 04:24:22,920 that user collection for the database so 6166 04:24:22,920 --> 04:24:24,279 if you want to search for a user you 6167 04:24:24,279 --> 04:24:26,359 would reference this user model and you 6168 04:24:26,359 --> 04:24:28,600 would call this find one method like 6169 04:24:28,600 --> 04:24:31,399 that okay but let's go ahead and just 6170 04:24:31,399 --> 04:24:34,119 export this model so we're pretty much 6171 04:24:34,119 --> 04:24:36,720 done with our schema and our model all 6172 04:24:36,720 --> 04:24:38,960 right so now we can go ahead and save 6173 04:24:38,960 --> 04:24:41,399 data to a database save a user to a 6174 04:24:41,399 --> 04:24:44,319 database and we'll go ahead into our 6175 04:24:44,319 --> 04:24:46,960 user endpoint so I already have one set 6176 04:24:46,960 --> 04:24:49,680 up right over here already and you can 6177 04:24:49,680 --> 04:24:52,520 see I'm inside my user router uh right 6178 04:24:52,520 --> 04:24:56,040 over here so I'm going to just use reuse 6179 04:24:56,040 --> 04:24:56,960 this 6180 04:24:56,960 --> 04:24:59,040 endpoint but I would encourage you if 6181 04:24:59,040 --> 04:25:01,080 you don't if you are not using routers I 6182 04:25:01,080 --> 04:25:02,439 would encourage you to do so but if you 6183 04:25:02,439 --> 04:25:04,119 don't have it set up just create your 6184 04:25:04,119 --> 04:25:07,040 own uh / API us endpoint or whatever 6185 04:25:07,040 --> 04:25:09,279 endpoint you want to create the user I'm 6186 04:25:09,279 --> 04:25:11,000 going to remove all this stuff out over 6187 04:25:11,000 --> 04:25:13,359 here and I'm also going to just remove 6188 04:25:13,359 --> 04:25:15,560 this validation check because I don't 6189 04:25:15,560 --> 04:25:17,760 want to combine too much stuff because 6190 04:25:17,760 --> 04:25:19,600 some I know some of you may have not 6191 04:25:19,600 --> 04:25:21,000 watched the previous parts of this 6192 04:25:21,000 --> 04:25:22,279 tutorial and I want to make sure you at 6193 04:25:22,279 --> 04:25:24,159 least understand the purpose of what 6194 04:25:24,159 --> 04:25:25,359 we're trying to do instead of just 6195 04:25:25,359 --> 04:25:27,040 adding all of these dependencies on top 6196 04:25:27,040 --> 04:25:28,760 of it but if you have been following 6197 04:25:28,760 --> 04:25:31,359 along then I encourage you to perform 6198 04:25:31,359 --> 04:25:33,520 the validation check yourself so that 6199 04:25:33,520 --> 04:25:35,840 way it's better practice for you okay 6200 04:25:35,840 --> 04:25:37,520 but we're going to pretty much start as 6201 04:25:37,520 --> 04:25:39,520 if we're doing it from scratch so don't 6202 04:25:39,520 --> 04:25:41,680 worry so what we want to do is we're 6203 04:25:41,680 --> 04:25:43,239 going to send the post request to this 6204 04:25:43,239 --> 04:25:44,920 endpoint and we're going to send the 6205 04:25:44,920 --> 04:25:48,119 username password and um the display 6206 04:25:48,119 --> 04:25:49,520 name 6207 04:25:49,520 --> 04:25:52,479 okay so const body we're going to 6208 04:25:52,479 --> 04:25:54,080 destructure the request body from the 6209 04:25:54,080 --> 04:25:57,359 request object and since we're not doing 6210 04:25:57,359 --> 04:25:58,800 validation right now I'm just going to 6211 04:25:58,800 --> 04:26:01,800 assume that the validation uh has 6212 04:26:01,800 --> 04:26:03,439 happened so that means that I'm going to 6213 04:26:03,439 --> 04:26:05,399 assume that the request body is correct 6214 04:26:05,399 --> 04:26:07,159 but of course like I said you want to 6215 04:26:07,159 --> 04:26:09,600 always make sure you are validating 6216 04:26:09,600 --> 04:26:13,439 everything okay but we we'll assume just 6217 04:26:13,439 --> 04:26:14,800 for tutorial purposes we'll assume that 6218 04:26:14,800 --> 04:26:16,359 the request body is 6219 04:26:16,359 --> 04:26:20,359 valid and then what I'll do is I'll just 6220 04:26:20,359 --> 04:26:23,640 go ahead and do this I need to First 6221 04:26:23,640 --> 04:26:26,080 import that let me remove that I need to 6222 04:26:26,080 --> 04:26:31,600 First import that user model so import 6223 04:26:31,920 --> 04:26:38,000 user uh import user from 6224 04:26:38,119 --> 04:26:40,840 6225 04:26:40,840 --> 04:26:45,080 schemas user okay so I just imported the 6226 04:26:45,080 --> 04:26:48,960 user model and then what I need to do is 6227 04:26:48,960 --> 04:26:50,680 I need to create an instance of that 6228 04:26:50,680 --> 04:26:53,080 user model it's very easy all we do is 6229 04:26:53,080 --> 04:26:54,319 first declare variable I'll call this 6230 04:26:54,319 --> 04:26:55,159 new 6231 04:26:55,159 --> 04:27:00,560 user equals new user like this and then 6232 04:27:00,560 --> 04:27:02,199 this Constructor the user Constructor I 6233 04:27:02,199 --> 04:27:06,279 can pass in the object the body object 6234 04:27:06,279 --> 04:27:08,080 which contains all the fields that we 6235 04:27:08,080 --> 04:27:10,399 want to save to the database for the 6236 04:27:10,399 --> 04:27:13,840 user so once I do that I need to then 6237 04:27:13,840 --> 04:27:17,439 actually save that user to the database 6238 04:27:17,439 --> 04:27:20,479 so what I'll do is I will do this 6239 04:27:20,479 --> 04:27:23,600 declare variable called saved user now 6240 04:27:23,600 --> 04:27:25,560 to save the user to the database we just 6241 04:27:25,560 --> 04:27:28,560 simply call the save method on this new 6242 04:27:28,560 --> 04:27:32,359 user variable because that's the user 6243 04:27:32,359 --> 04:27:36,040 instance but this method is asynchronous 6244 04:27:36,040 --> 04:27:37,920 so we need to make sure we are using a 6245 04:27:37,920 --> 04:27:41,560 weight behind this new user. save call 6246 04:27:41,560 --> 04:27:43,640 but of course to do to do that we need 6247 04:27:43,640 --> 04:27:45,960 to add the Asing keyword in front of our 6248 04:27:45,960 --> 04:27:49,119 callback function okay and then let's 6249 04:27:49,119 --> 04:27:51,119 also make sure we are handling errors 6250 04:27:51,119 --> 04:27:54,720 accordingly so what I'll do is this I'm 6251 04:27:54,720 --> 04:27:55,840 going to go 6252 04:27:55,840 --> 04:27:59,479 ahead and since this function can likely 6253 04:27:59,479 --> 04:28:00,800 throw an error I'm going to wrap it 6254 04:28:00,800 --> 04:28:03,760 inside the try catch like 6255 04:28:03,760 --> 04:28:05,960 this because this is just creating the 6256 04:28:05,960 --> 04:28:06,920 instance it's not going to throw an 6257 04:28:06,920 --> 04:28:08,960 error I don't think the Constructor the 6258 04:28:08,960 --> 04:28:11,239 Constructor will actually error out it's 6259 04:28:11,239 --> 04:28:13,600 only when you try to save then mongodb 6260 04:28:13,600 --> 04:28:15,239 could failed to save the user maybe 6261 04:28:15,239 --> 04:28:17,159 because uh the username that we're 6262 04:28:17,159 --> 04:28:19,439 trying to save for that new user already 6263 04:28:19,439 --> 04:28:21,840 exists in the entire user collection so 6264 04:28:21,840 --> 04:28:24,239 it could error out so first if there are 6265 04:28:24,239 --> 04:28:26,840 no errors at all and we're good then 6266 04:28:26,840 --> 04:28:28,920 I'll just return a 6267 04:28:28,920 --> 04:28:32,279 response so let's set the status code to 6268 04:28:32,279 --> 04:28:34,439 be 6269 04:28:34,439 --> 04:28:36,880 2011 whoops it's 6270 04:28:36,880 --> 04:28:41,279 response. status 2011 and then send back 6271 04:28:41,279 --> 04:28:44,560 the new user like this and if an error 6272 04:28:44,560 --> 04:28:47,040 does happen uh I'll just log the 6273 04:28:47,040 --> 04:28:51,239 error and I'll just send back a status 6274 04:28:51,239 --> 04:28:53,920 code of 400 which just means bad 6275 04:28:53,920 --> 04:28:56,920 request oh whoops should be return 6276 04:28:56,920 --> 04:28:58,760 misspelled that okay let's test this out 6277 04:28:58,760 --> 04:29:00,720 let's go into Thunder client let's make 6278 04:29:00,720 --> 04:29:03,199 a post request to 6279 04:29:03,199 --> 04:29:07,720 our users endpoint 6280 04:29:08,680 --> 04:29:13,199 okay and let's pass in the username 6281 04:29:13,199 --> 04:29:15,359 anen 6282 04:29:15,359 --> 04:29:18,439 password let's do hello one 123 and then 6283 04:29:18,439 --> 04:29:20,840 display 6284 04:29:22,319 --> 04:29:26,840 name click Send uh is our server 6285 04:29:26,840 --> 04:29:31,080 up oh well I forgot to name this user. 6286 04:29:31,080 --> 04:29:35,880 MJS not user.js so let me rename that um 6287 04:29:35,880 --> 04:29:37,479 um and I guess I will click yes to 6288 04:29:37,479 --> 04:29:38,720 update the 6289 04:29:38,720 --> 04:29:42,040 import so inside my routes file yep yeah 6290 04:29:42,040 --> 04:29:43,800 so the problem was that I needed to 6291 04:29:43,800 --> 04:29:49,439 actually add user do MJS here as well 6292 04:29:49,439 --> 04:29:52,520 okay um all right so now our server 6293 04:29:52,520 --> 04:29:55,080 should be up and 6294 04:29:55,080 --> 04:29:58,080 running click Send okay and now you can 6295 04:29:58,080 --> 04:30:02,600 see the data was saved to the 6296 04:30:02,600 --> 04:30:06,080 database let's go into mongodb let's 6297 04:30:06,080 --> 04:30:08,319 refresh see how the express tutorial 6298 04:30:08,319 --> 04:30:10,239 database appears over here and we have 6299 04:30:10,239 --> 04:30:12,880 our users collection and I can now see 6300 04:30:12,880 --> 04:30:15,560 my user in the users collection so 6301 04:30:15,560 --> 04:30:18,000 that's wonderful and watch what happens 6302 04:30:18,000 --> 04:30:20,760 if I try to click Send again it's going 6303 04:30:20,760 --> 04:30:22,760 to say bad request and the reason why is 6304 04:30:22,760 --> 04:30:24,720 because uh you can see over here it 6305 04:30:24,720 --> 04:30:26,439 complains about a duplicate key error 6306 04:30:26,439 --> 04:30:28,720 and that's because I marked the username 6307 04:30:28,720 --> 04:30:31,760 as a unique field okay so if we try to 6308 04:30:31,760 --> 04:30:34,479 save that same username to the database 6309 04:30:34,479 --> 04:30:37,000 again it's going to throw an error so 6310 04:30:37,000 --> 04:30:38,920 hopefully that makes sense now 6311 04:30:38,920 --> 04:30:40,239 additionally what I'm going to do and 6312 04:30:40,239 --> 04:30:41,520 this is really just for the people who 6313 04:30:41,520 --> 04:30:44,119 are in fact wanting to validate their 6314 04:30:44,119 --> 04:30:46,279 schemas I will add the schema check 6315 04:30:46,279 --> 04:30:47,439 right 6316 04:30:47,439 --> 04:30:50,080 now so what I'll do is right before we 6317 04:30:50,080 --> 04:30:52,880 call this request Handler function I'm 6318 04:30:52,880 --> 04:30:55,279 going to call that check schema function 6319 04:30:55,279 --> 04:30:56,920 and I'm going to pass 6320 04:30:56,920 --> 04:31:00,680 in the create user validation 6321 04:31:00,680 --> 04:31:02,680 schema and like I said all the code is 6322 04:31:02,680 --> 04:31:04,119 going to be in the description so if you 6323 04:31:04,119 --> 04:31:05,199 do want to do some of the things that 6324 04:31:05,199 --> 04:31:07,159 you may have missed from earlier just go 6325 04:31:07,159 --> 04:31:08,880 to the GI up link and grab the code and 6326 04:31:08,880 --> 04:31:10,880 you can see everything from there but 6327 04:31:10,880 --> 04:31:13,359 I'm just using this validation schema 6328 04:31:13,359 --> 04:31:15,520 and I have this password uh that I 6329 04:31:15,520 --> 04:31:16,960 didn't have this before but I just added 6330 04:31:16,960 --> 04:31:18,800 this just now so I have this password 6331 04:31:18,800 --> 04:31:21,040 field right over here and then I am 6332 04:31:21,040 --> 04:31:23,040 making sure that password is not empty 6333 04:31:23,040 --> 04:31:24,880 and you can see that I'm making sure 6334 04:31:24,880 --> 04:31:26,760 that the username has all these 6335 04:31:26,760 --> 04:31:28,279 validators that are applied to that 6336 04:31:28,279 --> 04:31:29,800 username field same thing for the 6337 04:31:29,800 --> 04:31:32,439 display name okay so by calling this 6338 04:31:32,439 --> 04:31:34,199 this will make sure it checks the 6339 04:31:34,199 --> 04:31:37,000 request body and performs the necessary 6340 04:31:37,000 --> 04:31:39,720 validation on it this middleware does 6341 04:31:39,720 --> 04:31:41,680 not actually throw an error we need to 6342 04:31:41,680 --> 04:31:43,800 go inside the request Handler and 6343 04:31:43,800 --> 04:31:44,880 actually 6344 04:31:44,880 --> 04:31:47,920 check if there are any errors so the way 6345 04:31:47,920 --> 04:31:50,359 we do that is we just simply declare a 6346 04:31:50,359 --> 04:31:52,199 variable called result and we call this 6347 04:31:52,199 --> 04:31:55,000 validation result function I think that 6348 04:31:55,000 --> 04:31:56,399 is imported yep it's imported right up 6349 04:31:56,399 --> 04:31:59,040 top over here from Express 6350 04:31:59,040 --> 04:32:02,640 validator and we pass in the request 6351 04:32:02,640 --> 04:32:05,119 object into validation result and what 6352 04:32:05,119 --> 04:32:08,119 this will do is it'll actually grab that 6353 04:32:08,119 --> 04:32:09,920 um this Dynamic Property that was 6354 04:32:09,920 --> 04:32:12,080 attached to the request object that 6355 04:32:12,080 --> 04:32:14,880 Express validator does for us and it'll 6356 04:32:14,880 --> 04:32:17,239 give us the information about what the 6357 04:32:17,239 --> 04:32:19,920 statuses of the request body it'll tell 6358 04:32:19,920 --> 04:32:21,479 us if there are any errors it'll tell us 6359 04:32:21,479 --> 04:32:23,520 if everything is good if there are no 6360 04:32:23,520 --> 04:32:27,040 errors so what we can do is I can just 6361 04:32:27,040 --> 04:32:28,359 simply say if 6362 04:32:28,359 --> 04:32:32,239 result uh dot let's see is empty so this 6363 04:32:32,239 --> 04:32:34,080 just basically means if there are no 6364 04:32:34,080 --> 04:32:37,399 errors then we can proceed but I'll make 6365 04:32:37,399 --> 04:32:41,279 sure I do this if there are errors so if 6366 04:32:41,279 --> 04:32:42,800 the result is not empty that means there 6367 04:32:42,800 --> 04:32:46,159 are errors I'll return a response and 6368 04:32:46,159 --> 04:32:47,279 I'll just 6369 04:32:47,279 --> 04:32:50,960 simply do this return response. 6370 04:32:50,960 --> 04:32:54,920 send and I'm going to go ahead and send 6371 04:32:54,920 --> 04:32:57,119 results. array and this actually gives 6372 04:32:57,119 --> 04:33:00,319 me all the errors so it will tell us 6373 04:33:00,319 --> 04:33:03,040 okay what fields are missing or what 6374 04:33:03,040 --> 04:33:05,639 fields are invalid okay so just very 6375 04:33:05,639 --> 04:33:08,240 quickly I'll show you how this works so 6376 04:33:08,240 --> 04:33:10,799 notice how if I omit display name it's 6377 04:33:10,799 --> 04:33:12,359 going to complain about display name not 6378 04:33:12,359 --> 04:33:14,759 being there if I omit password it's 6379 04:33:14,759 --> 04:33:16,759 going to complain about the password and 6380 04:33:16,759 --> 04:33:19,719 if I omit username username will not 6381 04:33:19,719 --> 04:33:21,118 it'll complain about the username over 6382 04:33:21,118 --> 04:33:24,359 here so hopefully that makes sense with 6383 04:33:24,359 --> 04:33:28,480 how this works so now of course if there 6384 04:33:28,480 --> 04:33:30,719 are no errors at all then we can proceed 6385 04:33:30,719 --> 04:33:34,879 with the rest of our um our logic but 6386 04:33:34,879 --> 04:33:36,438 ideally you don't want to use the actual 6387 04:33:36,438 --> 04:33:38,639 request body object from the request 6388 04:33:38,639 --> 04:33:41,039 object itself like we're doing over here 6389 04:33:41,039 --> 04:33:43,599 you want to actually use this function 6390 04:33:43,599 --> 04:33:45,278 so let me first declare a variable I'll 6391 04:33:45,278 --> 04:33:48,039 call this data equals and then we're 6392 04:33:48,039 --> 04:33:50,000 going to call this matched data function 6393 04:33:50,000 --> 04:33:52,359 which is imported up top over here from 6394 04:33:52,359 --> 04:33:54,599 Express 6395 04:33:54,599 --> 04:33:56,680 validator and then I'm going to pass in 6396 04:33:56,680 --> 04:33:58,680 the request object and so what this 6397 04:33:58,680 --> 04:34:00,199 match data function will do is it'll 6398 04:34:00,199 --> 04:34:02,958 grab all the validated fields for you so 6399 04:34:02,958 --> 04:34:04,840 it'll grab username password and display 6400 04:34:04,840 --> 04:34:08,240 name for us okay so let me remove this 6401 04:34:08,240 --> 04:34:11,400 and let me place this with data 6402 04:34:11,400 --> 04:34:14,359 instead and I'll also just console log 6403 04:34:14,359 --> 04:34:17,039 data as well and let's try this again so 6404 04:34:17,039 --> 04:34:19,919 this time let's make a post request uh 6405 04:34:19,919 --> 04:34:22,278 for the username I'll do Anon 6406 04:34:22,278 --> 04:34:25,118 one and that was created and if you look 6407 04:34:25,118 --> 04:34:27,400 at the console you can see that that 6408 04:34:27,400 --> 04:34:29,080 object is being logged right over there 6409 04:34:29,080 --> 04:34:31,520 and of course if we did omit any data at 6410 04:34:31,520 --> 04:34:34,278 all then it would just return that 6411 04:34:34,278 --> 04:34:35,561 response right over here let me set the 6412 04:34:35,561 --> 04:34:36,561 status 6413 04:34:36,561 --> 04:34:39,039 to uh 6414 04:34:39,039 --> 04:34:43,400 400 okay there we go perfect so like I 6415 04:34:43,400 --> 04:34:44,759 said I just wanted to show you how to 6416 04:34:44,759 --> 04:34:46,400 perform validation if that was something 6417 04:34:46,400 --> 04:34:48,719 that you also additionally wanted to do 6418 04:34:48,719 --> 04:34:50,719 but I also wanted to show you a very 6419 04:34:50,719 --> 04:34:52,759 easy way just to save records to the 6420 04:34:52,759 --> 04:34:54,958 database okay and everything is going to 6421 04:34:54,958 --> 04:34:58,199 be similar to how we just did this okay 6422 04:34:58,199 --> 04:35:01,080 so we have one user schema if you wanted 6423 04:35:01,080 --> 04:35:04,320 to save products to your database cuz we 6424 04:35:04,320 --> 04:35:06,359 have endpoint for product s then you 6425 04:35:06,359 --> 04:35:07,520 would have to make sure you create a 6426 04:35:07,520 --> 04:35:09,919 product schema compile that schema into 6427 04:35:09,919 --> 04:35:11,840 a model by using this model function 6428 04:35:11,840 --> 04:35:13,400 right over here or this model method on 6429 04:35:13,400 --> 04:35:16,199 the Mongoose import and then now you can 6430 04:35:16,199 --> 04:35:18,240 just easily create products in your 6431 04:35:18,240 --> 04:35:19,799 database that's literally all you have 6432 04:35:19,799 --> 04:35:22,480 to do all right so now in the previous 6433 04:35:22,480 --> 04:35:24,199 parts of the tutorial where we covered 6434 04:35:24,199 --> 04:35:26,278 authentication with passport I was 6435 04:35:26,278 --> 04:35:29,039 actually using users that were in an 6436 04:35:29,039 --> 04:35:30,719 array and what I want to do is I want to 6437 04:35:30,719 --> 04:35:33,799 go back to the authentication part and I 6438 04:35:33,799 --> 04:35:35,438 want to show you how we can replace all 6439 04:35:35,438 --> 04:35:37,320 this stuff with using the actual 6440 04:35:37,320 --> 04:35:39,520 database itself CU I know many of you 6441 04:35:39,520 --> 04:35:41,118 want to actually see an example of how 6442 04:35:41,118 --> 04:35:42,680 to do that and that's what I'm going to 6443 04:35:42,680 --> 04:35:44,879 do and then after we finish this I'm 6444 04:35:44,879 --> 04:35:47,599 going to show you how we can encrypt or 6445 04:35:47,599 --> 04:35:49,759 hash out our passwords because you 6446 04:35:49,759 --> 04:35:52,599 obviously never want to save raw 6447 04:35:52,599 --> 04:35:55,199 passwords raw text passwords like this 6448 04:35:55,199 --> 04:35:56,840 in your database because if someone 6449 04:35:56,840 --> 04:35:58,759 hacks into your database they can see 6450 04:35:58,759 --> 04:36:00,199 everything they can see the username the 6451 04:36:00,199 --> 04:36:02,160 password and that's not good you want to 6452 04:36:02,160 --> 04:36:03,680 actually hash it so I'll show you that 6453 04:36:03,680 --> 04:36:06,039 after we just replace all this stuff 6454 04:36:06,039 --> 04:36:09,759 over here okay so first let's go ahead 6455 04:36:09,759 --> 04:36:12,958 and do this I'm going to go 6456 04:36:12,958 --> 04:36:16,400 ahead and go 6457 04:36:16,400 --> 04:36:19,840 into my verify function right here so 6458 04:36:19,840 --> 04:36:21,240 this is where we are searching for the 6459 04:36:21,240 --> 04:36:22,879 user in a database and then we're going 6460 04:36:22,879 --> 04:36:25,400 to compare the passwords okay so I'm 6461 04:36:25,400 --> 04:36:27,278 going to pretty much just remove this 6462 04:36:27,278 --> 04:36:28,438 Mock 6463 04:36:28,438 --> 04:36:30,799 usind and let me just remove this whole 6464 04:36:30,799 --> 04:36:33,760 let me just remove all 6465 04:36:34,080 --> 04:36:35,958 this 6466 04:36:35,958 --> 04:36:39,840 okay move all this so we need to First 6467 04:36:39,840 --> 04:36:42,278 import the user 6468 04:36:42,278 --> 04:36:46,599 model from our user MJS file inside the 6469 04:36:46,599 --> 04:36:48,561 schemas 6470 04:36:48,561 --> 04:36:50,599 folder and then we need to search for 6471 04:36:50,599 --> 04:36:53,199 the user so I'll show you how to do that 6472 04:36:53,199 --> 04:36:56,480 so conine user equals so first we need 6473 04:36:56,480 --> 04:36:58,680 to make sure we reference the user model 6474 04:36:58,680 --> 04:37:00,359 and we're going to call this 6475 04:37:00,359 --> 04:37:03,039 find one 6476 04:37:03,039 --> 04:37:05,480 method and this is where we need to 6477 04:37:05,480 --> 04:37:08,799 actually specify a filter so you're 6478 04:37:08,799 --> 04:37:09,958 going to pass in an object and you're 6479 04:37:09,958 --> 04:37:11,520 going to specify what you want to search 6480 04:37:11,520 --> 04:37:13,759 the user by so in this case we're 6481 04:37:13,759 --> 04:37:16,400 searching the user by the username so 6482 04:37:16,400 --> 04:37:17,879 I'm going to set 6483 04:37:17,879 --> 04:37:21,359 username like this okay since the 6484 04:37:21,359 --> 04:37:22,879 username argument is the same as the 6485 04:37:22,879 --> 04:37:24,680 username field I don't have to do this 6486 04:37:24,680 --> 04:37:26,840 like username callon username I can just 6487 04:37:26,840 --> 04:37:29,320 do username like that now this fin one 6488 04:37:29,320 --> 04:37:31,520 method is asynchronous so we do need to 6489 04:37:31,520 --> 04:37:36,199 make sure that we await this call 6490 04:37:36,199 --> 04:37:37,879 and then we also need to make sure we 6491 04:37:37,879 --> 04:37:39,561 add the acing keyword in front of our 6492 04:37:39,561 --> 04:37:40,958 verify 6493 04:37:40,958 --> 04:37:43,118 function okay and of course if there are 6494 04:37:43,118 --> 04:37:45,438 any errors at all it will just be it 6495 04:37:45,438 --> 04:37:48,000 will be thrown by let's say the fine one 6496 04:37:48,000 --> 04:37:50,400 method and then we're catching it so 6497 04:37:50,400 --> 04:37:51,639 that will be handled accordingly so 6498 04:37:51,639 --> 04:37:55,278 that's already set up just fine so now 6499 04:37:55,278 --> 04:37:58,599 let's first check to see if fine user is 6500 04:37:58,599 --> 04:38:01,480 actually defined because this fine one 6501 04:38:01,480 --> 04:38:03,480 method could return null which means 6502 04:38:03,480 --> 04:38:05,480 that the user was not found so if 6503 04:38:05,480 --> 04:38:08,240 there's no fine user then I'll throw a 6504 04:38:08,240 --> 04:38:11,759 new error and I'll just call I'll just 6505 04:38:11,759 --> 04:38:15,958 say uh user not found and then the error 6506 04:38:15,958 --> 04:38:17,160 will be caught right down over here in 6507 04:38:17,160 --> 04:38:18,799 this catch block which we'll call the 6508 04:38:18,799 --> 04:38:21,320 done function with the error instance 6509 04:38:21,320 --> 04:38:25,039 now the user is found and we can verify 6510 04:38:25,039 --> 04:38:27,080 that the user is defined so now we want 6511 04:38:27,080 --> 04:38:28,759 to check to see if the passwords are 6512 04:38:28,759 --> 04:38:33,240 valid so if find user. password so I can 6513 04:38:33,240 --> 04:38:35,438 reference the property directly on this 6514 04:38:35,438 --> 04:38:37,840 instance that I just searched for if 6515 04:38:37,840 --> 04:38:40,719 it's not equal to password then we're 6516 04:38:40,719 --> 04:38:43,240 going to throw an error as well and 6517 04:38:43,240 --> 04:38:45,118 we'll just say bad 6518 04:38:45,118 --> 04:38:47,359 credentials literally the same exact 6519 04:38:47,359 --> 04:38:49,000 thing only difference is that we are 6520 04:38:49,000 --> 04:38:50,278 replacing the 6521 04:38:50,278 --> 04:38:53,160 array um with referencing the actual 6522 04:38:53,160 --> 04:38:55,240 user model and calling the f one method 6523 04:38:55,240 --> 04:38:57,520 okay same exact thing let me just remove 6524 04:38:57,520 --> 04:38:59,080 these two console 6525 04:38:59,080 --> 04:39:02,000 logs and we need to go into our 6526 04:39:02,000 --> 04:39:04,520 deserialized user method and then we 6527 04:39:04,520 --> 04:39:07,160 need to make sure we remove this mock us 6528 04:39:07,160 --> 04:39:10,199 that fine and uh 6529 04:39:10,199 --> 04:39:13,919 instead let me remove all this instead 6530 04:39:13,919 --> 04:39:15,719 we're going to go ahead and call user. 6531 04:39:15,719 --> 04:39:18,199 find one but this time we're going to 6532 04:39:18,199 --> 04:39:21,080 actually search by the ID but the nice 6533 04:39:21,080 --> 04:39:22,240 thing is though there's actually this 6534 04:39:22,240 --> 04:39:25,400 method called find by 6535 04:39:25,400 --> 04:39:28,879 ID and I think this should return just 6536 04:39:28,879 --> 04:39:33,400 one user record let me just double 6537 04:39:33,840 --> 04:39:35,958 check 6538 04:39:35,958 --> 04:39:37,360 yep finds a single document so it 6539 04:39:37,360 --> 04:39:39,000 doesn't return an array just wanted to 6540 04:39:39,000 --> 04:39:41,080 double check so this is good because 6541 04:39:41,080 --> 04:39:44,200 remember our mongodb database it autog 6542 04:39:44,200 --> 04:39:46,840 generates these object IDs right over 6543 04:39:46,840 --> 04:39:50,280 here so we can use that to search in a 6544 04:39:50,280 --> 04:39:52,878 database okay so I I'll just pass in ID 6545 04:39:52,878 --> 04:39:54,600 like that and let's just make sure we 6546 04:39:54,600 --> 04:39:56,240 add the Asing keyword in front of our 6547 04:39:56,240 --> 04:39:57,718 callback function for der serialized 6548 04:39:57,718 --> 04:40:00,120 user and the rest of the logic stays the 6549 04:40:00,120 --> 04:40:01,638 same if the user is not found throw an 6550 04:40:01,638 --> 04:40:03,798 error if the user is found then it's 6551 04:40:03,798 --> 04:40:05,080 going to ignore this part and then we're 6552 04:40:05,080 --> 04:40:07,040 going to call the done 6553 04:40:07,040 --> 04:40:11,000 function okay and I think that should be 6554 04:40:11,000 --> 04:40:14,638 it um let me just make sure that 6555 04:40:14,638 --> 04:40:17,080 everything else is 6556 04:40:17,080 --> 04:40:19,280 good uh we can ignore the other parts of 6557 04:40:19,280 --> 04:40:20,560 our application that are still using the 6558 04:40:20,560 --> 04:40:23,798 mock users that's fine okay so let's 6559 04:40:23,798 --> 04:40:26,600 test out our login and log out feature 6560 04:40:26,600 --> 04:40:28,920 using the actual database to search for 6561 04:40:28,920 --> 04:40:33,080 the user and see what happens so we have 6562 04:40:33,080 --> 04:40:34,840 two users we have Anson and we have we 6563 04:40:34,840 --> 04:40:36,680 have our password set to one 12 hello 6564 04:40:36,680 --> 04:40:37,718 123 for 6565 04:40:37,718 --> 04:40:40,600 that so let's go 6566 04:40:40,600 --> 04:40:43,280 into let me 6567 04:40:43,280 --> 04:40:46,840 see okay let's go into Postman uh so I'm 6568 04:40:46,840 --> 04:40:48,160 going to pass in Anson and then I'm 6569 04:40:48,160 --> 04:40:51,000 going to pass in the password hello 6570 04:40:51,000 --> 04:40:54,480 123 um oh whoops I made the post request 6571 04:40:54,480 --> 04:40:56,798 to the wrong 6572 04:40:56,798 --> 04:40:59,520 endpoint okay so it's still processing I 6573 04:40:59,520 --> 04:41:02,200 wonder what's going 6574 04:41:03,160 --> 04:41:06,638 on uh let's 6575 04:41:07,638 --> 04:41:11,680 see let me try let me restart my 6576 04:41:11,680 --> 04:41:14,480 server let's try 6577 04:41:14,480 --> 04:41:16,760 again uh okay so for some reason it's 6578 04:41:16,760 --> 04:41:18,920 still processing I think I may have 6579 04:41:18,920 --> 04:41:21,360 forgot may have forgotten 6580 04:41:21,360 --> 04:41:25,840 something let's see find by ID uh oh 6581 04:41:25,840 --> 04:41:27,240 yeah that's right I forgot to call the 6582 04:41:27,240 --> 04:41:29,798 done function down here silly me so 6583 04:41:29,798 --> 04:41:31,560 let's just pass in null for the error 6584 04:41:31,560 --> 04:41:33,000 and pass in the 6585 04:41:33,000 --> 04:41:35,160 user my apology 6586 04:41:35,160 --> 04:41:37,240 click Send okay so we are logged in 6587 04:41:37,240 --> 04:41:40,840 let's verify that we're logged in by 6588 04:41:40,840 --> 04:41:45,718 making a call to the status 6589 04:41:48,280 --> 04:41:51,240 endpoint so we're making a get request 6590 04:41:51,240 --> 04:41:53,440 so I am logged in you can see 6591 04:41:53,440 --> 04:41:56,798 now my actual ID that's in the database 6592 04:41:56,798 --> 04:41:58,638 is right there okay and I have my 6593 04:41:58,638 --> 04:42:01,040 username my display name my password all 6594 04:42:01,040 --> 04:42:02,560 that stuff that's from the 6595 04:42:02,560 --> 04:42:05,320 database okay so we are a ble to log in 6596 04:42:05,320 --> 04:42:07,160 using records that are in the database 6597 04:42:07,160 --> 04:42:10,920 so hopefully that makes 6598 04:42:12,958 --> 04:42:15,440 sense okay so now I'm going to show you 6599 04:42:15,440 --> 04:42:18,718 how to Hash passwords for your users 6600 04:42:18,718 --> 04:42:20,520 when you're creating them before you 6601 04:42:20,520 --> 04:42:22,480 actually save it to the database because 6602 04:42:22,480 --> 04:42:24,638 right now if you were to look right over 6603 04:42:24,638 --> 04:42:27,440 here in the database you can see all of 6604 04:42:27,440 --> 04:42:29,040 the users and their passwords and that's 6605 04:42:29,040 --> 04:42:31,760 obviously not good and that's obviously 6606 04:42:31,760 --> 04:42:34,600 a huge red flag for any application that 6607 04:42:34,600 --> 04:42:38,160 that saves the passwords raw like this 6608 04:42:38,160 --> 04:42:40,440 because if a hacker gets access to your 6609 04:42:40,440 --> 04:42:42,560 database they can literally see all your 6610 04:42:42,560 --> 04:42:45,760 users and all of their passwords and 6611 04:42:45,760 --> 04:42:47,440 that's not good so you want to make sure 6612 04:42:47,440 --> 04:42:49,560 you always hash your passwords before 6613 04:42:49,560 --> 04:42:51,798 you save it to the database and then 6614 04:42:51,798 --> 04:42:54,040 once we hash the passwords I'll show you 6615 04:42:54,040 --> 04:42:57,000 how when we log in how we can actually 6616 04:42:57,000 --> 04:43:00,280 compare the hash passwords so that way 6617 04:43:00,280 --> 04:43:01,560 we can validate that the user is 6618 04:43:01,560 --> 04:43:04,920 actually passing in the correct password 6619 04:43:04,920 --> 04:43:07,760 so first let's go ahead and install 6620 04:43:07,760 --> 04:43:11,638 bcrypt so let me go into my terminal 6621 04:43:11,638 --> 04:43:13,798 npmi so I'm going to type 6622 04:43:13,798 --> 04:43:17,840 npmi bcrypt like 6623 04:43:19,440 --> 04:43:22,878 this and once we're done let's just 6624 04:43:22,878 --> 04:43:24,600 rerun our server 6625 04:43:24,600 --> 04:43:27,520 again and now what I'm going to do is 6626 04:43:27,520 --> 04:43:30,878 let's go into our utils folder I'll 6627 04:43:30,878 --> 04:43:34,760 create a new file called uh 6628 04:43:34,760 --> 04:43:36,798 because we want to keep this logic 6629 04:43:36,798 --> 04:43:39,718 separate from the rest of our 6630 04:43:39,718 --> 04:43:42,080 codebase so inside here I'm going to go 6631 04:43:42,080 --> 04:43:47,120 ahead and import bcrypt from bcrypt like 6632 04:43:47,120 --> 04:43:50,760 this and what I want to do is I simply 6633 04:43:50,760 --> 04:43:52,840 just want to create a function that will 6634 04:43:52,840 --> 04:43:55,280 take care of hashing the password so 6635 04:43:55,280 --> 04:43:56,958 I'll create a function and I'll export 6636 04:43:56,958 --> 04:44:00,958 it I'll call this function hash 6637 04:44:01,200 --> 04:44:03,760 password and this function is going to 6638 04:44:03,760 --> 04:44:07,040 take in the actual password itself so 6639 04:44:07,040 --> 04:44:09,240 password and the way that we actually 6640 04:44:09,240 --> 04:44:11,840 begin hashing the password in bcrypt is 6641 04:44:11,840 --> 04:44:14,440 very simple the first thing that we need 6642 04:44:14,440 --> 04:44:17,120 is obviously the plain text password and 6643 04:44:17,120 --> 04:44:20,040 then the other thing that we need is a 6644 04:44:20,040 --> 04:44:23,000 what it's called a salt round okay and 6645 04:44:23,000 --> 04:44:24,520 basically the salt round just pretty 6646 04:44:24,520 --> 04:44:27,000 much means uh you know how much time is 6647 04:44:27,000 --> 04:44:30,280 needed to calculate the hash for for 6648 04:44:30,280 --> 04:44:33,000 bcrypt so of course the more rounds you 6649 04:44:33,000 --> 04:44:35,878 you want that it's going to increase 6650 04:44:35,878 --> 04:44:37,920 complexity uh the documentation 6651 04:44:37,920 --> 04:44:41,040 recommends 10 so I'll create a variable 6652 04:44:41,040 --> 04:44:44,040 I'll call this salt rounds equals 10 6653 04:44:44,040 --> 04:44:49,440 like that and then uh what we can do is 6654 04:44:49,440 --> 04:44:51,200 we first want to actually generate a 6655 04:44:51,200 --> 04:44:53,560 salt so this is the rounds but we want 6656 04:44:53,560 --> 04:44:56,638 to still generate the salt so I'm going 6657 04:44:56,638 --> 04:45:00,120 to go ahead and call bcrypt and then I'm 6658 04:45:00,120 --> 04:45:05,040 going to go ahead and call this gen salt 6659 04:45:05,040 --> 04:45:07,280 method like this and then you can just 6660 04:45:07,280 --> 04:45:09,920 simply pass in the rounds so salt rounds 6661 04:45:09,920 --> 04:45:11,400 like 6662 04:45:11,400 --> 04:45:14,240 that and then this is asynchronous but 6663 04:45:14,240 --> 04:45:16,680 there's also a gen salt sync function 6664 04:45:16,680 --> 04:45:18,760 that is synchronous so if you don't want 6665 04:45:18,760 --> 04:45:21,200 to use async A8 then you could just use 6666 04:45:21,200 --> 04:45:23,120 this function so it it really doesn't 6667 04:45:23,120 --> 04:45:25,400 matter all that much because we're going 6668 04:45:25,400 --> 04:45:26,680 to do we're going to be doing everything 6669 04:45:26,680 --> 04:45:29,120 in order anyways so first we want to 6670 04:45:29,120 --> 04:45:30,718 generate the salt so let me store this 6671 04:45:30,718 --> 04:45:34,240 in a variable so bcrypt do gen salt sync 6672 04:45:34,240 --> 04:45:36,680 and then pass inst 6673 04:45:36,680 --> 04:45:39,560 rounds next what you're going to do is 6674 04:45:39,560 --> 04:45:41,840 you're going to go ahead and call 6675 04:45:41,840 --> 04:45:43,638 bcrypt and then you're going to call 6676 04:45:43,638 --> 04:45:46,638 Hash so again this is asynchronous as 6677 04:45:46,638 --> 04:45:48,878 well so this will 6678 04:45:48,878 --> 04:45:52,520 return a promise with the data in this 6679 04:45:52,520 --> 04:45:54,680 case our password 6680 04:45:54,680 --> 04:45:57,320 hashed now there's also hash sync which 6681 04:45:57,320 --> 04:45:59,480 is the same function it just does it 6682 04:45:59,480 --> 04:46:01,320 synchronously so it doesn't return a 6683 04:46:01,320 --> 04:46:03,280 promise but it will call this function 6684 04:46:03,280 --> 04:46:05,638 and then it return the string so I'll 6685 04:46:05,638 --> 04:46:07,878 just use hash sync since I'm using gen 6686 04:46:07,878 --> 04:46:10,638 salt sync as well so you call this 6687 04:46:10,638 --> 04:46:12,400 function and you pass in the text 6688 04:46:12,400 --> 04:46:15,040 password that you want to hash and then 6689 04:46:15,040 --> 04:46:18,080 you want to pass in the salt okay so 6690 04:46:18,080 --> 04:46:20,798 I'll just pass in this salt right over 6691 04:46:20,798 --> 04:46:26,200 here just like that and then I'm done 6692 04:46:26,200 --> 04:46:29,000 and then I can just return this so 6693 04:46:29,000 --> 04:46:30,760 whenever I call Hash password this will 6694 04:46:30,760 --> 04:46:31,958 hash the password for me so let me 6695 04:46:31,958 --> 04:46:34,760 actually console log salt as well cuz I 6696 04:46:34,760 --> 04:46:36,600 want to actually show you what this 6697 04:46:36,600 --> 04:46:39,360 value looks like so now what we want to 6698 04:46:39,360 --> 04:46:43,040 do is before we actually create our user 6699 04:46:43,040 --> 04:46:44,080 we want to make sure we hash the 6700 04:46:44,080 --> 04:46:46,360 password so I'm going to go into my 6701 04:46:46,360 --> 04:46:49,400 users. MJS file in the routes folder so 6702 04:46:49,400 --> 04:46:51,718 I'm going to go back to my users 6703 04:46:51,718 --> 04:46:53,440 endpoint where I'm creating an actual 6704 04:46:53,440 --> 04:46:55,798 user so this one right over here and you 6705 04:46:55,798 --> 04:46:59,840 can see uh right over here on line 61 6706 04:46:59,840 --> 04:47:03,840 I'm creating a new instance of my user 6707 04:47:03,840 --> 04:47:06,480 and then I'm saving it right inside this 6708 04:47:06,480 --> 04:47:09,360 Tri catch or inside this Tri block so 6709 04:47:09,360 --> 04:47:12,600 before I actually pass the data into the 6710 04:47:12,600 --> 04:47:15,040 user Constructor what I want to do is I 6711 04:47:15,040 --> 04:47:17,600 want to reference data. password because 6712 04:47:17,600 --> 04:47:20,480 remember this data is the request body 6713 04:47:20,480 --> 04:47:22,718 that is being sent to this endpoint and 6714 04:47:22,718 --> 04:47:25,400 we expect there to be a password field 6715 04:47:25,400 --> 04:47:27,558 and since we are doing validation after 6716 04:47:27,558 --> 04:47:29,638 this point the password field will be 6717 04:47:29,638 --> 04:47:33,080 there so I want to basically reassign a 6718 04:47:33,080 --> 04:47:35,400 new value or assign new value to the 6719 04:47:35,400 --> 04:47:38,400 password field to the data object itself 6720 04:47:38,400 --> 04:47:41,320 okay so when I pass the data object to 6721 04:47:41,320 --> 04:47:44,360 the Constructor of user it will contain 6722 04:47:44,360 --> 04:47:46,798 the hashed password not the raw password 6723 04:47:46,798 --> 04:47:47,878 so what I'm going to do is I'm going to 6724 04:47:47,878 --> 04:47:49,680 call Hash password and that's going to 6725 04:47:49,680 --> 04:47:52,558 be imported up top over here from our 6726 04:47:52,558 --> 04:47:54,600 helpers file that we just created from 6727 04:47:54,600 --> 04:47:56,240 the utils 6728 04:47:56,240 --> 04:47:59,120 folder uh where am I okay right over 6729 04:47:59,120 --> 04:48:01,480 here now remember the hash password 6730 04:48:01,480 --> 04:48:03,480 function is synchronous because we're 6731 04:48:03,480 --> 04:48:06,080 not using any async A8 in here and we're 6732 04:48:06,080 --> 04:48:07,480 not returning any promises we're just 6733 04:48:07,480 --> 04:48:09,638 returning the string so we don't need to 6734 04:48:09,638 --> 04:48:10,920 use async 6735 04:48:10,920 --> 04:48:14,718 A8 but if you did use the asynchronous 6736 04:48:14,718 --> 04:48:17,400 methods like gen Sal and then hash then 6737 04:48:17,400 --> 04:48:19,160 of course you would need to First await 6738 04:48:19,160 --> 04:48:21,680 this and then you would need to add 6739 04:48:21,680 --> 04:48:23,760 async like that and then you would need 6740 04:48:23,760 --> 04:48:25,200 to add async in front of this hash 6741 04:48:25,200 --> 04:48:27,280 password call so I just wanted to 6742 04:48:27,280 --> 04:48:28,878 mention that for those of you who choose 6743 04:48:28,878 --> 04:48:32,040 to use the asynchronous versions of the 6744 04:48:32,040 --> 04:48:34,280 functions so I'm going to call hash 6745 04:48:34,280 --> 04:48:36,920 password and I'll pass in data. password 6746 04:48:36,920 --> 04:48:39,120 so this will take the raw password hash 6747 04:48:39,120 --> 04:48:40,798 it and then store it back to the 6748 04:48:40,798 --> 04:48:43,360 password field for data so I'll console 6749 04:48:43,360 --> 04:48:45,240 log I'm already conso logging data up 6750 04:48:45,240 --> 04:48:47,320 here but I'll console log it after we 6751 04:48:47,320 --> 04:48:50,120 assigned the new value for password so 6752 04:48:50,120 --> 04:48:51,480 let's go ahead and see how this all 6753 04:48:51,480 --> 04:48:55,160 works so let's go into Thunder client 6754 04:48:55,160 --> 04:48:59,400 let's make a post request to API users 6755 04:48:59,400 --> 04:49:02,520 and we need to send a username password 6756 04:49:02,520 --> 04:49:05,320 and display name 6757 04:49:05,320 --> 04:49:08,718 let's change it let's do uh John and 6758 04:49:08,718 --> 04:49:10,120 username will be John because we already 6759 04:49:10,120 --> 04:49:12,200 have Anson as username already in the 6760 04:49:12,200 --> 04:49:15,280 database going to click Send uh is our 6761 04:49:15,280 --> 04:49:17,040 server up and 6762 04:49:17,040 --> 04:49:19,600 running oh whoops I forgot one more 6763 04:49:19,600 --> 04:49:23,000 thing I think I yep I forgot to name 6764 04:49:23,000 --> 04:49:26,798 helpers JS to MJS let me rename that and 6765 04:49:26,798 --> 04:49:29,200 then inside the users. MJS file we need 6766 04:49:29,200 --> 04:49:32,400 to make sure we are adding that MJS 6767 04:49:32,400 --> 04:49:34,040 extension that was the main problem over 6768 04:49:34,040 --> 04:49:37,160 there I keep forgetting so I apologize 6769 04:49:37,160 --> 04:49:38,798 for that but let's test this out again 6770 04:49:38,798 --> 04:49:41,360 so our server is up and running I go 6771 04:49:41,360 --> 04:49:44,120 back to thunder client click Send uh 6772 04:49:44,120 --> 04:49:46,000 let's see password oh username must be 6773 04:49:46,000 --> 04:49:47,798 at least five characters let's do 6774 04:49:47,798 --> 04:49:51,680 Johnny Okay click Send okay and now you 6775 04:49:51,680 --> 04:49:56,040 can see that the password is hashed 6776 04:49:56,040 --> 04:49:58,798 let's go into my database and you can 6777 04:49:58,798 --> 04:50:01,080 see Johnny is saved in the database and 6778 04:50:01,080 --> 04:50:03,958 it has the hash password okay wonderful 6779 04:50:03,958 --> 04:50:06,680 we now know how to Hash passwords and 6780 04:50:06,680 --> 04:50:09,080 save it to the database now the next 6781 04:50:09,080 --> 04:50:10,680 problem that we have since we have 6782 04:50:10,680 --> 04:50:13,200 authentication is we need to be able to 6783 04:50:13,200 --> 04:50:16,240 actually compare the hashed password 6784 04:50:16,240 --> 04:50:19,520 with a value that we're sending to the 6785 04:50:19,520 --> 04:50:21,440 server because right now let's say if I 6786 04:50:21,440 --> 04:50:23,520 try to log in as Johnny let's do this 6787 04:50:23,520 --> 04:50:25,718 with our current logic right now if I 6788 04:50:25,718 --> 04:50:27,798 try to log in as 6789 04:50:27,798 --> 04:50:30,798 Johnny and I and I provide my password 6790 04:50:30,798 --> 04:50:32,520 like this it's going to throw an error 6791 04:50:32,520 --> 04:50:34,400 it's going to say bad credentials okay 6792 04:50:34,400 --> 04:50:36,280 that's because we're trying to 6793 04:50:36,280 --> 04:50:41,040 compare uh this password hello 123 with 6794 04:50:41,040 --> 04:50:44,520 the hashed text and obviously hello 123 6795 04:50:44,520 --> 04:50:48,760 is not equal to this entire thing so 6796 04:50:48,760 --> 04:50:52,160 here's what we need to do we need to go 6797 04:50:52,160 --> 04:50:54,878 back into our strategy 6798 04:50:54,878 --> 04:50:58,240 file right over here inside our verify 6799 04:50:58,240 --> 04:51:01,680 function okay right over 6800 04:51:01,680 --> 04:51:04,520 here what we need to do right before we 6801 04:51:04,520 --> 04:51:07,400 compare the password instead of uh just 6802 04:51:07,400 --> 04:51:09,520 comparing the raw password that is sent 6803 04:51:09,520 --> 04:51:12,280 from the client side with the hash 6804 04:51:12,280 --> 04:51:15,718 password now we need to actually hash 6805 04:51:15,718 --> 04:51:17,040 that password that was sent from the 6806 04:51:17,040 --> 04:51:19,840 client so what I'm saying is when I send 6807 04:51:19,840 --> 04:51:23,200 hello123 as a password to the server I 6808 04:51:23,200 --> 04:51:25,878 need to Hash hello 123 and then we're 6809 04:51:25,878 --> 04:51:26,958 going to 6810 04:51:26,958 --> 04:51:30,638 compare that hash to the hash value 6811 04:51:30,638 --> 04:51:32,240 that's in the database hopefully that 6812 04:51:32,240 --> 04:51:35,000 makes sense okay and actually there's a 6813 04:51:35,000 --> 04:51:36,798 built-in function that we can use to 6814 04:51:36,798 --> 04:51:39,718 compare hash passwords and I'll show you 6815 04:51:39,718 --> 04:51:41,040 what that field is so let me go back 6816 04:51:41,040 --> 04:51:42,600 into helpers 6817 04:51:42,600 --> 04:51:46,680 MJS so there's this function called uh 6818 04:51:46,680 --> 04:51:50,440 bcrypt do compare or compare sync I'll 6819 04:51:50,440 --> 04:51:52,680 use compare sync and what you do is you 6820 04:51:52,680 --> 04:51:56,400 pass in the plain text 6821 04:51:56,400 --> 04:51:58,558 password and then you pass in the 6822 04:51:58,558 --> 04:52:00,840 encrypted which is the hash password so 6823 04:52:00,840 --> 04:52:02,320 this will be the actual hash password 6824 04:52:02,320 --> 04:52:05,680 that is saved to the user in the 6825 04:52:05,680 --> 04:52:08,040 database so first let me just actually 6826 04:52:08,040 --> 04:52:11,440 create a helper function so I'll do 6827 04:52:11,440 --> 04:52:15,080 compare password equals 6828 04:52:15,080 --> 04:52:17,638 password uh let me do this 6829 04:52:17,638 --> 04:52:21,120 plane hashed like 6830 04:52:21,120 --> 04:52:24,638 this okay and then uh this bcrypt 6831 04:52:24,638 --> 04:52:27,360 compare sync function will return a 6832 04:52:27,360 --> 04:52:30,400 booing so I'll just return that Boolean 6833 04:52:30,400 --> 04:52:32,920 only pass in plane and then the second 6834 04:52:32,920 --> 04:52:34,440 argument for compare sync will be 6835 04:52:34,440 --> 04:52:36,320 encrypted which is just going to be the 6836 04:52:36,320 --> 04:52:39,080 hashed password and so this will return 6837 04:52:39,080 --> 04:52:41,760 a Boolean value so if it if these two 6838 04:52:41,760 --> 04:52:43,320 values are equal to each 6839 04:52:43,320 --> 04:52:46,680 other then it will return true if it's 6840 04:52:46,680 --> 04:52:48,680 not then it will return false so we 6841 04:52:48,680 --> 04:52:50,600 don't actually need to rehash anything 6842 04:52:50,600 --> 04:52:52,840 because this compare sync function will 6843 04:52:52,840 --> 04:52:56,400 take care of it for us okay so let's go 6844 04:52:56,400 --> 04:53:00,718 over to here and then all I need to do 6845 04:53:00,718 --> 04:53:02,320 is just simply instead of doing this 6846 04:53:02,320 --> 04:53:04,160 check like this I can just call compare 6847 04:53:04,160 --> 04:53:05,878 sync or I'm sorry not compare sync 6848 04:53:05,878 --> 04:53:08,400 compare password and then just pass in 6849 04:53:08,400 --> 04:53:11,920 the plain password so that's password 6850 04:53:11,920 --> 04:53:14,000 right over here so this is the value 6851 04:53:14,000 --> 04:53:16,120 sent from the client the raw text 6852 04:53:16,120 --> 04:53:17,798 password and then we're going to pass in 6853 04:53:17,798 --> 04:53:20,360 the hashed password so find user. 6854 04:53:20,360 --> 04:53:23,558 password like that okay and then of 6855 04:53:23,558 --> 04:53:25,360 course if this returns uh we want to 6856 04:53:25,360 --> 04:53:27,440 negate the value because if it returns 6857 04:53:27,440 --> 04:53:29,480 true that means the passwords do match 6858 04:53:29,480 --> 04:53:32,000 but if the password uh don't does not 6859 04:53:32,000 --> 04:53:34,320 match it would return false so if not 6860 04:53:34,320 --> 04:53:36,878 false then we'll throw a new error so 6861 04:53:36,878 --> 04:53:38,440 let's go ahead and try and 6862 04:53:38,440 --> 04:53:40,080 reauthenticate and now you see we're 6863 04:53:40,080 --> 04:53:42,440 good to go and notice how if I try to 6864 04:53:42,440 --> 04:53:44,798 log in as 6865 04:53:44,798 --> 04:53:47,798 ansen because currently ansen has the 6866 04:53:47,798 --> 04:53:49,878 raw text password save in the database 6867 04:53:49,878 --> 04:53:51,718 it should actually error out because now 6868 04:53:51,718 --> 04:53:53,958 we're trying to compare uh the plain 6869 04:53:53,958 --> 04:53:55,520 text to a hashed value which obviously 6870 04:53:55,520 --> 04:53:58,718 is not going to work so yeah that is how 6871 04:53:58,718 --> 04:54:01,558 you can save the hashed password instead 6872 04:54:01,558 --> 04:54:04,240 of the text based password to the 6873 04:54:04,240 --> 04:54:06,360 database and then you can and that's how 6874 04:54:06,360 --> 04:54:07,798 you can compare them as 6875 04:54:07,798 --> 04:54:10,400 well so I hope this part of hashing 6876 04:54:10,400 --> 04:54:12,680 passwords and how you can save the hash 6877 04:54:12,680 --> 04:54:15,280 password to the database and compare it 6878 04:54:15,280 --> 04:54:17,600 makes sense so in the next section of 6879 04:54:17,600 --> 04:54:19,718 the tutorial we're going to go ahead and 6880 04:54:19,718 --> 04:54:21,878 talk about session stores something that 6881 04:54:21,878 --> 04:54:23,718 will help us tremendously because right 6882 04:54:23,718 --> 04:54:26,120 now whenever we keep on restarting our 6883 04:54:26,120 --> 04:54:29,000 server our sessions keep getting dropped 6884 04:54:29,000 --> 04:54:31,320 from the memory store because it's 6885 04:54:31,320 --> 04:54:33,680 volatile so I'll show you how we can 6886 04:54:33,680 --> 04:54:35,360 actually use session stores to save the 6887 04:54:35,360 --> 04:54:38,600 session data to the database so that way 6888 04:54:38,600 --> 04:54:40,798 when we drop the server and then restart 6889 04:54:40,798 --> 04:54:42,760 it our session will be restored and 6890 04:54:42,760 --> 04:54:46,360 you'll see that we remain logged 6891 04:54:48,040 --> 04:54:50,840 in okay so now let's go ahead and move 6892 04:54:50,840 --> 04:54:53,040 on to session stores so this is 6893 04:54:53,040 --> 04:54:56,000 something that you very likely will need 6894 04:54:56,000 --> 04:54:57,798 especially when you want to persist 6895 04:54:57,798 --> 04:54:59,798 session data for the user because 6896 04:54:59,798 --> 04:55:01,760 sometimes your server may go down for 6897 04:55:01,760 --> 04:55:03,680 unknown reasons and they might restart 6898 04:55:03,680 --> 04:55:04,958 and when that happens all of your 6899 04:55:04,958 --> 04:55:07,000 session data will be gone because by 6900 04:55:07,000 --> 04:55:08,878 default Express session stores it in 6901 04:55:08,878 --> 04:55:10,360 memory so what you want to do is you 6902 04:55:10,360 --> 04:55:12,440 want to store this in a database so that 6903 04:55:12,440 --> 04:55:14,000 way it can be persisted whenever your 6904 04:55:14,000 --> 04:55:15,920 server Goes Down And if it goes back up 6905 04:55:15,920 --> 04:55:17,558 the session store will have that session 6906 04:55:17,558 --> 04:55:19,680 data there and express session will look 6907 04:55:19,680 --> 04:55:21,798 in that session store in the database to 6908 04:55:21,798 --> 04:55:23,638 grab the session data and restore it for 6909 04:55:23,638 --> 04:55:25,600 the user so earlier I actually did show 6910 04:55:25,600 --> 04:55:28,080 you how the inmemory session store looks 6911 04:55:28,080 --> 04:55:29,798 like and how it stores data I'll show 6912 04:55:29,798 --> 04:55:32,558 you again so inside my API users 6913 04:55:32,558 --> 04:55:35,958 endpoint is where I have this being uh 6914 04:55:35,958 --> 04:55:38,120 logged so what this does is it looks for 6915 04:55:38,120 --> 04:55:40,878 the session ID and uh if there are an 6916 04:55:40,878 --> 04:55:43,160 errors so just throw in error but then 6917 04:55:43,160 --> 04:55:45,160 we pretty much just log the session data 6918 04:55:45,160 --> 04:55:47,360 right over here so right now I'm not 6919 04:55:47,360 --> 04:55:49,440 logged into the application at all I'm 6920 04:55:49,440 --> 04:55:50,638 not authenticated so I don't have a 6921 04:55:50,638 --> 04:55:53,000 cookie or anything but if I make a 6922 04:55:53,000 --> 04:55:55,718 request to API 6923 04:55:55,718 --> 04:55:58,320 users and if I show the logs you can see 6924 04:55:58,320 --> 04:56:00,878 that right over here uh inside session 6925 04:56:00,878 --> 04:56:03,480 store get that's where I'm logging this 6926 04:56:03,480 --> 04:56:05,040 right over here you can see that the 6927 04:56:05,040 --> 04:56:06,958 session data is undefined okay that's 6928 04:56:06,958 --> 04:56:09,360 fine let me go ahead and log in first 6929 04:56:09,360 --> 04:56:12,440 cuz we haven't actually U modified the 6930 04:56:12,440 --> 04:56:13,760 session data at all we haven't actually 6931 04:56:13,760 --> 04:56:16,480 logged in yet so let me go ahead and log 6932 04:56:16,480 --> 04:56:19,040 in so let's do 6933 04:56:19,040 --> 04:56:22,520 this API 6934 04:56:23,600 --> 04:56:27,600 SLO log in oh whoops did I forget yep 6935 04:56:27,600 --> 04:56:31,400 sorry about that uh bad credentials uh 6936 04:56:31,400 --> 04:56:33,000 oh wait you know what it is it's because 6937 04:56:33,000 --> 04:56:36,280 I'm still comparing the old the raw 6938 04:56:36,280 --> 04:56:38,878 based the raw text password in database 6939 04:56:38,878 --> 04:56:42,160 let me use uh Johnny 6940 04:56:42,160 --> 04:56:44,840 instead and then the password is I think 6941 04:56:44,840 --> 04:56:47,120 it was hello 1 123 as well okay so I 6942 04:56:47,120 --> 04:56:49,320 just successfully logged in let's verify 6943 04:56:49,320 --> 04:56:50,958 let's go to the status endpoint and you 6944 04:56:50,958 --> 04:56:53,558 can see that I'm logged in obviously you 6945 04:56:53,558 --> 04:56:55,320 don't want to return the password but 6946 04:56:55,320 --> 04:56:56,840 that's something for a separate part of 6947 04:56:56,840 --> 04:56:59,600 this tutorial but now watch this when I 6948 04:56:59,600 --> 04:57:01,798 go to/ API 6949 04:57:01,798 --> 04:57:05,718 users you can see see that in the 6950 04:57:05,718 --> 04:57:08,160 console so we're inside session store 6951 04:57:08,160 --> 04:57:11,000 get so we're logging it right over here 6952 04:57:11,000 --> 04:57:12,160 and then you can see that this is the 6953 04:57:12,160 --> 04:57:14,760 session data and you see how the session 6954 04:57:14,760 --> 04:57:17,440 data I have the cookie and then I have 6955 04:57:17,440 --> 04:57:19,878 the passport and then I have the user 6956 04:57:19,878 --> 04:57:21,360 okay so every single time we make a 6957 04:57:21,360 --> 04:57:24,480 request to the server um Express session 6958 04:57:24,480 --> 04:57:26,600 will take care of looking for the 6959 04:57:26,600 --> 04:57:29,760 session data in the inmemory store and 6960 04:57:29,760 --> 04:57:33,240 then it will know who the user is okay 6961 04:57:33,240 --> 04:57:35,680 and then right over here we have the 6962 04:57:35,680 --> 04:57:36,558 user 6963 04:57:36,558 --> 04:57:39,400 ID uh inside passport okay and then 6964 04:57:39,400 --> 04:57:41,680 passport will take care of calling the 6965 04:57:41,680 --> 04:57:44,080 serialized user with the ID and then 6966 04:57:44,080 --> 04:57:45,080 it'll search for the user in the 6967 04:57:45,080 --> 04:57:47,200 database and then that's how it'll grab 6968 04:57:47,200 --> 04:57:48,558 that user from the database and attach 6969 04:57:48,558 --> 04:57:54,480 it to the request object okay so if the 6970 04:57:54,480 --> 04:57:55,840 server goes down so let's say right now 6971 04:57:55,840 --> 04:57:57,920 if I restart the 6972 04:57:57,920 --> 04:58:01,080 server and if I try to 6973 04:58:01,080 --> 04:58:03,520 visit uh let's say if I visit the 6974 04:58:03,520 --> 04:58:06,200 previous endpoint let's do au/ status 6975 04:58:06,200 --> 04:58:07,840 you see how it says unauthorized so 6976 04:58:07,840 --> 04:58:10,040 we're not even logged in anymore okay 6977 04:58:10,040 --> 04:58:12,240 all of our session data is gone if I 6978 04:58:12,240 --> 04:58:15,240 were to go back to SL API users you can 6979 04:58:15,240 --> 04:58:18,200 see that now the session store does not 6980 04:58:18,200 --> 04:58:20,878 have our data it says undefined so 6981 04:58:20,878 --> 04:58:23,480 obviously that's a problem so what we 6982 04:58:23,480 --> 04:58:26,760 can do is we can use a session store to 6983 04:58:26,760 --> 04:58:29,718 save the session data and it's actually 6984 04:58:29,718 --> 04:58:32,400 not that difficult to use because all we 6985 04:58:32,400 --> 04:58:34,000 really need to do 6986 04:58:34,000 --> 04:58:36,120 is just have a database 6987 04:58:36,120 --> 04:58:39,040 connection which we already do already 6988 04:58:39,040 --> 04:58:40,920 so in earlier parts of the tutorial I 6989 04:58:40,920 --> 04:58:43,680 showed you how to connect to a mongodb 6990 04:58:43,680 --> 04:58:47,000 database using so that's this 6991 04:58:47,000 --> 04:58:49,320 right over here and then what we can do 6992 04:58:49,320 --> 04:58:51,958 is we can reuse that connection to 6993 04:58:51,958 --> 04:58:53,958 connect our session store to that 6994 04:58:53,958 --> 04:58:56,638 database so we're going to use this 6995 04:58:56,638 --> 04:58:58,878 package called connect and pretty 6996 04:58:58,878 --> 04:59:01,120 much this is just a mongod DB session 6997 04:59:01,120 --> 04:59:03,760 store for Express now let's say for 6998 04:59:03,760 --> 04:59:05,680 example if you're using some other 6999 04:59:05,680 --> 04:59:07,520 database there are a bunch of different 7000 04:59:07,520 --> 04:59:09,638 session stores right over here so this 7001 04:59:09,638 --> 04:59:11,320 is the express session documentation if 7002 04:59:11,320 --> 04:59:15,600 you just scroll all the way down and you 7003 04:59:15,600 --> 04:59:17,558 scroll down to compatible session stores 7004 04:59:17,558 --> 04:59:19,600 you can see that let's see there is one 7005 04:59:19,600 --> 04:59:23,320 for couchbase DB there's one for uh 7006 04:59:23,320 --> 04:59:26,840 mcash yep connect 7007 04:59:26,840 --> 04:59:29,200 right over here there's one for SQL this 7008 04:59:29,200 --> 04:59:32,280 is the Microsoft SQL Server neo4j redis 7009 04:59:32,280 --> 04:59:35,240 Firebase there are a bunch okay so you 7010 04:59:35,240 --> 04:59:36,760 just have to look for this look in this 7011 04:59:36,760 --> 04:59:38,280 list and find the one that you want to 7012 04:59:38,280 --> 04:59:40,080 use we're just going to use connect 7013 04:59:40,080 --> 04:59:41,680 mango for now so let's just first 7014 04:59:41,680 --> 04:59:44,320 install connect so inside my 7015 04:59:44,320 --> 04:59:46,920 terminal I'll type npmi 7016 04:59:46,920 --> 04:59:49,920 connect 7017 04:59:50,400 --> 04:59:52,480 and of course you must make sure 7018 04:59:52,480 --> 04:59:54,878 you have already a database connection 7019 04:59:54,878 --> 04:59:56,920 so in this case since we're using 7020 04:59:56,920 --> 04:59:58,760 I can actually just reuse this database 7021 04:59:58,760 --> 05:00:00,440 connection so now that we've installed 7022 05:00:00,440 --> 05:00:03,040 connect we can import that and 7023 05:00:03,040 --> 05:00:07,240 into our index file so I'll import 7024 05:00:07,240 --> 05:00:10,080 store from connect just like that 7025 05:00:10,080 --> 05:00:12,280 and then we need to go down into our 7026 05:00:12,280 --> 05:00:15,400 session middleware and we need to set 7027 05:00:15,400 --> 05:00:18,360 this store property so where you're 7028 05:00:18,360 --> 05:00:20,760 pretty much calling this session 7029 05:00:20,760 --> 05:00:23,680 function and then you want to reference 7030 05:00:23,680 --> 05:00:26,558 store. 7031 05:00:26,558 --> 05:00:30,440 create and since we are using we 7032 05:00:30,440 --> 05:00:33,600 can actually reuse that connection so 7033 05:00:33,600 --> 05:00:35,638 there's this property called client in 7034 05:00:35,638 --> 05:00:38,638 the connect options like this and 7035 05:00:38,638 --> 05:00:40,600 then you can reference which which 7036 05:00:40,600 --> 05:00:43,280 we have imported up top over here and 7037 05:00:43,280 --> 05:00:44,718 then you can reference connection and 7038 05:00:44,718 --> 05:00:47,840 then you can call this gets client 7039 05:00:47,840 --> 05:00:50,520 method just like that okay and this says 7040 05:00:50,520 --> 05:00:52,240 Returns the mongodb driver client 7041 05:00:52,240 --> 05:00:54,120 instance that this connection uses to 7042 05:00:54,120 --> 05:00:55,400 talk to mongod 7043 05:00:55,400 --> 05:00:58,558 DB okay so let's go ahead and start up 7044 05:00:58,558 --> 05:01:00,440 the server again and let's just make 7045 05:01:00,440 --> 05:01:03,120 sure everything is good okay so we're 7046 05:01:03,120 --> 05:01:05,760 connected to the database and let's 7047 05:01:05,760 --> 05:01:06,920 actually try 7048 05:01:06,920 --> 05:01:09,480 to authenticate now because that's what 7049 05:01:09,480 --> 05:01:12,638 actually modifies the 7050 05:01:12,638 --> 05:01:15,840 session so let's go ahead and make an 7051 05:01:15,840 --> 05:01:17,558 API 7052 05:01:17,558 --> 05:01:21,040 request to API let's send the 7053 05:01:21,040 --> 05:01:22,638 username 7054 05:01:22,638 --> 05:01:24,760 Johnny and then 7055 05:01:24,760 --> 05:01:26,320 password 7056 05:01:26,320 --> 05:01:29,520 l123 so we are logged in successfully no 7057 05:01:29,520 --> 05:01:32,440 errors in the console okay that's good 7058 05:01:32,440 --> 05:01:34,240 now let's go into our database and let's 7059 05:01:34,240 --> 05:01:37,000 see what happens okay so I'm going to 7060 05:01:37,000 --> 05:01:40,558 refresh and you notice how now there is 7061 05:01:40,558 --> 05:01:42,400 this sessions 7062 05:01:42,400 --> 05:01:45,120 collection okay notice how now there's a 7063 05:01:45,120 --> 05:01:46,798 sessions collection let me expand this 7064 05:01:46,798 --> 05:01:50,680 real quick and you can kind of see how 7065 05:01:50,680 --> 05:01:52,760 we have uh let me see if I can kind of 7066 05:01:52,760 --> 05:01:54,760 do this so you can see it 7067 05:01:54,760 --> 05:01:58,200 better okay so you see how we have this 7068 05:01:58,200 --> 05:02:00,600 sessions collection right of here so now 7069 05:02:00,600 --> 05:02:03,440 we're actually storing the session data 7070 05:02:03,440 --> 05:02:06,400 in mongod DB in our database so then 7071 05:02:06,400 --> 05:02:08,760 what happens is 7072 05:02:08,760 --> 05:02:12,080 now I'm logged in okay I just logged in 7073 05:02:12,080 --> 05:02:14,200 I'm going to make a get request to the 7074 05:02:14,200 --> 05:02:16,680 o/ status endpoint to verify that I am 7075 05:02:16,680 --> 05:02:20,160 logged in which I am okay so we're good 7076 05:02:20,160 --> 05:02:22,320 now watch this the problem that I 7077 05:02:22,320 --> 05:02:24,600 mentioned earlier was that if I were to 7078 05:02:24,600 --> 05:02:26,400 close the server so I'm going to exit 7079 05:02:26,400 --> 05:02:29,638 the server and if I restart it it would 7080 05:02:29,638 --> 05:02:31,240 log us out because all of the session 7081 05:02:31,240 --> 05:02:33,958 data was saved in memory but because now 7082 05:02:33,958 --> 05:02:36,000 that we actually have a session store 7083 05:02:36,000 --> 05:02:37,638 that is a 7084 05:02:37,638 --> 05:02:40,718 database it will use the database to 7085 05:02:40,718 --> 05:02:44,440 restore the session data so watch this 7086 05:02:44,440 --> 05:02:46,320 okay so I have my session data stored in 7087 05:02:46,320 --> 05:02:50,040 the database and notice how if I make a 7088 05:02:50,040 --> 05:02:54,120 get request to the off/ status 7089 05:02:54,120 --> 05:02:57,958 endpoint notice how I am still logged in 7090 05:02:57,958 --> 05:02:59,920 okay if I remove this store completely 7091 05:02:59,920 --> 05:03:01,680 it's going to use the memory store by 7092 05:03:01,680 --> 05:03:05,120 default okay and let's go ahead and 7093 05:03:05,120 --> 05:03:07,480 restart the 7094 05:03:07,480 --> 05:03:10,120 server click send notice how now I am 7095 05:03:10,120 --> 05:03:11,958 unauthorized because my session data is 7096 05:03:11,958 --> 05:03:14,200 not found in memory because it's using 7097 05:03:14,200 --> 05:03:17,000 the inmemory stored by default so I I 7098 05:03:17,000 --> 05:03:18,480 really hope this makes sense and I I 7099 05:03:18,480 --> 05:03:20,280 hope this showcases how important a 7100 05:03:20,280 --> 05:03:22,520 session store is because now instead of 7101 05:03:22,520 --> 05:03:24,958 having your session data stored on 7102 05:03:24,958 --> 05:03:27,200 stored in memory it stores it in a 7103 05:03:27,200 --> 05:03:30,718 database which is great for persistence 7104 05:03:30,718 --> 05:03:32,040 okay so you can restart your server how 7105 05:03:32,040 --> 05:03:34,120 many times as you want 7106 05:03:34,120 --> 05:03:36,240 want the session data will always be 7107 05:03:36,240 --> 05:03:37,920 restored so notice how now if I just 7108 05:03:37,920 --> 05:03:40,000 call this endpoint again after just 7109 05:03:40,000 --> 05:03:43,280 uncommenting out this part the store 7110 05:03:43,280 --> 05:03:46,840 options now we see our data so what 7111 05:03:46,840 --> 05:03:49,360 happens underneath the hood is by 7112 05:03:49,360 --> 05:03:51,520 configuring that session store it will 7113 05:03:51,520 --> 05:03:54,080 basically look inside the sessions 7114 05:03:54,080 --> 05:03:55,840 document or inside the sessions 7115 05:03:55,840 --> 05:03:59,718 collection and it'll search for uh this 7116 05:03:59,718 --> 05:04:01,200 session ID right over here so if you 7117 05:04:01,200 --> 05:04:05,558 look right over here wrg K if I kind of 7118 05:04:05,558 --> 05:04:07,558 show you the cookies right over here let 7119 05:04:07,558 --> 05:04:11,120 me see if I can find it you see how this 7120 05:04:11,120 --> 05:04:13,440 is my session ID right over here and I 7121 05:04:13,440 --> 05:04:15,878 can even log it too I think I may be 7122 05:04:15,878 --> 05:04:17,440 logging 7123 05:04:17,440 --> 05:04:23,480 already nope let me log it right 7124 05:04:25,360 --> 05:04:27,760 over let's 7125 05:04:27,760 --> 05:04:30,958 see let me go 7126 05:04:30,958 --> 05:04:33,638 into let me go back to to the status 7127 05:04:33,638 --> 05:04:36,520 endpoint and let me just log 7128 05:04:36,520 --> 05:04:39,000 request session 7129 05:04:39,000 --> 05:04:42,638 ID okay and if I make a request 7130 05:04:42,638 --> 05:04:45,080 again you're going see that we have that 7131 05:04:45,080 --> 05:04:46,958 that's our session ID right there and 7132 05:04:46,958 --> 05:04:49,718 notice how that session ID is the same 7133 05:04:49,718 --> 05:04:52,400 ID that's in the our document right over 7134 05:04:52,400 --> 05:04:55,160 here so what happens underneath the hood 7135 05:04:55,160 --> 05:04:56,798 is when we send the request to the 7136 05:04:56,798 --> 05:04:58,600 server remember we're sending the cookie 7137 05:04:58,600 --> 05:04:59,798 back to the 7138 05:04:59,798 --> 05:05:01,798 server right our cookie is right over 7139 05:05:01,798 --> 05:05:04,120 here so that gets pars on the server and 7140 05:05:04,120 --> 05:05:06,360 then what happens is instead of looking 7141 05:05:06,360 --> 05:05:09,360 for the session data in the memory store 7142 05:05:09,360 --> 05:05:12,558 it'll look in our mongodb database which 7143 05:05:12,558 --> 05:05:15,760 is persistent and that's how it will 7144 05:05:15,760 --> 05:05:17,760 take care of looking for the session 7145 05:05:17,760 --> 05:05:20,040 data sorry about clicking all this stuff 7146 05:05:20,040 --> 05:05:23,360 let me click over here and show you the 7147 05:05:23,360 --> 05:05:25,878 the mongodb compass client so it'll look 7148 05:05:25,878 --> 05:05:28,040 for the ID okay and then it looks for 7149 05:05:28,040 --> 05:05:29,760 the session property and it will 7150 05:05:29,760 --> 05:05:32,558 basically take this whole string of 7151 05:05:32,558 --> 05:05:35,760 ified object parse into Json and then 7152 05:05:35,760 --> 05:05:40,760 attach this object to that request. 7153 05:05:40,760 --> 05:05:43,558 session object which is what you see 7154 05:05:43,558 --> 05:05:47,000 right over here okay and notice how we 7155 05:05:47,000 --> 05:05:49,480 have the password data right over 7156 05:05:49,480 --> 05:05:52,958 here that's right over here and those 7157 05:05:52,958 --> 05:05:54,760 how this is right over here as well the 7158 05:05:54,760 --> 05:05:57,440 user ID so everything is in the database 7159 05:05:57,440 --> 05:05:59,200 now which is great so now since we're on 7160 05:05:59,200 --> 05:06:00,920 the topic of session stores I want to 7161 05:06:00,920 --> 05:06:03,200 revisit these two properties save 7162 05:06:03,200 --> 05:06:06,878 uninitialized and resave for uh the 7163 05:06:06,878 --> 05:06:09,558 session configuration because I 7164 05:06:09,558 --> 05:06:11,240 mentioned this and I told you all not to 7165 05:06:11,240 --> 05:06:12,520 worry about it so much until we got to 7166 05:06:12,520 --> 05:06:15,400 session stores so right now we have the 7167 05:06:15,400 --> 05:06:18,600 sets of false okay so what this means is 7168 05:06:18,600 --> 05:06:20,958 uh only when you modify that session 7169 05:06:20,958 --> 05:06:24,240 data object then it will actually save 7170 05:06:24,240 --> 05:06:27,320 this to the session store okay so in our 7171 05:06:27,320 --> 05:06:29,638 case right now when we authenticate 7172 05:06:29,638 --> 05:06:32,000 using passport passport will modify the 7173 05:06:32,000 --> 05:06:34,558 session data object for us which means 7174 05:06:34,558 --> 05:06:37,120 that it will also save it to the session 7175 05:06:37,120 --> 05:06:39,040 store which is actually what you see 7176 05:06:39,040 --> 05:06:43,240 happening right over here okay so when 7177 05:06:43,240 --> 05:06:46,000 you set save an initialize true it's 7178 05:06:46,000 --> 05:06:47,760 going to save every single session 7179 05:06:47,760 --> 05:06:50,400 object to your session store even if you 7180 05:06:50,400 --> 05:06:52,958 didn't modify the session at all so I'll 7181 05:06:52,958 --> 05:06:55,160 show you I'll show you an example okay 7182 05:06:55,160 --> 05:06:56,920 so right now what I'll do let me just 7183 05:06:56,920 --> 05:06:59,558 delete this session from the database so 7184 05:06:59,558 --> 05:07:01,718 that means I'm no longer authenticated 7185 05:07:01,718 --> 05:07:02,760 you can see right now if I try to make 7186 05:07:02,760 --> 05:07:05,440 the get request it's going to say I'm 7187 05:07:05,440 --> 05:07:08,480 unauthorized okay CU I don't have that 7188 05:07:08,480 --> 05:07:10,360 session data stored on the service side 7189 05:07:10,360 --> 05:07:12,480 now but what I'm going to do is I'm 7190 05:07:12,480 --> 05:07:13,638 going to go ahead and set save 7191 05:07:13,638 --> 05:07:16,520 uninitialized the true and I'll just 7192 05:07:16,520 --> 05:07:20,558 visit any random endpoint so I can visit 7193 05:07:20,558 --> 05:07:24,000 uh let's see I'll visit SL API off/ 7194 05:07:24,000 --> 05:07:26,760 status and let me also clear my cookies 7195 05:07:26,760 --> 05:07:29,600 as well before I do 7196 05:07:29,600 --> 05:07:32,360 this just so that we are at a clean 7197 05:07:32,360 --> 05:07:34,840 state so I'm going to make a request to 7198 05:07:34,840 --> 05:07:38,718 API status okay and notice how it gives 7199 05:07:38,718 --> 05:07:40,520 us back a 7200 05:07:40,520 --> 05:07:43,440 cookie and notice how now in the session 7201 05:07:43,440 --> 05:07:45,840 store you see how it's saving this 7202 05:07:45,840 --> 05:07:47,718 session data to the database to the 7203 05:07:47,718 --> 05:07:50,878 session store even though we never 7204 05:07:50,878 --> 05:07:52,638 modified the session data at all and you 7205 05:07:52,638 --> 05:07:55,600 can tell because uh let me show you the 7206 05:07:55,600 --> 05:07:58,240 logs okay you can see that we have the 7207 05:07:58,240 --> 05:07:59,760 session data and we have this 7208 05:07:59,760 --> 05:08:01,638 cookie so it's going to send us that 7209 05:08:01,638 --> 05:08:03,798 cookie back but we don't have anything 7210 05:08:03,798 --> 05:08:07,040 related to the user at all so if I try 7211 05:08:07,040 --> 05:08:09,558 to revisit this endpoint 7212 05:08:09,558 --> 05:08:12,480 again if I refresh I'm still going to 7213 05:08:12,480 --> 05:08:14,558 use this but notice how if I clear the 7214 05:08:14,558 --> 05:08:17,440 cookies now if I click if I send a 7215 05:08:17,440 --> 05:08:19,400 request again it's going to create 7216 05:08:19,400 --> 05:08:23,200 another session uh data in the database 7217 05:08:23,200 --> 05:08:24,360 and then I can go ahead and just clear 7218 05:08:24,360 --> 05:08:26,558 the cookie again and it's going to 7219 05:08:26,558 --> 05:08:28,558 create one again so even though I'm not 7220 05:08:28,558 --> 05:08:30,160 doing anything but just trying to visit 7221 05:08:30,160 --> 05:08:32,760 an endpoint it creates a session 7222 05:08:32,760 --> 05:08:35,520 record for us okay and this really 7223 05:08:35,520 --> 05:08:38,000 depends on how you want to implement 7224 05:08:38,000 --> 05:08:40,760 your application sometimes this might be 7225 05:08:40,760 --> 05:08:42,760 useful but you can start to see that 7226 05:08:42,760 --> 05:08:44,840 this is not necessarily a good thing 7227 05:08:44,840 --> 05:08:47,718 because um it's it's just going to save 7228 05:08:47,718 --> 05:08:49,638 a bunch of unmodified session data to 7229 05:08:49,638 --> 05:08:51,320 your database and that could use up a 7230 05:08:51,320 --> 05:08:55,040 lot of storage so it's better to only 7231 05:08:55,040 --> 05:08:56,680 save the session data when it's been 7232 05:08:56,680 --> 05:08:59,400 modified so in cases where the user logs 7233 05:08:59,400 --> 05:09:01,200 in passport will actually modify that 7234 05:09:01,200 --> 05:09:03,360 session data and then it will save the 7235 05:09:03,360 --> 05:09:06,000 session data to the database so let's 7236 05:09:06,000 --> 05:09:08,360 take a look at the resave option now so 7237 05:09:08,360 --> 05:09:10,680 currently it sets a false uh so I'm 7238 05:09:10,680 --> 05:09:12,120 going to set it to 7239 05:09:12,120 --> 05:09:15,840 true and currently I have dropped all of 7240 05:09:15,840 --> 05:09:18,718 my sessions in the database but what 7241 05:09:18,718 --> 05:09:20,400 I'll do is I'll make a request a get 7242 05:09:20,400 --> 05:09:22,160 request to this status 7243 05:09:22,160 --> 05:09:24,400 endpoint okay that's fine we do get back 7244 05:09:24,400 --> 05:09:26,360 a cookie though which is which is fine 7245 05:09:26,360 --> 05:09:29,680 as well if I refresh I can see that my 7246 05:09:29,680 --> 05:09:32,718 session data is over here so what save 7247 05:09:32,718 --> 05:09:34,760 really does is it pretty much just 7248 05:09:34,760 --> 05:09:38,440 forces this cookie to be resaved every 7249 05:09:38,440 --> 05:09:41,000 single time so you notice how right now 7250 05:09:41,000 --> 05:09:43,400 let's pay attention to this date right 7251 05:09:43,400 --> 05:09:45,760 over here this date string okay notice 7252 05:09:45,760 --> 05:09:48,520 how every time I make a request it's 7253 05:09:48,520 --> 05:09:51,600 going to go ahead and update this time 7254 05:09:51,600 --> 05:09:54,798 right over here okay so if I keep 7255 05:09:54,798 --> 05:09:56,680 refreshing it's basically just going to 7256 05:09:56,680 --> 05:10:00,558 keep updating that expiration date okay 7257 05:10:00,558 --> 05:10:03,600 so it Reaves it every single time if I 7258 05:10:03,600 --> 05:10:05,638 set it back to 7259 05:10:05,638 --> 05:10:08,160 false and let's go ahead and let me just 7260 05:10:08,160 --> 05:10:11,160 refresh real quick so pay attention to 7261 05:10:11,160 --> 05:10:15,040 this time so 14 43 39 so you'll notice 7262 05:10:15,040 --> 05:10:17,280 how if I click Send again and I if I 7263 05:10:17,280 --> 05:10:19,638 refresh notice how the date does not 7264 05:10:19,638 --> 05:10:21,680 actually get modified because we're not 7265 05:10:21,680 --> 05:10:25,558 forcing that cookie to be updated to be 7266 05:10:25,558 --> 05:10:28,920 resaved every single time even though 7267 05:10:28,920 --> 05:10:31,718 there's no changes happening at all now 7268 05:10:31,718 --> 05:10:34,558 if I actually try to log in so watch 7269 05:10:34,558 --> 05:10:35,798 this I'm going to go ahead and try to 7270 05:10:35,798 --> 05:10:39,240 log in now so I am logged in and I think 7271 05:10:39,240 --> 05:10:42,520 I am using the same cookie as 7272 05:10:42,520 --> 05:10:46,120 well uh let's see maybe not let me 7273 05:10:46,120 --> 05:10:48,638 refresh oh okay so here's what happened 7274 05:10:48,638 --> 05:10:51,718 okay so it actually replaced that 7275 05:10:51,718 --> 05:10:54,360 session ID that we previously had with 7276 05:10:54,360 --> 05:10:56,240 this one so notice how when I refreshed 7277 05:10:56,240 --> 05:10:58,480 we have this session ID and you'll 7278 05:10:58,480 --> 05:11:01,520 notice that now everything got updated 7279 05:11:01,520 --> 05:11:05,280 and it modified the session so typically 7280 05:11:05,280 --> 05:11:07,920 when you do modify the session it will 7281 05:11:07,920 --> 05:11:10,240 actually update the cookie as well which 7282 05:11:10,240 --> 05:11:12,040 is what you see over here you can see 7283 05:11:12,040 --> 05:11:13,480 that I don't have the same cookie 7284 05:11:13,480 --> 05:11:20,200 anymore as before okay um let me see yep 7285 05:11:20,200 --> 05:11:22,680 that's fine so yeah hopefully the resave 7286 05:11:22,680 --> 05:11:25,200 part makes sense now typically ideally 7287 05:11:25,200 --> 05:11:26,600 you would want this set to false and you 7288 05:11:26,600 --> 05:11:29,120 would also want save an initialized set 7289 05:11:29,120 --> 05:11:31,240 to false as well but it also could 7290 05:11:31,240 --> 05:11:32,760 depend on 7291 05:11:32,760 --> 05:11:34,480 when it's useful like for example if you 7292 05:11:34,480 --> 05:11:36,360 have it set to true that could mean that 7293 05:11:36,360 --> 05:11:37,680 someone just visited your website 7294 05:11:37,680 --> 05:11:40,120 they're performing some kind of uh 7295 05:11:40,120 --> 05:11:41,920 operations maybe they're adding products 7296 05:11:41,920 --> 05:11:44,680 to a cart but then once they log in you 7297 05:11:44,680 --> 05:11:46,440 want to persist that session data as 7298 05:11:46,440 --> 05:11:49,000 well so that way it's not gone so once 7299 05:11:49,000 --> 05:11:52,400 they log in they have their cart all set 7300 05:11:52,400 --> 05:11:54,280 up already even though they did that 7301 05:11:54,280 --> 05:11:55,320 when they were a guest on the 7302 05:11:55,320 --> 05:11:59,840 application so hopefully that makes 7303 05:12:00,878 --> 05:12:03,878 sense 7304 05:12:03,958 --> 05:12:05,360 all right so in this part of the 7305 05:12:05,360 --> 05:12:07,160 expressjs tutorial I'm going to show you 7306 05:12:07,160 --> 05:12:10,680 how to set up ooth 2 using passportjs so 7307 05:12:10,680 --> 05:12:12,878 this will allow you to use third-party 7308 05:12:12,878 --> 05:12:15,558 providers such as Discord or Facebook to 7309 05:12:15,558 --> 05:12:17,600 log into your application rather than 7310 05:12:17,600 --> 05:12:19,760 logging in using just local username and 7311 05:12:19,760 --> 05:12:22,520 password so I will be using Discord as 7312 05:12:22,520 --> 05:12:24,718 my third party provider to allow myself 7313 05:12:24,718 --> 05:12:27,120 to log into my application using Discord 7314 05:12:27,120 --> 05:12:29,000 but as long as you understand what I am 7315 05:12:29,000 --> 05:12:30,680 doing throughout this tutorial and I 7316 05:12:30,680 --> 05:12:32,120 will be explaining everything that is 7317 05:12:32,120 --> 05:12:33,600 going on you should have no problem 7318 05:12:33,600 --> 05:12:35,680 following along using whatever third 7319 05:12:35,680 --> 05:12:37,520 party provider you want so even though 7320 05:12:37,520 --> 05:12:40,280 I'm using Discord you can use literally 7321 05:12:40,280 --> 05:12:42,480 any other provider such as GitHub or 7322 05:12:42,480 --> 05:12:43,958 Facebook the only difference is that you 7323 05:12:43,958 --> 05:12:45,798 just need to make sure you figure out 7324 05:12:45,798 --> 05:12:48,160 where to create the actual ooth 7325 05:12:48,160 --> 05:12:49,878 application and you can easily find that 7326 05:12:49,878 --> 05:12:51,718 out by just going to the developer 7327 05:12:51,718 --> 05:12:53,718 documentation they typically provide 7328 05:12:53,718 --> 05:12:55,240 links where you can read up on how to 7329 05:12:55,240 --> 05:12:57,160 create an actual oo app but like I said 7330 05:12:57,160 --> 05:12:58,680 as long as you understand what is going 7331 05:12:58,680 --> 05:13:00,958 on with setting up our Discord oath 7332 05:13:00,958 --> 05:13:03,080 application and grabbing the required 7333 05:13:03,080 --> 05:13:04,680 values then everything should be fine 7334 05:13:04,680 --> 05:13:06,320 and you should be able to integrate this 7335 05:13:06,320 --> 05:13:08,638 with any third party provider so the 7336 05:13:08,638 --> 05:13:11,160 first thing that I am going to do is go 7337 05:13:11,160 --> 05:13:13,680 to my thirdparty providers website so 7338 05:13:13,680 --> 05:13:15,558 discord.com so and I'm going to go ahead 7339 05:13:15,558 --> 05:13:17,200 and scroll down to the developer 7340 05:13:17,200 --> 05:13:18,920 documentation section so if you're using 7341 05:13:18,920 --> 05:13:20,840 Twitter for example you would go to 7342 05:13:20,840 --> 05:13:24,240 twitter.com and search for the developer 7343 05:13:24,240 --> 05:13:26,520 section okay I'm going to click on get 7344 05:13:26,520 --> 05:13:30,120 started now and so now I want to look 7345 05:13:30,120 --> 05:13:31,280 for the section where it says 7346 05:13:31,280 --> 05:13:34,000 application so right over here 7347 05:13:34,000 --> 05:13:35,958 applications you want to make sure you 7348 05:13:35,958 --> 05:13:37,680 create a new 7349 05:13:37,680 --> 05:13:39,480 application so I'm going to go ahead and 7350 05:13:39,480 --> 05:13:43,718 call this let's do ooth to 7351 05:13:43,718 --> 05:13:46,558 Discord oh whoops o I guess let's do 7352 05:13:46,558 --> 05:13:49,558 Anon oo 7353 05:13:49,600 --> 05:13:52,638 2 so I've just created my application 7354 05:13:52,638 --> 05:13:54,200 and whatever provider you're using will 7355 05:13:54,200 --> 05:13:56,400 prompt you to do the same so now what 7356 05:13:56,400 --> 05:13:58,920 I'll do is I'm going to go to the oath 2 7357 05:13:58,920 --> 05:14:01,080 section and there's a couple of fields 7358 05:14:01,080 --> 05:14:03,958 that we need to get we need the client 7359 05:14:03,958 --> 05:14:06,878 ID and then we also need the client 7360 05:14:06,878 --> 05:14:09,160 secret and let me just enter my tofa 7361 05:14:09,160 --> 05:14:10,558 code real quick and get back to you all 7362 05:14:10,558 --> 05:14:12,360 all right so I was just able to get my 7363 05:14:12,360 --> 05:14:14,120 client secret you want to make sure that 7364 05:14:14,120 --> 05:14:15,840 you keep this client secret private you 7365 05:14:15,840 --> 05:14:17,958 do not want anyone to know what this 7366 05:14:17,958 --> 05:14:20,000 secret is so I'm going to copy that and 7367 05:14:20,000 --> 05:14:21,440 just for now I'm going to just paste 7368 05:14:21,440 --> 05:14:22,760 this right over here now just for 7369 05:14:22,760 --> 05:14:24,920 tutorial purposes it's okay that you are 7370 05:14:24,920 --> 05:14:26,320 seeing this client secret cuz I'm going 7371 05:14:26,320 --> 05:14:28,320 to reset it anyways and let me just go 7372 05:14:28,320 --> 05:14:29,878 ahead and copy the client ID now this 7373 05:14:29,878 --> 05:14:31,320 client ID is not something that you need 7374 05:14:31,320 --> 05:14:33,000 to worry about anyone can have it 7375 05:14:33,000 --> 05:14:34,200 doesn't really do anything other than 7376 05:14:34,200 --> 05:14:36,638 just identify the actual applications ID 7377 05:14:36,638 --> 05:14:38,120 so it's okay if someone else sees the 7378 05:14:38,120 --> 05:14:39,760 client ID now the other thing that we 7379 05:14:39,760 --> 05:14:41,718 need is we need to add a redirect so 7380 05:14:41,718 --> 05:14:43,718 this redirect is important because the 7381 05:14:43,718 --> 05:14:45,798 way that the flow is going to work is we 7382 05:14:45,798 --> 05:14:48,040 need to make a request to an endpoint 7383 05:14:48,040 --> 05:14:49,320 that's going to take care of uh 7384 05:14:49,320 --> 05:14:51,558 redirecting us to the third party 7385 05:14:51,558 --> 05:14:52,520 provider that we're trying to 7386 05:14:52,520 --> 05:14:54,558 authenticate with in our case we need to 7387 05:14:54,558 --> 05:14:56,160 set up an endpoint later on that will 7388 05:14:56,160 --> 05:14:58,558 take care of calling passport to 7389 05:14:58,558 --> 05:15:00,840 redirect us to the Discord platform and 7390 05:15:00,840 --> 05:15:03,320 then once the Discord platform shows up 7391 05:15:03,320 --> 05:15:05,878 that authorization page and it shows us 7392 05:15:05,878 --> 05:15:07,360 all the different permissions and the 7393 05:15:07,360 --> 05:15:09,958 Scopes that we want to allow we click 7394 05:15:09,958 --> 05:15:12,000 authorize and then Discord needs to 7395 05:15:12,000 --> 05:15:14,878 redirect us to this redirect URL so 7396 05:15:14,878 --> 05:15:16,558 hopefully that makes sense so that's why 7397 05:15:16,558 --> 05:15:18,320 we need this redirect URL cuz we need to 7398 05:15:18,320 --> 05:15:19,718 tell Discord hey once we're done 7399 05:15:19,718 --> 05:15:21,798 authorizing send us back to this URL so 7400 05:15:21,798 --> 05:15:23,600 I'm going to go ahead and just pass in 7401 05:15:23,600 --> 05:15:26,280 Local Host 43000 because that's what my 7402 05:15:26,280 --> 05:15:29,558 server is running on SL API sloth let's 7403 05:15:29,558 --> 05:15:32,558 do Discord and then redirect now keep in 7404 05:15:32,558 --> 05:15:33,920 mind this endpoint has not been 7405 05:15:33,920 --> 05:15:36,200 implemented yet but we will implement it 7406 05:15:36,200 --> 05:15:38,840 later on now you can add another 7407 05:15:38,840 --> 05:15:41,120 redirect URL if you want this is useful 7408 05:15:41,120 --> 05:15:43,798 if you want to use the same app in 7409 05:15:43,798 --> 05:15:45,440 different environments such as let's say 7410 05:15:45,440 --> 05:15:46,360 uh right now we're currently in 7411 05:15:46,360 --> 05:15:47,718 development mode but let's say you want 7412 05:15:47,718 --> 05:15:49,200 to use this for production as well then 7413 05:15:49,200 --> 05:15:51,160 you can add your production URL which is 7414 05:15:51,160 --> 05:15:53,600 your domain name and then same endpoint 7415 05:15:53,600 --> 05:15:55,000 I'm going to go ahead and just click 7416 05:15:55,000 --> 05:15:56,958 save changes and let me also copy this 7417 05:15:56,958 --> 05:15:58,920 redirect URL cuz we will need this as 7418 05:15:58,920 --> 05:16:01,840 well so we just finished setting up up 7419 05:16:01,840 --> 05:16:04,600 the ooff 2 app for Discord so the next 7420 05:16:04,600 --> 05:16:07,600 thing that we can do is install our 7421 05:16:07,600 --> 05:16:09,638 strategy package so since I'm using 7422 05:16:09,638 --> 05:16:11,240 Discord I want to make sure that I 7423 05:16:11,240 --> 05:16:14,638 install the correct strategy module for 7424 05:16:14,638 --> 05:16:16,558 my project so remember that there are 7425 05:16:16,558 --> 05:16:18,920 plenty of strategies to choose from 7426 05:16:18,920 --> 05:16:20,558 depending on which third party provider 7427 05:16:20,558 --> 05:16:22,200 you want to use so if you're using 7428 05:16:22,200 --> 05:16:24,000 Facebook there's a passport Facebook 7429 05:16:24,000 --> 05:16:26,320 library that you can install if you want 7430 05:16:26,320 --> 05:16:28,280 to use Twitter there's passport Twitter 7431 05:16:28,280 --> 05:16:29,558 there's a bunch you just have to go on 7432 05:16:29,558 --> 05:16:31,878 this website passport js. org and just 7433 05:16:31,878 --> 05:16:34,360 search for these strategies so obviously 7434 05:16:34,360 --> 05:16:35,600 I'm using Discord so I'm going to 7435 05:16:35,600 --> 05:16:37,638 install passport Discord so I'm going to 7436 05:16:37,638 --> 05:16:39,798 go into my windows Powershell and I'll 7437 05:16:39,798 --> 05:16:43,120 type npmi passport hyphen Discord now 7438 05:16:43,120 --> 05:16:45,120 make sure that if you don't have 7439 05:16:45,120 --> 05:16:47,440 passport the base passport package you 7440 05:16:47,440 --> 05:16:49,320 install passport because you need 7441 05:16:49,320 --> 05:16:51,480 passport itself as well I already have 7442 05:16:51,480 --> 05:16:53,280 it already because I did a tutorial 7443 05:16:53,280 --> 05:16:55,080 earlier with this passport itself with 7444 05:16:55,080 --> 05:16:56,200 the local strategy so I'm not going to 7445 05:16:56,200 --> 05:16:58,320 reinstall it I'm going to go ahead and 7446 05:16:58,320 --> 05:17:02,558 hit enter and install passport Discord 7447 05:17:02,558 --> 05:17:03,958 okay we're good to go with that now let 7448 05:17:03,958 --> 05:17:05,718 me just run my server 7449 05:17:05,718 --> 05:17:10,200 again okay there we go so now what we're 7450 05:17:10,200 --> 05:17:12,440 going to do is this so in earlier parts 7451 05:17:12,440 --> 05:17:14,480 of the tutorial I did set a passport 7452 05:17:14,480 --> 05:17:16,120 with a local strategy however I don't 7453 05:17:16,120 --> 05:17:17,040 want to I don't want to make this 7454 05:17:17,040 --> 05:17:19,558 tutorial super complex so what I'm going 7455 05:17:19,558 --> 05:17:22,120 to do is just temporarily I'm going to 7456 05:17:22,120 --> 05:17:24,160 kind of like disable the local strategy 7457 05:17:24,160 --> 05:17:26,000 cuz we really don't want to have these 7458 05:17:26,000 --> 05:17:27,440 two conflict with each other and that's 7459 05:17:27,440 --> 05:17:29,638 kind of like something that is for a 7460 05:17:29,638 --> 05:17:31,878 different video to solve and I don't 7461 05:17:31,878 --> 05:17:34,240 want to make this whole section super 7462 05:17:34,240 --> 05:17:35,520 complicated I'm just going to go ahead 7463 05:17:35,520 --> 05:17:37,320 and comment out the local strategy the 7464 05:17:37,320 --> 05:17:39,638 local strategy file import up top here 7465 05:17:39,638 --> 05:17:42,520 and I'm going to leave the rest of these 7466 05:17:42,520 --> 05:17:44,840 routes alone for now now so what I 7467 05:17:44,840 --> 05:17:47,160 mentioned earlier is if you didn't see 7468 05:17:47,160 --> 05:17:48,878 the part where we set a passport you 7469 05:17:48,878 --> 05:17:50,440 need to make sure you first import 7470 05:17:50,440 --> 05:17:52,440 passport like this from the passport 7471 05:17:52,440 --> 05:17:54,360 package and then right down over here 7472 05:17:54,360 --> 05:17:56,000 you want to make sure you initialize 7473 05:17:56,000 --> 05:17:58,240 passport by calling app.use and then 7474 05:17:58,240 --> 05:18:00,240 passing in passport. initialize like 7475 05:18:00,240 --> 05:18:01,680 this and then you also want want to 7476 05:18:01,680 --> 05:18:03,840 configure passport to work with Express 7477 05:18:03,840 --> 05:18:06,240 session so we just call app.use and pass 7478 05:18:06,240 --> 05:18:08,160 and passport. session and that's 7479 05:18:08,160 --> 05:18:09,638 literally all you need to do the rest of 7480 05:18:09,638 --> 05:18:11,520 the things that we'll do for our Discord 7481 05:18:11,520 --> 05:18:13,360 strategy will be pretty straightforward 7482 05:18:13,360 --> 05:18:14,840 and doesn't depend on anything else okay 7483 05:18:14,840 --> 05:18:16,440 so now that we got that out of the way 7484 05:18:16,440 --> 05:18:19,280 let's go ahead and create our Discord 7485 05:18:19,280 --> 05:18:21,920 strategy much similar to how we created 7486 05:18:21,920 --> 05:18:23,520 our local 7487 05:18:23,520 --> 05:18:25,558 strategy so I'm going to go ahead and 7488 05:18:25,558 --> 05:18:26,920 create a new file and I'll call this 7489 05:18:26,920 --> 05:18:28,718 Discord hyen 7490 05:18:28,718 --> 05:18:30,320 strategy. 7491 05:18:30,320 --> 05:18:31,920 MJS 7492 05:18:31,920 --> 05:18:35,160 and I'm going to import inside this file 7493 05:18:35,160 --> 05:18:37,958 passport from passport and I'm going to 7494 05:18:37,958 --> 05:18:40,360 import this strategy class and the 7495 05:18:40,360 --> 05:18:43,760 strategy class actually comes 7496 05:18:43,920 --> 05:18:46,760 from passport 7497 05:18:46,760 --> 05:18:49,160 Discord now of course if you're using a 7498 05:18:49,160 --> 05:18:51,638 different passport strategy all the 7499 05:18:51,638 --> 05:18:53,600 passport strategies have this strategy 7500 05:18:53,600 --> 05:18:55,400 class so if you're using passport 7501 05:18:55,400 --> 05:18:56,878 Facebook you can still import this 7502 05:18:56,878 --> 05:18:58,120 strategy class it's going to be in every 7503 05:18:58,120 --> 05:19:01,760 single strategy module it's standard 7504 05:19:01,760 --> 05:19:03,558 so now what we need to do we need to go 7505 05:19:03,558 --> 05:19:08,040 ahead and call passport. use and pass in 7506 05:19:08,040 --> 05:19:13,240 an instance of this strategy class like 7507 05:19:13,240 --> 05:19:17,520 this and now we need to pass in options 7508 05:19:17,520 --> 05:19:20,280 for strategy and we also need to pass in 7509 05:19:20,280 --> 05:19:23,440 the verify function so I'll handle the 7510 05:19:23,440 --> 05:19:25,840 strategy options first so since we are 7511 05:19:25,840 --> 05:19:29,160 using oo 2 we need to make sure we 7512 05:19:29,160 --> 05:19:32,520 configure the oo 2 to have it point to 7513 05:19:32,520 --> 05:19:34,240 our Discord application that we just 7514 05:19:34,240 --> 05:19:36,360 created so remember how earlier I 7515 05:19:36,360 --> 05:19:38,240 grabbed the client Seeker the client ID 7516 05:19:38,240 --> 05:19:40,558 and the redirect URL well all that is 7517 05:19:40,558 --> 05:19:42,680 going to be passed into this object 7518 05:19:42,680 --> 05:19:44,958 right over here for the strategy options 7519 05:19:44,958 --> 05:19:47,000 so first we'll set up the client ID so 7520 05:19:47,000 --> 05:19:48,320 you can see there's this property that I 7521 05:19:48,320 --> 05:19:50,600 can assign a value and I'm going to go 7522 05:19:50,600 --> 05:19:52,680 ahead and assign it the value of my 7523 05:19:52,680 --> 05:19:55,600 client ID and of course like I said if 7524 05:19:55,600 --> 05:19:57,520 you're using Facebook or Twitter or 7525 05:19:57,520 --> 05:19:59,958 whatever the oath app that you create on 7526 05:19:59,958 --> 05:20:02,280 those platforms will have a client ID 7527 05:20:02,280 --> 05:20:04,760 it's standard across any platform that 7528 05:20:04,760 --> 05:20:08,320 implements o 2 okay and then it'll also 7529 05:20:08,320 --> 05:20:09,440 have the client 7530 05:20:09,440 --> 05:20:11,558 secret so I'm going to grab my client 7531 05:20:11,558 --> 05:20:13,600 secret and paste it over here keep in 7532 05:20:13,600 --> 05:20:15,120 mind that just for tutorial purposes I 7533 05:20:15,120 --> 05:20:16,878 am hardcoding all this ideally you would 7534 05:20:16,878 --> 05:20:19,160 want to place these values in an 7535 05:20:19,160 --> 05:20:20,878 environment variable so I'll recommend 7536 05:20:20,878 --> 05:20:22,280 doing that but just for tutorial 7537 05:20:22,280 --> 05:20:23,760 purposes it's okay I'm going to go ahead 7538 05:20:23,760 --> 05:20:25,558 and now copy the redirect URL cuz we 7539 05:20:25,558 --> 05:20:27,680 will need that and the field name is 7540 05:20:27,680 --> 05:20:29,920 actually callback URL so when I say 7541 05:20:29,920 --> 05:20:32,360 callback URL or redirect URL I use those 7542 05:20:32,360 --> 05:20:33,878 terms synonymously okay so we're going 7543 05:20:33,878 --> 05:20:36,440 to paste that URL this is the Callback 7544 05:20:36,440 --> 05:20:38,320 URL that Discord or whatever provider 7545 05:20:38,320 --> 05:20:41,040 will make a request too upon success 7546 05:20:41,040 --> 05:20:43,320 upon authorization success now there's 7547 05:20:43,320 --> 05:20:45,320 one more field that we need to configure 7548 05:20:45,320 --> 05:20:47,360 and that's the scope field and this is 7549 05:20:47,360 --> 05:20:49,320 important because we need to actually 7550 05:20:49,320 --> 05:20:51,400 State what permissions that we want to 7551 05:20:51,400 --> 05:20:52,840 have what Fields what type of 7552 05:20:52,840 --> 05:20:54,200 information that we want to access and 7553 05:20:54,200 --> 05:20:56,120 that's defined all in the scope so by 7554 05:20:56,120 --> 05:20:57,440 default if you don't leave the scope at 7555 05:20:57,440 --> 05:20:58,958 all then you won't have really any 7556 05:20:58,958 --> 05:21:01,240 access at all to the user's information 7557 05:21:01,240 --> 05:21:03,200 but if you wanted the users's let's say 7558 05:21:03,200 --> 05:21:05,878 username or their Discord ID then you 7559 05:21:05,878 --> 05:21:08,680 would need to use this identify scope 7560 05:21:08,680 --> 05:21:10,440 now the Scopes are defined differently 7561 05:21:10,440 --> 05:21:12,400 on each platform so for example Discord 7562 05:21:12,400 --> 05:21:14,638 has this identify scope let me see if I 7563 05:21:14,638 --> 05:21:16,360 can kind of show you all through the 7564 05:21:16,360 --> 05:21:19,400 documentation let me see right over 7565 05:21:19,400 --> 05:21:22,320 here okay you see how over here at o off 7566 05:21:22,320 --> 05:21:24,798 two Scopes and you can see that we have 7567 05:21:24,798 --> 05:21:27,280 this identity scope and it tells you 7568 05:21:27,280 --> 05:21:31,040 that what it allows you to access is 7569 05:21:31,040 --> 05:21:34,320 allows you to make a request to this uh 7570 05:21:34,320 --> 05:21:36,440 let me zoom in a little bit it allows 7571 05:21:36,440 --> 05:21:38,680 you to 7572 05:21:38,680 --> 05:21:41,920 access this users at me end point so if 7573 05:21:41,920 --> 05:21:43,440 I click on that it pretty much gives you 7574 05:21:43,440 --> 05:21:45,160 the user itself okay the user that 7575 05:21:45,160 --> 05:21:47,320 authenticated so we need the authentify 7576 05:21:47,320 --> 05:21:48,920 scope to get at least the basic stuff 7577 05:21:48,920 --> 05:21:51,200 like the username the ID of the user 7578 05:21:51,200 --> 05:21:52,920 stuff like that now it also does mention 7579 05:21:52,920 --> 05:21:55,160 that it does not give you the email so 7580 05:21:55,160 --> 05:21:56,878 in order to grab the email you need to 7581 05:21:56,878 --> 05:21:59,000 use the email scope so notice how over 7582 05:21:59,000 --> 05:22:01,680 here there is this me scroll up here 7583 05:22:01,680 --> 05:22:03,558 there's an email scope and it tells you 7584 05:22:03,558 --> 05:22:05,160 right over here in the description it 7585 05:22:05,160 --> 05:22:07,638 enables the same endpoint at users at me 7586 05:22:07,638 --> 05:22:09,200 to return an email so if you want an 7587 05:22:09,200 --> 05:22:10,600 email then you would provide that in the 7588 05:22:10,600 --> 05:22:12,240 scope over here if you wanted to grab 7589 05:22:12,240 --> 05:22:14,480 all the users servers all their guilds 7590 05:22:14,480 --> 05:22:16,440 that they are in then you can use the 7591 05:22:16,440 --> 05:22:18,920 guild scope okay so I'll just do 7592 05:22:18,920 --> 05:22:20,718 identify and I'll 7593 05:22:20,718 --> 05:22:23,798 do guilds for now so we're done with our 7594 05:22:23,798 --> 05:22:26,080 configuration for the strategy options 7595 05:22:26,080 --> 05:22:28,200 this verif function is responsible for 7596 05:22:28,200 --> 05:22:30,638 performing validation on the user that's 7597 05:22:30,638 --> 05:22:32,958 trying to to authenticate now since we 7598 05:22:32,958 --> 05:22:34,920 are using oo 2 and there's no need to 7599 05:22:34,920 --> 05:22:36,200 worry about a password because they're 7600 05:22:36,200 --> 05:22:37,840 using a third party platform they're not 7601 05:22:37,840 --> 05:22:39,520 going to be logging in with a password 7602 05:22:39,520 --> 05:22:41,360 what you would need to do is actually 7603 05:22:41,360 --> 05:22:43,360 check to see if the user exists in the 7604 05:22:43,360 --> 05:22:45,480 database and if they don't you can save 7605 05:22:45,480 --> 05:22:48,000 that record to the database okay so the 7606 05:22:48,000 --> 05:22:49,200 reason why you would want to save a 7607 05:22:49,200 --> 05:22:51,480 third party uh user account to your 7608 05:22:51,480 --> 05:22:53,840 database is sometimes you might have 7609 05:22:53,840 --> 05:22:56,240 certain types of data that you want to 7610 05:22:56,240 --> 05:22:58,080 relate to that user so sometimes you 7611 05:22:58,080 --> 05:23:01,160 might have uh like a one: one or one to 7612 05:23:01,160 --> 05:23:02,920 many relationship that you need to 7613 05:23:02,920 --> 05:23:06,360 associate with the user on your platform 7614 05:23:06,360 --> 05:23:08,280 so it's definitely a good idea to save 7615 05:23:08,280 --> 05:23:10,200 that user to a database so that way the 7616 05:23:10,200 --> 05:23:12,080 next time they log in they can see all 7617 05:23:12,080 --> 05:23:14,000 of their relations with other types of 7618 05:23:14,000 --> 05:23:16,200 data for example the user can see all of 7619 05:23:16,200 --> 05:23:19,200 your posts all of their messages all of 7620 05:23:19,200 --> 05:23:21,798 their um current activity things like 7621 05:23:21,798 --> 05:23:23,440 that okay so it's always a good idea to 7622 05:23:23,440 --> 05:23:25,718 save the user now this verif function 7623 05:23:25,718 --> 05:23:27,638 takes in four arguments the first two 7624 05:23:27,638 --> 05:23:30,120 arguments are both tokens so access 7625 05:23:30,120 --> 05:23:31,920 token and refresh 7626 05:23:31,920 --> 05:23:35,360 token the third argument is the profile 7627 05:23:35,360 --> 05:23:38,360 so the profile itself is going to be 7628 05:23:38,360 --> 05:23:39,840 pretty much the user object and it's 7629 05:23:39,840 --> 05:23:42,680 going to also have the email if you have 7630 05:23:42,680 --> 05:23:44,040 the email scope enabled and it's also 7631 05:23:44,040 --> 05:23:45,600 going to have all the guilds the servers 7632 05:23:45,600 --> 05:23:47,840 that the user in so that's what the 7633 05:23:47,840 --> 05:23:49,240 profile is and like I said if you're 7634 05:23:49,240 --> 05:23:52,120 using Google or Twitter then the profile 7635 05:23:52,120 --> 05:23:55,638 would contain Google details of the user 7636 05:23:55,638 --> 05:23:57,718 same thing with Twitter and that would 7637 05:23:57,718 --> 05:23:59,798 contain stuff like the username email 7638 05:23:59,798 --> 05:24:01,558 address 7639 05:24:01,558 --> 05:24:04,600 ID stuff like that and the last argument 7640 05:24:04,600 --> 05:24:07,000 is the done function which we're going 7641 05:24:07,000 --> 05:24:08,360 to go ahead and call when we're done 7642 05:24:08,360 --> 05:24:09,958 performing the logic that we need so 7643 05:24:09,958 --> 05:24:12,480 that way passport can then move on and 7644 05:24:12,480 --> 05:24:14,120 then take care of the seriation part 7645 05:24:14,120 --> 05:24:16,280 serialization part which I will explain 7646 05:24:16,280 --> 05:24:19,160 how that works so the access token and 7647 05:24:19,160 --> 05:24:21,440 the refresh token these are just uh 7648 05:24:21,440 --> 05:24:23,840 tokens that you're going to be using 7649 05:24:23,840 --> 05:24:27,440 primarily for making uh requests to the 7650 05:24:27,440 --> 05:24:29,878 API on behalf of the user so the access 7651 05:24:29,878 --> 05:24:31,638 token is what you would actually use to 7652 05:24:31,638 --> 05:24:34,200 make API calls so for example for 7653 05:24:34,200 --> 05:24:36,160 Discord in order for you to actually 7654 05:24:36,160 --> 05:24:38,958 even call these endpoints such as uh 7655 05:24:38,958 --> 05:24:43,440 users at me uh users me guilds you need 7656 05:24:43,440 --> 05:24:45,240 a an an authentication token so that's 7657 05:24:45,240 --> 05:24:48,280 kind of like some type of think of it 7658 05:24:48,280 --> 05:24:50,798 like an API key but it's not an API key 7659 05:24:50,798 --> 05:24:52,120 it's an access token and you use that 7660 05:24:52,120 --> 05:24:54,520 access token to be able to access the 7661 05:24:54,520 --> 05:24:56,520 data that you want to retrieve on behalf 7662 05:24:56,520 --> 05:24:59,840 of the authenticated user so for example 7663 05:24:59,840 --> 05:25:02,200 if let's say you want wanted to fetch I 7664 05:25:02,200 --> 05:25:04,840 don't know all of the guilds then you 7665 05:25:04,840 --> 05:25:07,040 would need that access token in our case 7666 05:25:07,040 --> 05:25:08,558 passport actually takes care of that for 7667 05:25:08,558 --> 05:25:11,000 us the passport Discord strategy so we 7668 05:25:11,000 --> 05:25:12,920 would need to manually do it ourselves 7669 05:25:12,920 --> 05:25:14,480 but if there comes up time where you do 7670 05:25:14,480 --> 05:25:17,040 need to do it you need that access token 7671 05:25:17,040 --> 05:25:20,600 the refresh token is just used for 7672 05:25:20,600 --> 05:25:22,958 literally refreshing the access token so 7673 05:25:22,958 --> 05:25:24,000 your access token is actually 7674 05:25:24,000 --> 05:25:27,200 short-lived usually it lasts for about I 7675 05:25:27,200 --> 05:25:28,920 think I don't remember the exact time 7676 05:25:28,920 --> 05:25:30,320 but it is a lot shorter than the refresh 7677 05:25:30,320 --> 05:25:32,718 token and the refresh token is usually a 7678 05:25:32,718 --> 05:25:34,480 lot it lasts a lot longer I think 7679 05:25:34,480 --> 05:25:37,320 usually about 6 months it really depends 7680 05:25:37,320 --> 05:25:39,440 but what you would do is you would store 7681 05:25:39,440 --> 05:25:41,920 these tokens in your database somewhere 7682 05:25:41,920 --> 05:25:43,638 and then you would pretty much use the 7683 05:25:43,638 --> 05:25:45,280 access token to make requests on behalf 7684 05:25:45,280 --> 05:25:48,320 of the user and then when it's time to 7685 05:25:48,320 --> 05:25:49,638 get a new token you would use the 7686 05:25:49,638 --> 05:25:52,558 refresh token to kind of like refresh 7687 05:25:52,558 --> 05:25:55,160 the access token so that way you would 7688 05:25:55,160 --> 05:25:57,520 not need to require the user to re log 7689 05:25:57,520 --> 05:26:00,200 in you just use the refresh token to get 7690 05:26:00,200 --> 05:26:02,040 a new access token and then that new 7691 05:26:02,040 --> 05:26:04,760 access token can be used to retrieve 7692 05:26:04,760 --> 05:26:07,160 data from the API on behalf of the user 7693 05:26:07,160 --> 05:26:08,878 so hopefully that makes sense we're not 7694 05:26:08,878 --> 05:26:10,080 going to be using these two tokens 7695 05:26:10,080 --> 05:26:11,600 though but I just wanted to mention it 7696 05:26:11,600 --> 05:26:13,718 just so that if you're using a whatever 7697 05:26:13,718 --> 05:26:16,120 third party provider you are and then 7698 05:26:16,120 --> 05:26:18,240 you need to actually make API calls then 7699 05:26:18,240 --> 05:26:20,680 you know what value you need to be using 7700 05:26:20,680 --> 05:26:22,840 which is primarily the access token I'm 7701 05:26:22,840 --> 05:26:24,920 just going to go ahead right now and 7702 05:26:24,920 --> 05:26:26,798 console log the profile object because I 7703 05:26:26,798 --> 05:26:29,240 want you to see what this looks like 7704 05:26:29,240 --> 05:26:31,320 after we actually get redirected to 7705 05:26:31,320 --> 05:26:33,878 Discord and click authorize but let's go 7706 05:26:33,878 --> 05:26:36,000 ahead and actually export our passport. 7707 05:26:36,000 --> 05:26:38,958 use call like this export default and 7708 05:26:38,958 --> 05:26:41,600 I'm going to go ahead into my index. MJS 7709 05:26:41,600 --> 05:26:43,440 file and I'm going to go ahead and 7710 05:26:43,440 --> 05:26:47,360 import that strategy like this so import 7711 05:26:47,360 --> 05:26:50,958 strategies Discord strategy. MJS okay so 7712 05:26:50,958 --> 05:26:53,520 that will load up the strategy into our 7713 05:26:53,520 --> 05:26:56,600 entire application and now we need to 7714 05:26:56,600 --> 05:26:58,080 set up an endpoint that the user is 7715 05:26:58,080 --> 05:27:00,240 going to be visiting in order for them 7716 05:27:00,240 --> 05:27:01,200 to 7717 05:27:01,200 --> 05:27:03,280 get redirected to the Discord platform 7718 05:27:03,280 --> 05:27:05,558 so they can authorize themselves with 7719 05:27:05,558 --> 05:27:08,120 our application using Discord so ignore 7720 05:27:08,120 --> 05:27:09,600 all of this stuff right now don't worry 7721 05:27:09,600 --> 05:27:11,320 about any of this we're just going to go 7722 05:27:11,320 --> 05:27:12,840 down over here and I'm going to set up a 7723 05:27:12,840 --> 05:27:15,638 simple app.get request and then we're 7724 05:27:15,638 --> 05:27:18,280 going to name this NPO API 7725 05:27:18,280 --> 05:27:20,600 SL o 7726 05:27:20,600 --> 05:27:24,080 Discord and then we need to pass in that 7727 05:27:24,080 --> 05:27:26,840 passport. authenticate call and then we 7728 05:27:26,840 --> 05:27:28,760 need to specify the strategy name which 7729 05:27:28,760 --> 05:27:30,840 in this case we're using Discord so I'm 7730 05:27:30,840 --> 05:27:34,120 going to pass in Discord like that okay 7731 05:27:34,120 --> 05:27:35,920 now let me show you what's going to 7732 05:27:35,920 --> 05:27:39,320 happen once I go to this endpoint so let 7733 05:27:39,320 --> 05:27:42,878 me go to my browser move this over here 7734 05:27:42,878 --> 05:27:45,520 okay so let me go to SL 7735 05:27:45,520 --> 05:27:47,480 API 7736 05:27:47,480 --> 05:27:50,320 Discord notice how now when I visit that 7737 05:27:50,320 --> 05:27:52,878 URL it redirects us to the Discord 7738 05:27:52,878 --> 05:27:56,120 platform and we can see right over here 7739 05:27:56,120 --> 05:27:57,920 it tells us okay this is the external 7740 05:27:57,920 --> 05:28:00,120 application wants to access your Discord 7741 05:28:00,120 --> 05:28:02,798 account and this is what the application 7742 05:28:02,798 --> 05:28:05,200 wants to access and again all this is 7743 05:28:05,200 --> 05:28:08,600 based on the scope so if I go 7744 05:28:08,600 --> 05:28:11,240 into my strategy you can see that I 7745 05:28:11,240 --> 05:28:13,680 currently have identify in Guilds and it 7746 05:28:13,680 --> 05:28:15,760 says okay identify relates to the 7747 05:28:15,760 --> 05:28:18,600 username Avatar and banner and guilds is 7748 05:28:18,600 --> 05:28:22,400 know what servers you're in if I were to 7749 05:28:22,400 --> 05:28:23,718 add 7750 05:28:23,718 --> 05:28:27,480 email and let's kind of like go back and 7751 05:28:27,480 --> 05:28:29,798 go back here I guess I have to revisit 7752 05:28:29,798 --> 05:28:32,400 the actual 7753 05:28:32,958 --> 05:28:35,558 endpoint notice how now it says access 7754 05:28:35,558 --> 05:28:38,200 your email address okay so hopefully 7755 05:28:38,200 --> 05:28:40,280 that makes sense for now I'll just do 7756 05:28:40,280 --> 05:28:43,718 identify which will just give me the 7757 05:28:43,718 --> 05:28:45,920 basics but like I said if you wanted to 7758 05:28:45,920 --> 05:28:47,600 get the guilds or email address just add 7759 05:28:47,600 --> 05:28:49,360 those in the scope and then you'll be 7760 05:28:49,360 --> 05:28:52,360 good so let me go back and let's go 7761 05:28:52,360 --> 05:28:55,240 ahead and make a call to the endpoint 7762 05:28:55,240 --> 05:28:58,040 and now it only asks for the username 7763 05:28:58,040 --> 05:29:00,600 Avatar and banner and then we'll click 7764 05:29:00,600 --> 05:29:01,440 on 7765 05:29:01,440 --> 05:29:04,280 authorize and now we get this cannot 7766 05:29:04,280 --> 05:29:07,760 gets and notice how the endpoint is our 7767 05:29:07,760 --> 05:29:10,718 redirect URL so we don't have this 7768 05:29:10,718 --> 05:29:12,280 endpoint set up yet so we need to set 7769 05:29:12,280 --> 05:29:14,600 that up so I'm going to go back to the 7770 05:29:14,600 --> 05:29:18,600 index. MJS file and I'll set it up right 7771 05:29:18,600 --> 05:29:20,760 here so it'll also be a get request as 7772 05:29:20,760 --> 05:29:25,000 well so it'll be SL API othis Discord 7773 05:29:25,000 --> 05:29:27,400 redirect SL redirect and then you're 7774 05:29:27,400 --> 05:29:30,280 going to call passport. authenticate and 7775 05:29:30,280 --> 05:29:33,798 and pass in Discord strategy like this 7776 05:29:33,798 --> 05:29:35,320 and then you can pass 7777 05:29:35,320 --> 05:29:38,000 in the request 7778 05:29:38,000 --> 05:29:42,080 Handler at the end so after passport is 7779 05:29:42,080 --> 05:29:44,718 finished logging the user in it's going 7780 05:29:44,718 --> 05:29:46,280 to go ahead and call this next middle 7781 05:29:46,280 --> 05:29:47,920 function which is the request Handler 7782 05:29:47,920 --> 05:29:48,878 and then we can just send back a 7783 05:29:48,878 --> 05:29:52,920 response of a status code of 200 okay 7784 05:29:52,920 --> 05:29:54,440 now I actually forgot to show you the 7785 05:29:54,440 --> 05:29:57,120 console real quick so that way you can 7786 05:29:57,120 --> 05:29:58,558 see what happened when we try to 7787 05:29:58,558 --> 05:30:00,958 authenticate so let me just reauth Kate 7788 05:30:00,958 --> 05:30:03,718 again and show you what happens okay so 7789 05:30:03,718 --> 05:30:06,680 let's go to the endpoint click 7790 05:30:06,680 --> 05:30:10,798 authorize and now watch this okay so now 7791 05:30:10,798 --> 05:30:13,480 you can see that when I make a 7792 05:30:13,480 --> 05:30:16,360 request so what happened is this okay I 7793 05:30:16,360 --> 05:30:20,718 made an API request to SL API Discord we 7794 05:30:20,718 --> 05:30:23,480 invoked the passport to authenticate 7795 05:30:23,480 --> 05:30:25,280 function the first time when that 7796 05:30:25,280 --> 05:30:27,480 happens it redirects us to the Discord 7797 05:30:27,480 --> 05:30:30,240 platform okay when you click on the 7798 05:30:30,240 --> 05:30:31,600 authorized button on the Discord 7799 05:30:31,600 --> 05:30:33,638 platform what happens is it actually 7800 05:30:33,638 --> 05:30:37,240 redirects you back to this redirect URL 7801 05:30:37,240 --> 05:30:39,638 that we set up just now and you'll 7802 05:30:39,638 --> 05:30:41,400 notice how uh I don't know if you can 7803 05:30:41,400 --> 05:30:43,920 see this now but before when it tried to 7804 05:30:43,920 --> 05:30:45,280 redirect us there was actually this 7805 05:30:45,280 --> 05:30:47,480 query parameter up there let me comment 7806 05:30:47,480 --> 05:30:50,080 this out and show you again okay so you 7807 05:30:50,080 --> 05:30:52,120 see right over here how we have this 7808 05:30:52,120 --> 05:30:53,798 query parameter called 7809 05:30:53,798 --> 05:30:56,400 code okay and then it has this code over 7810 05:30:56,400 --> 05:30:59,040 here so we actually use that code to 7811 05:30:59,040 --> 05:31:01,958 exchange it with the Discord API to get 7812 05:31:01,958 --> 05:31:04,000 the access token and the refresh token 7813 05:31:04,000 --> 05:31:05,798 okay that's what the code is used for in 7814 05:31:05,798 --> 05:31:08,320 order to actually grab the access token 7815 05:31:08,320 --> 05:31:10,040 refresh token we must implement this 7816 05:31:10,040 --> 05:31:13,440 redirect URL so the flow works like this 7817 05:31:13,440 --> 05:31:16,480 it goes to the first endpoint 7818 05:31:16,480 --> 05:31:19,718 soapi Discord and then we use password 7819 05:31:19,718 --> 05:31:21,480 to authenticate pass in that Discord 7820 05:31:21,480 --> 05:31:23,840 strategy name that'll redirect us to the 7821 05:31:23,840 --> 05:31:26,160 third party platform once we click click 7822 05:31:26,160 --> 05:31:27,718 on authorized it's going to redirect us 7823 05:31:27,718 --> 05:31:30,440 back to the Callback URL which which is 7824 05:31:30,440 --> 05:31:33,520 this redirect or over here when you do 7825 05:31:33,520 --> 05:31:35,360 this when you call passport. 7826 05:31:35,360 --> 05:31:37,320 authenticate the second time so it's 7827 05:31:37,320 --> 05:31:38,798 calling this passport. authenticate 7828 05:31:38,798 --> 05:31:40,120 middleware the second time it's going to 7829 05:31:40,120 --> 05:31:43,080 take that code query parameter and it's 7830 05:31:43,080 --> 05:31:45,878 going to exchange it for an access token 7831 05:31:45,878 --> 05:31:47,638 and a refresh token and then what 7832 05:31:47,638 --> 05:31:49,320 happens in the end is it goes ahead and 7833 05:31:49,320 --> 05:31:51,080 calls verify 7834 05:31:51,080 --> 05:31:53,520 function after it calls this passport. 7835 05:31:53,520 --> 05:31:55,798 authenticate function the second time 7836 05:31:55,798 --> 05:31:56,920 now the reason why I didn't return a 7837 05:31:56,920 --> 05:31:58,600 response back was simply because we need 7838 05:31:58,600 --> 05:32:00,480 to continue implementing the rest of 7839 05:32:00,480 --> 05:32:03,400 this verify function so here's what 7840 05:32:03,400 --> 05:32:05,200 we're going to do I'm going to go ahead 7841 05:32:05,200 --> 05:32:10,040 and create a Discord user schema for our 7842 05:32:10,040 --> 05:32:12,280 mongodb database and we're going to go 7843 05:32:12,280 --> 05:32:14,680 ahead and save the user to our database 7844 05:32:14,680 --> 05:32:16,080 so I'll create a new 7845 05:32:16,080 --> 05:32:19,718 file inside the schemas folder and 7846 05:32:19,718 --> 05:32:23,680 I'll call this file Discord user. MJS 7847 05:32:23,680 --> 05:32:25,638 and I'm pretty much just going 7848 05:32:25,638 --> 05:32:27,920 to do the same thing that I'm doing here 7849 05:32:27,920 --> 05:32:30,080 I'll just copy this and paste it here 7850 05:32:30,080 --> 05:32:31,840 here and I'm just going to rename this 7851 05:32:31,840 --> 05:32:35,400 to Discord user schema and we're going 7852 05:32:35,400 --> 05:32:36,878 to have of course we're going to have a 7853 05:32:36,878 --> 05:32:38,520 username we don't need display name we 7854 05:32:38,520 --> 05:32:40,280 don't need password uh I'll also just 7855 05:32:40,280 --> 05:32:42,480 store the Discord ID I guess so that'll 7856 05:32:42,480 --> 05:32:44,558 be a string and then we want it to be 7857 05:32:44,558 --> 05:32:47,920 required and also unique and then what 7858 05:32:47,920 --> 05:32:49,840 I'll do is I'll take this Discord user 7859 05:32:49,840 --> 05:32:52,798 schema I'll pass into this mango. model 7860 05:32:52,798 --> 05:32:55,000 call as a second argument and I'll 7861 05:32:55,000 --> 05:32:57,558 change this from user to Discord user 7862 05:32:57,558 --> 05:33:00,160 and same thing for this model name right 7863 05:33:00,160 --> 05:33:02,520 here so this is my Discord user schema 7864 05:33:02,520 --> 05:33:04,798 and this is my Discord user model and 7865 05:33:04,798 --> 05:33:06,558 now I'm going to go ahead and inside the 7866 05:33:06,558 --> 05:33:09,280 verifi function in our Discord strategy 7867 05:33:09,280 --> 05:33:11,440 this is where I am going to search for 7868 05:33:11,440 --> 05:33:14,000 the user in the database so we want to 7869 05:33:14,000 --> 05:33:16,798 search by a unique value that will never 7870 05:33:16,798 --> 05:33:18,680 change the Discord ID will never change 7871 05:33:18,680 --> 05:33:20,718 at all so we can use the Discord ID to 7872 05:33:20,718 --> 05:33:23,400 search for the user safely because of 7873 05:33:23,400 --> 05:33:24,840 course if you search by the username 7874 05:33:24,840 --> 05:33:27,040 that could potentially change so not a 7875 05:33:27,040 --> 05:33:29,680 good idea but better to search with the 7876 05:33:29,680 --> 05:33:31,798 those ID so let's go ahead and create a 7877 05:33:31,798 --> 05:33:34,520 variable let's call this find user and 7878 05:33:34,520 --> 05:33:36,280 then we need to use async and A8 so I'm 7879 05:33:36,280 --> 05:33:37,360 going to add async in front of this 7880 05:33:37,360 --> 05:33:40,000 callback function inside this or in 7881 05:33:40,000 --> 05:33:42,200 front of this verify function and inside 7882 05:33:42,200 --> 05:33:43,840 the verify function I'm going to use the 7883 05:33:43,840 --> 05:33:45,718 awake keyword and then I'm going to 7884 05:33:45,718 --> 05:33:47,920 import the Discord user 7885 05:33:47,920 --> 05:33:50,200 model let me do that real quick I'm 7886 05:33:50,200 --> 05:33:53,160 going to import that from mongus schemas 7887 05:33:53,160 --> 05:33:56,680 Discord user like that MJS so we're 7888 05:33:56,680 --> 05:33:58,638 going to go ahead and do await Discord 7889 05:33:58,638 --> 05:34:03,760 user find find one and then for the 7890 05:34:03,760 --> 05:34:06,240 filter I'm just going to pass in the 7891 05:34:06,240 --> 05:34:08,958 field that I want to search by so I want 7892 05:34:08,958 --> 05:34:11,878 to search by the Discord ID like that 7893 05:34:11,878 --> 05:34:13,240 and then I need to pass in the actual 7894 05:34:13,240 --> 05:34:15,600 Discord ID as a value so to grab the 7895 05:34:15,600 --> 05:34:17,280 Discord ID from the profile object you 7896 05:34:17,280 --> 05:34:20,000 just reference profile. ID like this 7897 05:34:20,000 --> 05:34:21,558 that'll give you the Discord ID or if 7898 05:34:21,558 --> 05:34:23,718 you're using a different platform like 7899 05:34:23,718 --> 05:34:25,840 Twitter or Google it'll give you their 7900 05:34:25,840 --> 05:34:30,360 respective IDs okay so what we'll do is 7901 05:34:30,360 --> 05:34:32,958 if the user is not found we'll create it 7902 05:34:32,958 --> 05:34:36,520 so if no find user we'll create the user 7903 05:34:36,520 --> 05:34:38,958 and save it to the database so this is a 7904 05:34:38,958 --> 05:34:40,440 two-step process we first need to create 7905 05:34:40,440 --> 05:34:43,160 an instance of the new user so cons new 7906 05:34:43,160 --> 05:34:45,638 user and all we need to do is use the 7907 05:34:45,638 --> 05:34:48,320 new keyword to create an instance of 7908 05:34:48,320 --> 05:34:49,920 Discord user like this and you just want 7909 05:34:49,920 --> 05:34:51,760 to pass in an object that contains the 7910 05:34:51,760 --> 05:34:53,840 fields that you want to set for this new 7911 05:34:53,840 --> 05:34:54,840 user that's going to be saved to 7912 05:34:54,840 --> 05:34:56,360 database so we only have two fields to 7913 05:34:56,360 --> 05:34:58,000 worry about that's username and the 7914 05:34:58,000 --> 05:35:02,000 Discord ID so I'll do this username 7915 05:35:02,000 --> 05:35:08,200 profile. username Discord ID 7916 05:35:08,360 --> 05:35:10,760 profile so this is the first step with 7917 05:35:10,760 --> 05:35:12,480 creating the actual user instance and 7918 05:35:12,480 --> 05:35:14,000 then we have to save it to the database 7919 05:35:14,000 --> 05:35:15,520 so I'm going to go ahead and call this 7920 05:35:15,520 --> 05:35:18,638 variable new saved user equals await and 7921 05:35:18,638 --> 05:35:20,920 then new user so reference that instance 7922 05:35:20,920 --> 05:35:22,520 and call the save method and the save 7923 05:35:22,520 --> 05:35:24,200 method is asynchronous it returns to 7924 05:35:24,200 --> 05:35:25,638 promise so we need to await it and once 7925 05:35:25,638 --> 05:35:27,360 we are done with this we're going to go 7926 05:35:27,360 --> 05:35:29,440 ahead and call the done function and 7927 05:35:29,440 --> 05:35:31,040 pass a null for the error and then pass 7928 05:35:31,040 --> 05:35:33,400 in the new saved user so once you call 7929 05:35:33,400 --> 05:35:34,958 this done function it's then going to 7930 05:35:34,958 --> 05:35:37,320 need to call the serialize user function 7931 05:35:37,320 --> 05:35:38,958 for passport which I'll show you how to 7932 05:35:38,958 --> 05:35:40,840 do that as well so don't worry so I 7933 05:35:40,840 --> 05:35:42,440 definitely want to add some error 7934 05:35:42,440 --> 05:35:45,080 handling inside this logic because our 7935 05:35:45,080 --> 05:35:46,718 fine one method could throw an error and 7936 05:35:46,718 --> 05:35:49,040 so can this do save method and we want 7937 05:35:49,040 --> 05:35:50,718 to make sure we're also handling these 7938 05:35:50,718 --> 05:35:53,320 method calls and their errors separate 7939 05:35:53,320 --> 05:35:55,320 because if I just wrap everything inside 7940 05:35:55,320 --> 05:35:57,280 a TR catch block like 7941 05:35:57,280 --> 05:35:59,440 this and then I catch the arrow down 7942 05:35:59,440 --> 05:36:01,520 here it would kind of be like difficult 7943 05:36:01,520 --> 05:36:03,878 to figure out which method CES the 7944 05:36:03,878 --> 05:36:06,878 problem is it to find one or to save so 7945 05:36:06,878 --> 05:36:09,638 here's what I recommend is first we can 7946 05:36:09,638 --> 05:36:11,440 declare a variable called Fine user like 7947 05:36:11,440 --> 05:36:13,200 this and then let me just remove this 7948 05:36:13,200 --> 05:36:15,558 const find user declaration let me copy 7949 05:36:15,558 --> 05:36:16,958 this whole line over here where we're 7950 05:36:16,958 --> 05:36:20,840 calling a wait Discord user. find1 and 7951 05:36:20,840 --> 05:36:24,000 I'll try catch and 7952 05:36:24,000 --> 05:36:26,360 wrap this method called the find one 7953 05:36:26,360 --> 05:36:28,480 method called inside the try and then 7954 05:36:28,480 --> 05:36:31,120 I'm going to sign the return value to 7955 05:36:31,120 --> 05:36:34,400 find user and if this F web method 7956 05:36:34,400 --> 05:36:35,840 called errors out for whatever reason 7957 05:36:35,840 --> 05:36:38,360 then we'll catch that error down inside 7958 05:36:38,360 --> 05:36:40,440 this catch block and so here what I'll 7959 05:36:40,440 --> 05:36:44,040 do is I'll return done passing the error 7960 05:36:44,040 --> 05:36:46,000 and then null for the user however if 7961 05:36:46,000 --> 05:36:47,840 there is no error then it'll just go 7962 05:36:47,840 --> 05:36:50,840 down to here and then I'll have another 7963 05:36:50,840 --> 05:36:54,558 try catch so try 7964 05:36:54,558 --> 05:36:57,080 catch and then right down over here we 7965 05:36:57,080 --> 05:36:58,878 are still checking to see if user find 7966 05:36:58,878 --> 05:37:01,160 user is under so if it's not undefined 7967 05:37:01,160 --> 05:37:02,840 then we will go ahead and create an 7968 05:37:02,840 --> 05:37:04,280 instance of the user and then we're 7969 05:37:04,280 --> 05:37:05,878 going to go ahead and call this save 7970 05:37:05,878 --> 05:37:07,638 method which could throw an error and we 7971 05:37:07,638 --> 05:37:09,200 want to make sure we're handling it so 7972 05:37:09,200 --> 05:37:11,120 that error will be caught inside this 7973 05:37:11,120 --> 05:37:12,680 will be caught by this catch block right 7974 05:37:12,680 --> 05:37:16,160 here and then now we have handled errors 7975 05:37:16,160 --> 05:37:17,958 for both possible methods that could 7976 05:37:17,958 --> 05:37:19,638 throw an error okay so I wanted to just 7977 05:37:19,638 --> 05:37:21,120 mention that real quick because it's not 7978 05:37:21,120 --> 05:37:23,000 good to just write code that doesn't 7979 05:37:23,000 --> 05:37:24,680 have any error handling so let me go 7980 05:37:24,680 --> 05:37:26,958 ahead and return done if there is an 7981 05:37:26,958 --> 05:37:29,320 error we'll pass error and then null for 7982 05:37:29,320 --> 05:37:32,320 the user okay and if everything is 7983 05:37:32,320 --> 05:37:34,638 successful then we will just call done 7984 05:37:34,638 --> 05:37:36,760 right over here so we're not done yet we 7985 05:37:36,760 --> 05:37:38,680 s it to handle the case where fine user 7986 05:37:38,680 --> 05:37:40,040 is defined so that means the user 7987 05:37:40,040 --> 05:37:41,840 already exists in the database so that 7988 05:37:41,840 --> 05:37:43,400 means they've already logged in at least 7989 05:37:43,400 --> 05:37:45,120 once first let me just add this return 7990 05:37:45,120 --> 05:37:46,798 keyword in front of this done function 7991 05:37:46,798 --> 05:37:48,920 call and then right outside over here if 7992 05:37:48,920 --> 05:37:51,160 the user if F user is in fact defined 7993 05:37:51,160 --> 05:37:53,958 then we'll just return done and then 7994 05:37:53,958 --> 05:37:56,040 pass a n for the error and then pass in 7995 05:37:56,040 --> 05:37:58,558 that find user object as a second 7996 05:37:58,558 --> 05:38:00,040 argument 7997 05:38:00,040 --> 05:38:02,840 so let's go ahead and try to hit the API 7998 05:38:02,840 --> 05:38:04,638 so let me just go into my database right 7999 05:38:04,638 --> 05:38:06,320 now you can see I have this Discord 8000 05:38:06,320 --> 05:38:09,040 users collection there's no data in here 8001 05:38:09,040 --> 05:38:12,638 currently when I go to this Discord 8002 05:38:12,638 --> 05:38:15,240 endpoint it's going to go ahead and 8003 05:38:15,240 --> 05:38:17,240 prompt me to click 8004 05:38:17,240 --> 05:38:19,440 authorize don't worry about this error 8005 05:38:19,440 --> 05:38:20,718 the reason why this error is out right 8006 05:38:20,718 --> 05:38:22,360 now is because we don't have the 8007 05:38:22,360 --> 05:38:24,958 serialized user and deserialized user 8008 05:38:24,958 --> 05:38:26,638 methods implemented yet but what I want 8009 05:38:26,638 --> 05:38:27,638 to do is I want to show you in the 8010 05:38:27,638 --> 05:38:30,320 database if I refresh you can see that 8011 05:38:30,320 --> 05:38:32,638 my Discord user was created right over 8012 05:38:32,638 --> 05:38:34,200 here in the database and you can see I 8013 05:38:34,200 --> 05:38:36,638 have the username and the Discord ID 8014 05:38:36,638 --> 05:38:39,200 right over here all right so now let's 8015 05:38:39,200 --> 05:38:41,920 go ahead and fix this fail to serialize 8016 05:38:41,920 --> 05:38:44,400 user intercession issue so as I 8017 05:38:44,400 --> 05:38:46,280 mentioned earlier already uh we went 8018 05:38:46,280 --> 05:38:49,680 over a lot of these basic stuff such as 8019 05:38:49,680 --> 05:38:52,000 serialized user and deserialized user 8020 05:38:52,000 --> 05:38:53,878 with the local strategy I highly 8021 05:38:53,878 --> 05:38:55,840 recommend you rewatch that section or 8022 05:38:55,840 --> 05:38:57,798 watch it if you missed that part but 8023 05:38:57,798 --> 05:38:59,760 what I'll do is I'll copy all this stuff 8024 05:38:59,760 --> 05:39:02,680 from the local strategy file and don't 8025 05:39:02,680 --> 05:39:04,520 worry I'll explain what I'm doing and 8026 05:39:04,520 --> 05:39:06,240 I'll explain everything that's going on 8027 05:39:06,240 --> 05:39:08,600 so I'm going to copy all of this and I'm 8028 05:39:08,600 --> 05:39:10,120 going to paste this inside my Discord 8029 05:39:10,120 --> 05:39:12,600 strategy file so let me explain one by 8030 05:39:12,600 --> 05:39:13,920 one what's going on and you know what 8031 05:39:13,920 --> 05:39:16,320 let me remove this Des serialized user 8032 05:39:16,320 --> 05:39:17,958 and let's just focus on serialized user 8033 05:39:17,958 --> 05:39:20,638 first so inside this verify function 8034 05:39:20,638 --> 05:39:23,680 whenever we call the done function and 8035 05:39:23,680 --> 05:39:25,240 assume that there's no errors so we pass 8036 05:39:25,240 --> 05:39:26,878 in null for the error and then we pass 8037 05:39:26,878 --> 05:39:29,160 in this user object okay so what really 8038 05:39:29,160 --> 05:39:30,400 happen happens when we call this done 8039 05:39:30,400 --> 05:39:32,638 function is we're passing in this user 8040 05:39:32,638 --> 05:39:34,798 object and we're telling passport that 8041 05:39:34,798 --> 05:39:37,718 we want to serialize this user object 8042 05:39:37,718 --> 05:39:39,638 into the session data okay remember 8043 05:39:39,638 --> 05:39:41,680 passport works well with Express session 8044 05:39:41,680 --> 05:39:43,600 so this user object right over here is 8045 05:39:43,600 --> 05:39:46,680 going to be passed into this callback 8046 05:39:46,680 --> 05:39:50,240 function as an argument right here so 8047 05:39:50,240 --> 05:39:53,520 whenever we first log in and once that 8048 05:39:53,520 --> 05:39:55,000 done function is called in the verif 8049 05:39:55,000 --> 05:39:56,520 function it will call this callback 8050 05:39:56,520 --> 05:39:59,000 function that is passed to serialize 8051 05:39:59,000 --> 05:40:01,600 user so whatever you pass in right over 8052 05:40:01,600 --> 05:40:03,120 here as the second argument to the done 8053 05:40:03,120 --> 05:40:05,718 function while error is set to null will 8054 05:40:05,718 --> 05:40:08,240 be passed over to this user argument 8055 05:40:08,240 --> 05:40:10,760 over here okay so watch this I'm going 8056 05:40:10,760 --> 05:40:12,920 to leave the console logs alone and I'm 8057 05:40:12,920 --> 05:40:14,760 going to 8058 05:40:14,760 --> 05:40:16,400 reauthenticate and you're going to see 8059 05:40:16,400 --> 05:40:19,080 what's going to happen Okay so let's go 8060 05:40:19,080 --> 05:40:20,400 ahead and click 8061 05:40:20,400 --> 05:40:22,878 authorize okay now you see how the error 8062 05:40:22,878 --> 05:40:25,240 goes away the fail to serialize user 8063 05:40:25,240 --> 05:40:26,600 inter session error goes away that's 8064 05:40:26,600 --> 05:40:28,200 because we were able to successfully 8065 05:40:28,200 --> 05:40:29,878 serialize user into to the session 8066 05:40:29,878 --> 05:40:32,240 object because we implemented this 8067 05:40:32,240 --> 05:40:33,718 function right over here and all you 8068 05:40:33,718 --> 05:40:35,760 really need to do is just call the done 8069 05:40:35,760 --> 05:40:37,478 function that is the second argument to 8070 05:40:37,478 --> 05:40:39,240 this call back when you call the done 8071 05:40:39,240 --> 05:40:41,680 function you pass in null as the error 8072 05:40:41,680 --> 05:40:44,280 and then you pass in some unique ID that 8073 05:40:44,280 --> 05:40:46,200 can be used to identify the user so you 8074 05:40:46,200 --> 05:40:48,600 can use either an ID or if you want to 8075 05:40:48,600 --> 05:40:50,120 use the username though even though that 8076 05:40:50,120 --> 05:40:52,160 is unique it can be changed so you would 8077 05:40:52,160 --> 05:40:53,920 have to just be responsible for updating 8078 05:40:53,920 --> 05:40:55,760 the session data cuz the session data 8079 05:40:55,760 --> 05:40:57,958 could go stale but what we're doing is 8080 05:40:57,958 --> 05:40:59,680 we are saying okay I want want to 8081 05:40:59,680 --> 05:41:02,878 serialize this ID of the user into the 8082 05:41:02,878 --> 05:41:05,320 session object now remember when we went 8083 05:41:05,320 --> 05:41:07,120 over sessions I explained how all that 8084 05:41:07,120 --> 05:41:10,240 stuff works so I'll show you right now 8085 05:41:10,240 --> 05:41:13,718 what will happen if right 8086 05:41:13,718 --> 05:41:18,558 over uh let's see right over here inside 8087 05:41:18,558 --> 05:41:20,240 the redirect endpoint I'm going to go 8088 05:41:20,240 --> 05:41:22,600 inside this request handle and I will 8089 05:41:22,600 --> 05:41:25,558 log console.log 8090 05:41:25,558 --> 05:41:28,798 request. session and then you'll also 8091 05:41:28,798 --> 05:41:31,558 see that we now have this request. user 8092 05:41:31,558 --> 05:41:35,040 object so let me go ahead and show you 8093 05:41:35,040 --> 05:41:39,400 what happens okay so let's 8094 05:41:39,400 --> 05:41:42,280 reauthenticate uh 8095 05:41:42,280 --> 05:41:44,600 whoops okay this is this is the reason 8096 05:41:44,600 --> 05:41:46,760 why we're having this errors because we 8097 05:41:46,760 --> 05:41:50,360 are already logged in and it's trying to 8098 05:41:50,360 --> 05:41:52,760 deserialize user because we already have 8099 05:41:52,760 --> 05:41:54,440 a cookie so what I can do is very 8100 05:41:54,440 --> 05:41:57,200 quickly I'll go ahead and just clear the 8101 05:41:57,200 --> 05:42:00,840 cookie and Let me refresh 8102 05:42:00,840 --> 05:42:02,798 click authorize now I want to show you 8103 05:42:02,798 --> 05:42:05,280 the logs you see how now the session 8104 05:42:05,280 --> 05:42:07,160 object so this session object is being 8105 05:42:07,160 --> 05:42:11,320 logged right over here inside the 8106 05:42:11,320 --> 05:42:13,878 request Handler for our redirect 8107 05:42:13,878 --> 05:42:16,240 endpoint so when Discord redirects us to 8108 05:42:16,240 --> 05:42:19,000 the endpoint it's going to go ahead and 8109 05:42:19,000 --> 05:42:20,798 eventually call this request Handler 8110 05:42:20,798 --> 05:42:23,000 function and you can see now the session 8111 05:42:23,000 --> 05:42:25,798 object has that passport property which 8112 05:42:25,798 --> 05:42:27,638 is an object and notice how we have this 8113 05:42:27,638 --> 05:42:30,040 user property inside this password 8114 05:42:30,040 --> 05:42:33,440 object and that is the users's ID okay 8115 05:42:33,440 --> 05:42:36,000 and then we also have the request. user 8116 05:42:36,000 --> 05:42:37,558 object being logged and then you can see 8117 05:42:37,558 --> 05:42:40,440 that this is the actual user object ID 8118 05:42:40,440 --> 05:42:42,040 okay that's the object ID of the user 8119 05:42:42,040 --> 05:42:43,520 document we have the username and then 8120 05:42:43,520 --> 05:42:45,440 we have the Discord ID this is the user 8121 05:42:45,440 --> 05:42:46,878 object itself so that's the 8122 05:42:46,878 --> 05:42:48,878 responsibility of the serialize user 8123 05:42:48,878 --> 05:42:51,680 function it pretty much allows us to say 8124 05:42:51,680 --> 05:42:54,080 okay how do we want to serialize our 8125 05:42:54,080 --> 05:42:57,638 user data into the session if I pass in 8126 05:42:57,638 --> 05:42:58,958 something else let's just say if I pass 8127 05:42:58,958 --> 05:43:01,840 the in just the entire user object then 8128 05:43:01,840 --> 05:43:03,760 what you'll see is this user property 8129 05:43:03,760 --> 05:43:06,400 will map to the entire user object okay 8130 05:43:06,400 --> 05:43:07,558 so let me just go ahead and clear my 8131 05:43:07,558 --> 05:43:09,440 cookies once again because it'll throw 8132 05:43:09,440 --> 05:43:11,878 an error because we are passing a valid 8133 05:43:11,878 --> 05:43:14,240 cookie but when we do that we actually 8134 05:43:14,240 --> 05:43:16,440 need the Der serialize user function and 8135 05:43:16,440 --> 05:43:18,000 I'll explain a little bit a little bit 8136 05:43:18,000 --> 05:43:19,760 about that in just a second but let me 8137 05:43:19,760 --> 05:43:23,080 try to reauthenticate 8138 05:43:23,600 --> 05:43:26,478 now and now if you look at the logs 8139 05:43:26,478 --> 05:43:29,280 you'll see how that user object is is 8140 05:43:29,280 --> 05:43:31,638 now in the session data like I said all 8141 05:43:31,638 --> 05:43:33,958 this stuff I've went over in early parts 8142 05:43:33,958 --> 05:43:36,320 of the tutorial when we over passport 8143 05:43:36,320 --> 05:43:38,760 and session so if you need a recap with 8144 05:43:38,760 --> 05:43:41,240 a full in-depth coverage of everything 8145 05:43:41,240 --> 05:43:43,160 of how it works I definitely recommend 8146 05:43:43,160 --> 05:43:46,280 you to revisit that section okay and 8147 05:43:46,280 --> 05:43:48,798 it'll clear up a lot of confusion but 8148 05:43:48,798 --> 05:43:51,120 let's go ahead and change this back to 8149 05:43:51,120 --> 05:43:54,120 passing the user ID and this is the uh 8150 05:43:54,120 --> 05:43:56,400 the actual document object ID not the 8151 05:43:56,400 --> 05:43:59,600 Discord ID okay so now let's go ahead 8152 05:43:59,600 --> 05:44:01,798 and implement the Der serialized user 8153 05:44:01,798 --> 05:44:03,360 method so we're going to go ahead and 8154 05:44:03,360 --> 05:44:06,680 call passport. deserialize user pass in 8155 05:44:06,680 --> 05:44:09,320 a callback function as well now this 8156 05:44:09,320 --> 05:44:10,760 function is important because when we 8157 05:44:10,760 --> 05:44:13,280 first log in we call serialized user 8158 05:44:13,280 --> 05:44:15,718 because we're basically taking the user 8159 05:44:15,718 --> 05:44:18,240 object we are storing it in the session 8160 05:44:18,240 --> 05:44:20,120 store and then we're setting a cookie 8161 05:44:20,120 --> 05:44:22,160 and sending it to the browser when the 8162 05:44:22,160 --> 05:44:24,520 browser has that cookie stored it's then 8163 05:44:24,520 --> 05:44:27,280 going to send the cookie upon subsequent 8164 05:44:27,280 --> 05:44:29,638 requests now that cookie is sent to the 8165 05:44:29,638 --> 05:44:32,400 server and our middle Wares such as 8166 05:44:32,400 --> 05:44:35,400 Express session passport will take care 8167 05:44:35,400 --> 05:44:37,040 of everything for us so what it does 8168 05:44:37,040 --> 05:44:38,760 underne the hood is it'll grab that 8169 05:44:38,760 --> 05:44:40,600 session ID from the cookie and it'll 8170 05:44:40,600 --> 05:44:43,520 look inside the session store for that 8171 05:44:43,520 --> 05:44:46,878 session ID and the data that it maps to 8172 05:44:46,878 --> 05:44:49,400 okay so I actually already have the 8173 05:44:49,400 --> 05:44:50,638 session store implemented so if I go 8174 05:44:50,638 --> 05:44:51,360 into 8175 05:44:51,360 --> 05:44:53,160 sessions you can see that we have a 8176 05:44:53,160 --> 05:44:55,160 bunch of session data right over here so 8177 05:44:55,160 --> 05:44:56,440 what happens is it goes inside the 8178 05:44:56,440 --> 05:44:59,558 session store looks for the session ID 8179 05:44:59,558 --> 05:45:01,920 so for example these are session IDs and 8180 05:45:01,920 --> 05:45:04,680 then it references this session property 8181 05:45:04,680 --> 05:45:07,240 right over here and currently this is a 8182 05:45:07,240 --> 05:45:09,558 string but what it does is it parses 8183 05:45:09,558 --> 05:45:12,000 this into a Json object and then it 8184 05:45:12,000 --> 05:45:13,958 attaches it to the 8185 05:45:13,958 --> 05:45:15,600 request. 8186 05:45:15,600 --> 05:45:19,120 session property okay hence why when I 8187 05:45:19,120 --> 05:45:20,718 showed you when I logged request. 8188 05:45:20,718 --> 05:45:22,240 session you saw how we had that cookie 8189 05:45:22,240 --> 05:45:24,080 property and then you also saw that we 8190 05:45:24,080 --> 05:45:25,520 had that password property and we had 8191 05:45:25,520 --> 05:45:28,160 the user property all that kind of stuff 8192 05:45:28,160 --> 05:45:30,400 what these serial user takes care of is 8193 05:45:30,400 --> 05:45:32,760 it pretty much takes that serialized 8194 05:45:32,760 --> 05:45:35,958 data so in our case it's just the 8195 05:45:35,958 --> 05:45:39,040 session data that's all uh pars into a 8196 05:45:39,040 --> 05:45:41,920 string and then it kind of like un 8197 05:45:41,920 --> 05:45:43,798 undoes it it converts it back from a 8198 05:45:43,798 --> 05:45:46,160 string to ajason object and restores it 8199 05:45:46,160 --> 05:45:48,520 by attaching it to the request object so 8200 05:45:48,520 --> 05:45:51,200 we can access it very easily so we know 8201 05:45:51,200 --> 05:45:53,240 who the user is and we can get the user 8202 05:45:53,240 --> 05:45:56,840 session data okay so the first argument 8203 05:45:56,840 --> 05:45:58,440 for deserialized user is going to be the 8204 05:45:58,440 --> 05:45:59,798 ID 8205 05:45:59,798 --> 05:46:01,200 which is really whatever it is that you 8206 05:46:01,200 --> 05:46:02,440 passed in right over here so if it's 8207 05:46:02,440 --> 05:46:04,160 user ID right over here then this 8208 05:46:04,160 --> 05:46:05,638 argument is going to be that user ID if 8209 05:46:05,638 --> 05:46:08,840 you passed in user. username like this 8210 05:46:08,840 --> 05:46:10,040 then this argument is going to be the 8211 05:46:10,040 --> 05:46:12,240 username if you passed in the user 8212 05:46:12,240 --> 05:46:14,320 object itself then this argument would 8213 05:46:14,320 --> 05:46:16,840 be the user itself 8214 05:46:16,840 --> 05:46:20,400 okay let's keep it as user ID and the 8215 05:46:20,400 --> 05:46:21,920 second argument for this callback 8216 05:46:21,920 --> 05:46:24,440 function is done similar to serialized 8217 05:46:24,440 --> 05:46:27,440 users call function now inside the 8218 05:46:27,440 --> 05:46:29,600 serialized user we actually need to to 8219 05:46:29,600 --> 05:46:32,160 search for the user by the ID so the 8220 05:46:32,160 --> 05:46:34,478 first thing that we're going to do is 8221 05:46:34,478 --> 05:46:37,000 I'm going to use a TR 8222 05:46:37,000 --> 05:46:40,080 catch always handle your errors and what 8223 05:46:40,080 --> 05:46:42,320 I'll do is I'll search for the user so 8224 05:46:42,320 --> 05:46:45,080 const find user equals and since we're 8225 05:46:45,080 --> 05:46:46,478 searching for the Discord user I'm going 8226 05:46:46,478 --> 05:46:49,400 to import the Discord user model I think 8227 05:46:49,400 --> 05:46:51,520 I already had that up top over yep I had 8228 05:46:51,520 --> 05:46:54,320 it up top over here so Discord user. 8229 05:46:54,320 --> 05:46:55,958 find one and we're going to actually 8230 05:46:55,958 --> 05:46:59,040 search not by the Discord ID but by the 8231 05:46:59,040 --> 05:47:02,558 object ID so the object ID the object ID 8232 05:47:02,558 --> 05:47:04,680 are the this is this ID right over here 8233 05:47:04,680 --> 05:47:06,600 not the Discord ID it's the 8234 05:47:06,600 --> 05:47:10,240 autogenerated ID that mongodb generates 8235 05:47:10,240 --> 05:47:13,558 for each document in the collection so I 8236 05:47:13,558 --> 05:47:16,000 can actually just use this find one or 8237 05:47:16,000 --> 05:47:18,798 is finded by ID method and pass in that 8238 05:47:18,798 --> 05:47:21,878 object ID so this will search for that 8239 05:47:21,878 --> 05:47:24,160 user by the ID and I need to make sure I 8240 05:47:24,160 --> 05:47:26,120 await this call because it is a 8241 05:47:26,120 --> 05:47:27,958 synchronous so let me add the Asing 8242 05:47:27,958 --> 05:47:29,200 keyword in front of our call call 8243 05:47:29,200 --> 05:47:32,440 function okay so now if the user is 8244 05:47:32,440 --> 05:47:34,798 found so I'm I'm going to use a turn 8245 05:47:34,798 --> 05:47:38,680 operator so if the user is found I'm 8246 05:47:38,680 --> 05:47:40,638 going to call done pass null for the 8247 05:47:40,638 --> 05:47:43,638 error and pass find user however if the 8248 05:47:43,638 --> 05:47:46,080 user is whoops sorry about this find 8249 05:47:46,080 --> 05:47:48,600 user Mark okay so if find user is 8250 05:47:48,600 --> 05:47:50,080 defined it's going to go ahead and call 8251 05:47:50,080 --> 05:47:52,798 done and then if find user is undefined 8252 05:47:52,798 --> 05:47:55,718 we'll call done but pass in uh null for 8253 05:47:55,718 --> 05:47:59,080 the error and null for the user okay so 8254 05:47:59,080 --> 05:48:01,360 there's no error but there's also no 8255 05:48:01,360 --> 05:48:04,798 user so now inside the catch we will 8256 05:48:04,798 --> 05:48:07,638 just simply call done passing the error 8257 05:48:07,638 --> 05:48:10,200 and null for the user okay so now you'll 8258 05:48:10,200 --> 05:48:11,680 see that the Der serialized user 8259 05:48:11,680 --> 05:48:14,440 function error will go away now to test 8260 05:48:14,440 --> 05:48:15,638 everything out we're going to go ahead 8261 05:48:15,638 --> 05:48:18,798 and make that request to the o/ Discord 8262 05:48:18,798 --> 05:48:20,798 endpoint which will eventually redirect 8263 05:48:20,798 --> 05:48:24,400 us back to this endpoint over here and 8264 05:48:24,400 --> 05:48:26,520 then what I'll do is I'll I'll visit 8265 05:48:26,520 --> 05:48:30,160 this API off/ status endpoint that I had 8266 05:48:30,160 --> 05:48:32,840 implemented already from previous parts 8267 05:48:32,840 --> 05:48:34,718 of the tutorial but it still works just 8268 05:48:34,718 --> 05:48:36,280 fine because the responsibility of this 8269 05:48:36,280 --> 05:48:39,200 endpoint is just return the user object 8270 05:48:39,200 --> 05:48:42,000 that's on the request object itself and 8271 05:48:42,000 --> 05:48:43,958 we already configured passport to do 8272 05:48:43,958 --> 05:48:45,000 that for 8273 05:48:45,000 --> 05:48:47,400 us okay specifically for the Discord 8274 05:48:47,400 --> 05:48:50,080 user so let's go ahead and go back to 8275 05:48:50,080 --> 05:48:52,600 the browser let's go ahead and try to 8276 05:48:52,600 --> 05:48:53,958 let me clear my cookies first because 8277 05:48:53,958 --> 05:48:56,160 we're still technically logged in so I 8278 05:48:56,160 --> 05:48:59,200 want to do a completely new login 8279 05:48:59,200 --> 05:49:01,638 so let's do that okay so I'm going to 8280 05:49:01,638 --> 05:49:03,638 click authorize so I've have 8281 05:49:03,638 --> 05:49:06,000 successfully logged in and now notice 8282 05:49:06,000 --> 05:49:09,798 how if I go to the console you can see 8283 05:49:09,798 --> 05:49:11,080 that all of my session data is right 8284 05:49:11,080 --> 05:49:16,320 over here and then if I go to API status 8285 05:49:16,320 --> 05:49:19,478 you see how now it returns to me the 8286 05:49:19,478 --> 05:49:22,718 Discord user that I am logged in as if I 8287 05:49:22,718 --> 05:49:24,040 were logging in as a different Discord 8288 05:49:24,040 --> 05:49:26,320 user it would go through that verifi fun 8289 05:49:26,320 --> 05:49:28,240 function logic and then it would end up 8290 05:49:28,240 --> 05:49:31,000 ser realizing that other user into the 8291 05:49:31,000 --> 05:49:32,240 session and then when we visit this 8292 05:49:32,240 --> 05:49:34,878 endpoint we can see our record that's 8293 05:49:34,878 --> 05:49:37,280 saved in the database so I hope all this 8294 05:49:37,280 --> 05:49:39,638 makes 8295 05:49:41,400 --> 05:49:44,478 sense all right everyone so in this part 8296 05:49:44,478 --> 05:49:46,440 of our expressjs tutorial I'm going to 8297 05:49:46,440 --> 05:49:49,320 show you how to set up just and use just 8298 05:49:49,320 --> 05:49:51,520 to write unit tests and run them for 8299 05:49:51,520 --> 05:49:53,798 your express application so just is a 8300 05:49:53,798 --> 05:49:55,920 testing framework for JavaScript it's 8301 05:49:55,920 --> 05:49:58,520 very popular it's made by Facebook or 8302 05:49:58,520 --> 05:50:00,240 now known as meta and it's been around 8303 05:50:00,240 --> 05:50:02,200 for a very long time and you can use 8304 05:50:02,200 --> 05:50:04,760 just to write tests and run them for 8305 05:50:04,760 --> 05:50:06,798 really any JavaScript application many 8306 05:50:06,798 --> 05:50:09,240 people use it to test no JS server side 8307 05:50:09,240 --> 05:50:12,000 apps you can use it also to test react 8308 05:50:12,000 --> 05:50:14,478 apps or angular apps it's very versatile 8309 05:50:14,478 --> 05:50:16,958 so let's go ahead and get started so 8310 05:50:16,958 --> 05:50:18,558 inside my project one thing that I do 8311 05:50:18,558 --> 05:50:20,558 want to mention is that remember that we 8312 05:50:20,558 --> 05:50:23,360 are using ES modules so if you recall 8313 05:50:23,360 --> 05:50:25,878 inside our package.json file you can see 8314 05:50:25,878 --> 05:50:28,040 that I have the type set to module and 8315 05:50:28,040 --> 05:50:30,240 all of my my file extensions are ending 8316 05:50:30,240 --> 05:50:32,760 with MJS now there's nothing wrong with 8317 05:50:32,760 --> 05:50:35,000 using ES modules and it is the way to go 8318 05:50:35,000 --> 05:50:36,718 in the future with developing surver 8319 05:50:36,718 --> 05:50:39,240 side with no. JS however as of right now 8320 05:50:39,240 --> 05:50:41,360 just they currently do support es 8321 05:50:41,360 --> 05:50:43,958 modules however it requires experimental 8322 05:50:43,958 --> 05:50:45,718 Flags so you need to enable an 8323 05:50:45,718 --> 05:50:47,680 environment variable and while that 8324 05:50:47,680 --> 05:50:50,120 might work out of the box there are some 8325 05:50:50,120 --> 05:50:51,920 things that don't necessarily work and 8326 05:50:51,920 --> 05:50:53,520 after doing some research on stack 8327 05:50:53,520 --> 05:50:55,600 Overflow and some of the GitHub issues 8328 05:50:55,600 --> 05:50:56,840 I've came to the conclusion that some of 8329 05:50:56,840 --> 05:50:58,600 the things that I was trying to get to 8330 05:50:58,600 --> 05:51:00,718 work just wasn't simply working to say 8331 05:51:00,718 --> 05:51:03,760 the least so the options that we have 8332 05:51:03,760 --> 05:51:05,400 are we can either revert our entire 8333 05:51:05,400 --> 05:51:07,440 project to Common JS so what that means 8334 05:51:07,440 --> 05:51:09,478 is we remove this type let's everything 8335 05:51:09,478 --> 05:51:11,520 back to commonjs because that's what the 8336 05:51:11,520 --> 05:51:13,558 default system is and then we would have 8337 05:51:13,558 --> 05:51:15,440 to remove all of our import statements 8338 05:51:15,440 --> 05:51:17,920 and replace them with require statements 8339 05:51:17,920 --> 05:51:20,320 so instead of import Express from 8340 05:51:20,320 --> 05:51:21,760 Express we would have to do const 8341 05:51:21,760 --> 05:51:25,320 Express equals require Express like this 8342 05:51:25,320 --> 05:51:27,600 and then we would have to change all of 8343 05:51:27,600 --> 05:51:29,440 our export stat stat as well so instead 8344 05:51:29,440 --> 05:51:32,360 of export cons mock users I would have 8345 05:51:32,360 --> 05:51:35,160 to do module. export and 8346 05:51:35,160 --> 05:51:39,000 then pass and mock users like this and 8347 05:51:39,000 --> 05:51:40,878 obviously that's not something that is 8348 05:51:40,878 --> 05:51:42,600 possible for many projects because you 8349 05:51:42,600 --> 05:51:44,600 might have already written so much code 8350 05:51:44,600 --> 05:51:45,680 and you can't just go through every 8351 05:51:45,680 --> 05:51:47,440 single file and change all those lines 8352 05:51:47,440 --> 05:51:49,240 just doesn't really make sense so I 8353 05:51:49,240 --> 05:51:51,080 figured out a solution where we can 8354 05:51:51,080 --> 05:51:52,798 actually configure Babble which is a 8355 05:51:52,798 --> 05:51:54,878 transpiler and we can use Babel to 8356 05:51:54,878 --> 05:51:58,320 actually take all of our MJS files and 8357 05:51:58,320 --> 05:52:00,478 kind of like transform them from es 8358 05:52:00,478 --> 05:52:02,600 modules into commonjs so that we just 8359 05:52:02,600 --> 05:52:05,280 can actually execute them correctly so 8360 05:52:05,280 --> 05:52:07,040 we're going to leave our type set to 8361 05:52:07,040 --> 05:52:08,400 module we're going to leave all of our 8362 05:52:08,400 --> 05:52:09,920 code the way it is we're not going to 8363 05:52:09,920 --> 05:52:11,240 change anything at all we're not going 8364 05:52:11,240 --> 05:52:13,638 to modify the import statements nothing 8365 05:52:13,638 --> 05:52:14,920 and I'm going to show you how this is 8366 05:52:14,920 --> 05:52:16,760 going to work now if you're someone that 8367 05:52:16,760 --> 05:52:18,240 is just trying to learn how to write 8368 05:52:18,240 --> 05:52:20,120 unit tests and you might already have 8369 05:52:20,120 --> 05:52:23,320 Babble or typescript set up then just 8370 05:52:23,320 --> 05:52:25,558 actually has documentation on how to 8371 05:52:25,558 --> 05:52:27,200 configure just with the right 8372 05:52:27,200 --> 05:52:29,638 environment if you go to the their docs 8373 05:52:29,638 --> 05:52:31,840 they actually show you right over here 8374 05:52:31,840 --> 05:52:34,280 if you're using Babel webpack typescript 8375 05:52:34,280 --> 05:52:35,718 all this stuff so for example if you're 8376 05:52:35,718 --> 05:52:38,160 using typescript you can use tsj right 8377 05:52:38,160 --> 05:52:39,600 over here even mentions over here and 8378 05:52:39,600 --> 05:52:41,160 it's basically just a pre-processor so 8379 05:52:41,160 --> 05:52:42,478 you can actually use just to test your 8380 05:52:42,478 --> 05:52:44,840 project right in the typescript so to 8381 05:52:44,840 --> 05:52:46,840 get started we need to install a couple 8382 05:52:46,840 --> 05:52:48,320 of dependencies so I'm going to type 8383 05:52:48,320 --> 05:52:51,200 npmi hyphen capital D CU I'm going to 8384 05:52:51,200 --> 05:52:53,200 install these as Dev dependencies so the 8385 05:52:53,200 --> 05:52:57,160 packages are at Babel slore 8386 05:52:57,160 --> 05:53:03,080 Babel code at Babel slpre EnV so that's 8387 05:53:03,080 --> 05:53:05,600 it for Babel we then need to install 8388 05:53:05,600 --> 05:53:09,120 just as well and then let's go ahead and 8389 05:53:09,120 --> 05:53:11,000 hit 8390 05:53:11,000 --> 05:53:13,718 enter so while this is happening I'm 8391 05:53:13,718 --> 05:53:16,798 going to go ahead and go into my project 8392 05:53:16,798 --> 05:53:18,600 folder and I'm going to go ahead and 8393 05:53:18,600 --> 05:53:21,200 just set up the Babel RC configuration 8394 05:53:21,200 --> 05:53:23,638 file so you're going to go ahead and 8395 05:53:23,638 --> 05:53:26,120 create a new file I'm going to call it 8396 05:53:26,120 --> 05:53:29,240 Babble RC like this and then I'm going 8397 05:53:29,240 --> 05:53:32,320 to go ahead and pass an object like that 8398 05:53:32,320 --> 05:53:34,878 and then we need this presets field and 8399 05:53:34,878 --> 05:53:36,718 this is going to be an array and inside 8400 05:53:36,718 --> 05:53:38,478 this array we're actually going to pass 8401 05:53:38,478 --> 05:53:40,638 an array and then inside this inner 8402 05:53:40,638 --> 05:53:43,920 array we're going to pass a string and 8403 05:53:43,920 --> 05:53:47,478 we're going to type at Babel SL preset 8404 05:53:47,478 --> 05:53:48,400 hyphen 8405 05:53:48,400 --> 05:53:51,040 EnV and then we're going to pass a 8406 05:53:51,040 --> 05:53:53,200 second argument or second element inside 8407 05:53:53,200 --> 05:53:55,160 this inner array so we have this object 8408 05:53:55,160 --> 05:53:56,958 right over here and this object is going 8409 05:53:56,958 --> 05:53:58,638 to have this targets proper property 8410 05:53:58,638 --> 05:54:00,600 which is going to map to an object and 8411 05:54:00,600 --> 05:54:03,120 in that object we're going to have node 8412 05:54:03,120 --> 05:54:04,600 set to current okay so we're just 8413 05:54:04,600 --> 05:54:07,440 configuring the preset EnV for Babel 8414 05:54:07,440 --> 05:54:09,440 okay so that's pretty much it for the 8415 05:54:09,440 --> 05:54:12,280 Babel stuff now let's go ahead and close 8416 05:54:12,280 --> 05:54:14,558 that so our packages have finish 8417 05:54:14,558 --> 05:54:17,280 installing and let's go ahead and type 8418 05:54:17,280 --> 05:54:21,240 npm init just at latest so we want to 8419 05:54:21,240 --> 05:54:23,520 configure just and what this will do is 8420 05:54:23,520 --> 05:54:26,160 set up a just config file for us so I'm 8421 05:54:26,160 --> 05:54:27,798 going to hit enter and then it's going 8422 05:54:27,798 --> 05:54:29,400 to ask ask us some questions so it's 8423 05:54:29,400 --> 05:54:30,958 going to say would you like to use just 8424 05:54:30,958 --> 05:54:32,240 when running test so this will just 8425 05:54:32,240 --> 05:54:33,840 create a test script for us so I'm going 8426 05:54:33,840 --> 05:54:35,200 to hit yes would you like to use 8427 05:54:35,200 --> 05:54:37,120 typescript if you're using typescript 8428 05:54:37,120 --> 05:54:39,040 then press yes but I'm going to press no 8429 05:54:39,040 --> 05:54:41,440 uh choose the test environment so we are 8430 05:54:41,440 --> 05:54:44,760 testing a server sided noj application 8431 05:54:44,760 --> 05:54:47,760 so we're going to hit enter for node but 8432 05:54:47,760 --> 05:54:49,160 if you're testing like a frontend 8433 05:54:49,160 --> 05:54:51,478 application for react and You' want to 8434 05:54:51,478 --> 05:54:54,638 hit jsom do you want just add coverage 8435 05:54:54,638 --> 05:54:56,958 reports we'll hit no for that we'll just 8436 05:54:56,958 --> 05:55:00,160 select it as V8 and then for this one 8437 05:55:00,160 --> 05:55:02,600 you want to hit yes automatically clear 8438 05:55:02,600 --> 05:55:05,240 mock calls instances context and result 8439 05:55:05,240 --> 05:55:07,600 before every test this will ensure that 8440 05:55:07,600 --> 05:55:09,840 whenever you run your next test there 8441 05:55:09,840 --> 05:55:12,638 won't be any leftover uh mock calls or 8442 05:55:12,638 --> 05:55:14,478 mock data from the previous test and 8443 05:55:14,478 --> 05:55:16,600 it's actually very important so you can 8444 05:55:16,600 --> 05:55:18,958 see that it modifi the package Json file 8445 05:55:18,958 --> 05:55:22,400 and then we create this just. config.js 8446 05:55:22,400 --> 05:55:24,798 file okay so we're not done yet because 8447 05:55:24,798 --> 05:55:26,080 there's one more thing that we need to 8448 05:55:26,080 --> 05:55:30,320 do inside our just .c config.js file so 8449 05:55:30,320 --> 05:55:32,718 right over here we need to go to this 8450 05:55:32,718 --> 05:55:34,520 transform property currently it is 8451 05:55:34,520 --> 05:55:36,798 commented out but we want to uncomment 8452 05:55:36,798 --> 05:55:38,558 this part and we want to set this to be 8453 05:55:38,558 --> 05:55:41,200 an object and now what this transform 8454 05:55:41,200 --> 05:55:43,200 property will take care of is it will 8455 05:55:43,200 --> 05:55:45,520 look for those source files that match a 8456 05:55:45,520 --> 05:55:48,320 regular expression and transform the 8457 05:55:48,320 --> 05:55:50,240 source code into something that Jess can 8458 05:55:50,240 --> 05:55:52,400 actually run which is pretty much plain 8459 05:55:52,400 --> 05:55:53,798 JavaScript so what I'm going to do 8460 05:55:53,798 --> 05:55:55,760 inside this object is I'm going to use 8461 05:55:55,760 --> 05:55:57,958 this regular expression right over here 8462 05:55:57,958 --> 05:56:00,280 I'm going to copy it and you all can 8463 05:56:00,280 --> 05:56:02,600 just copy this as well so this will just 8464 05:56:02,600 --> 05:56:04,798 look for our MJS files inside our 8465 05:56:04,798 --> 05:56:07,878 directory and what I'm going to do is 8466 05:56:07,878 --> 05:56:12,080 map this value to babble justest so this 8467 05:56:12,080 --> 05:56:13,920 is going to be the tool that it's going 8468 05:56:13,920 --> 05:56:16,798 to use to deal with a transformation and 8469 05:56:16,798 --> 05:56:18,520 now we actually didn't install Babel 8470 05:56:18,520 --> 05:56:21,240 justest but when you install just it 8471 05:56:21,240 --> 05:56:23,478 actually does install Babble justest for 8472 05:56:23,478 --> 05:56:25,200 you as of right now if you look at your 8473 05:56:25,200 --> 05:56:27,160 node modules you can see that there 8474 05:56:27,160 --> 05:56:30,320 should be this dable just package right 8475 05:56:30,320 --> 05:56:32,638 over here so that's how we can verify 8476 05:56:32,638 --> 05:56:36,200 that that package was installed for us 8477 05:56:36,200 --> 05:56:38,000 okay so we're done with this part and 8478 05:56:38,000 --> 05:56:39,680 let's just also go 8479 05:56:39,680 --> 05:56:41,850 into uh right 8480 05:56:41,850 --> 05:56:44,280 [Music] 8481 05:56:44,280 --> 05:56:48,638 over let me go over here uh module file 8482 05:56:48,638 --> 05:56:50,840 extensions so we'll just uncomment that 8483 05:56:50,840 --> 05:56:52,400 and we'll just you can remove the ones 8484 05:56:52,400 --> 05:56:53,680 that you don't need so I'm not going to 8485 05:56:53,680 --> 05:56:57,558 be using TSX or jsx or TS uh so I'm just 8486 05:56:57,558 --> 05:56:59,280 going to move all that and I'm just 8487 05:56:59,280 --> 05:57:02,160 going to close this out so we should be 8488 05:57:02,160 --> 05:57:04,958 done with our setup uh we do need to 8489 05:57:04,958 --> 05:57:06,920 actually go into our package Json file 8490 05:57:06,920 --> 05:57:09,478 and for some reason uh when we ran the 8491 05:57:09,478 --> 05:57:11,400 just configuration it did not override 8492 05:57:11,400 --> 05:57:14,040 this test script so let's go ahead and 8493 05:57:14,040 --> 05:57:15,798 do that real quick whoops let's go ahead 8494 05:57:15,798 --> 05:57:18,558 and do that so all we have to do is just 8495 05:57:18,558 --> 05:57:21,200 have this test script and then have 8496 05:57:21,200 --> 05:57:24,478 execute this just binary so this will 8497 05:57:24,478 --> 05:57:26,080 look inside your node modules inside 8498 05:57:26,080 --> 05:57:28,558 this bin folder and it will look for 8499 05:57:28,558 --> 05:57:31,280 this just binary right over here and 8500 05:57:31,280 --> 05:57:33,160 it'll just run that command and that's 8501 05:57:33,160 --> 05:57:35,240 what will actually run your test for you 8502 05:57:35,240 --> 05:57:38,798 when you are ready so let's go ahead and 8503 05:57:38,798 --> 05:57:41,360 write a very very simple test and kind 8504 05:57:41,360 --> 05:57:44,558 of like explore the just API so what 8505 05:57:44,558 --> 05:57:46,000 we're going to do inside our source 8506 05:57:46,000 --> 05:57:47,638 folder we're going to create a new 8507 05:57:47,638 --> 05:57:51,160 folder and I'm going to name it uncore 8508 05:57:51,160 --> 05:57:54,040 testscore and this is industry standard 8509 05:57:54,040 --> 05:57:55,360 and this is where you're going to place 8510 05:57:55,360 --> 05:57:58,160 all of your tests so you need to create 8511 05:57:58,160 --> 05:58:00,878 a file and then you want to name the 8512 05:58:00,878 --> 05:58:03,558 file of your test ideally something that 8513 05:58:03,558 --> 05:58:06,120 resembles the actual file name so for 8514 05:58:06,120 --> 05:58:07,638 example if I were 8515 05:58:07,638 --> 05:58:11,200 testing let's say something inside my 8516 05:58:11,200 --> 05:58:14,200 users. MJS file inside the routes folder 8517 05:58:14,200 --> 05:58:15,958 then I would want to name the test file 8518 05:58:15,958 --> 05:58:18,718 something like users Dot and then the 8519 05:58:18,718 --> 05:58:21,558 extension here is where you want to name 8520 05:58:21,558 --> 05:58:25,400 it either dopc or test and then end it 8521 05:58:25,400 --> 05:58:28,680 with JS like this so if you were using 8522 05:58:28,680 --> 05:58:31,958 typescript it would be dope. TS or 8523 05:58:31,958 --> 05:58:33,878 test.ts since we're just using 8524 05:58:33,878 --> 05:58:37,878 JavaScript I'm going to name it dope. JS 8525 05:58:37,878 --> 05:58:41,360 or. test.js doesn't matter which 8526 05:58:41,360 --> 05:58:44,000 extension either test or spec that you 8527 05:58:44,000 --> 05:58:47,200 want to use I personally like to use 8528 05:58:47,200 --> 05:58:49,280 spec so I'm just going to go ahead and 8529 05:58:49,280 --> 05:58:51,760 do that and now it's going to go ahead 8530 05:58:51,760 --> 05:58:54,360 and create this file for me okay let's 8531 05:58:54,360 --> 05:58:57,160 just explore the just API very quickly 8532 05:58:57,160 --> 05:58:58,760 and then I'll get to actually testing 8533 05:58:58,760 --> 05:59:02,360 some of our express route handlers so we 8534 05:59:02,360 --> 05:59:04,280 have this described function and now 8535 05:59:04,280 --> 05:59:07,120 you'll notice that right now it isn't 8536 05:59:07,120 --> 05:59:09,280 going to be imported from anywhere by 8537 05:59:09,280 --> 05:59:12,638 default just 8538 05:59:13,400 --> 05:59:15,680 actually so the very first function that 8539 05:59:15,680 --> 05:59:17,200 you're going to be using that comes from 8540 05:59:17,200 --> 05:59:20,160 the just API is this describe function 8541 05:59:20,160 --> 05:59:22,040 right over here now one thing that I do 8542 05:59:22,040 --> 05:59:24,200 want to mention is that you could 8543 05:59:24,200 --> 05:59:25,160 actually 8544 05:59:25,160 --> 05:59:29,520 import this describe function from @jg 8545 05:59:29,520 --> 05:59:30,760 globals like 8546 05:59:30,760 --> 05:59:32,878 this and then you can just use it 8547 05:59:32,878 --> 05:59:34,878 accordingly like this the thing is 8548 05:59:34,878 --> 05:59:36,958 though just typically is configured 8549 05:59:36,958 --> 05:59:38,760 globally but you can see right now for 8550 05:59:38,760 --> 05:59:40,160 some reason it is not configured 8551 05:59:40,160 --> 05:59:42,280 globally so I'll show you very quickly 8552 05:59:42,280 --> 05:59:45,760 how we can actually configure just to 8553 05:59:45,760 --> 05:59:48,760 have this describe function and other 8554 05:59:48,760 --> 05:59:53,040 functions like it test before all for 8555 05:59:53,040 --> 05:59:55,760 each which are hooks these two are hook 8556 05:59:55,760 --> 05:59:57,200 functions I'm going to show you how to 8557 05:59:57,200 --> 05:59:58,920 configure this so that it it's actually 8558 05:59:58,920 --> 06:00:00,400 Global so we don't have to manually 8559 06:00:00,400 --> 06:00:03,320 import it in our files so what you need 8560 06:00:03,320 --> 06:00:06,160 to do is you need to First create a new 8561 06:00:06,160 --> 06:00:08,360 file inside your root directory called 8562 06:00:08,360 --> 06:00:12,638 JSC config.js and then you're going to 8563 06:00:12,638 --> 06:00:15,798 set this object and have this type 8564 06:00:15,798 --> 06:00:18,440 acquisition property which is an object 8565 06:00:18,440 --> 06:00:20,600 and then you're going to add this 8566 06:00:20,600 --> 06:00:23,320 include property which is an array and 8567 06:00:23,320 --> 06:00:25,320 then you're going to want to add just 8568 06:00:25,320 --> 06:00:27,760 okay now we don't actually have any any 8569 06:00:27,760 --> 06:00:29,878 types installed I believe let me just 8570 06:00:29,878 --> 06:00:31,798 double check yep we don't have types for 8571 06:00:31,798 --> 06:00:33,878 just installed is what I meant to say so 8572 06:00:33,878 --> 06:00:36,958 we need to actually we need to actually 8573 06:00:36,958 --> 06:00:39,638 install those types in order for us to 8574 06:00:39,638 --> 06:00:42,400 get it to work so what I'll do and you 8575 06:00:42,400 --> 06:00:45,000 can kind of see that now it actually 8576 06:00:45,000 --> 06:00:48,520 recognizes uh just. describe globally 8577 06:00:48,520 --> 06:00:51,680 but just to be safe make sure you 8578 06:00:51,680 --> 06:00:53,840 install 8579 06:00:53,840 --> 06:00:58,400 the at types slj package like that 8580 06:00:58,400 --> 06:00:59,360 okay even though we're not using 8581 06:00:59,360 --> 06:01:00,680 typescript you can still use this 8582 06:01:00,680 --> 06:01:04,000 package to have types set up globally 8583 06:01:04,000 --> 06:01:06,878 like this okay so now you can see that 8584 06:01:06,878 --> 06:01:09,360 the whoops right over here we have the 8585 06:01:09,360 --> 06:01:11,200 just types over here and it should 8586 06:01:11,200 --> 06:01:13,400 recognize them globally without any 8587 06:01:13,400 --> 06:01:17,400 issues so going back to our users. spc. 8588 06:01:17,400 --> 06:01:20,520 JS file I'm going to call this just 8589 06:01:20,520 --> 06:01:23,440 describe function like this and again no 8590 06:01:23,440 --> 06:01:26,240 no need to import it and this describe 8591 06:01:26,240 --> 06:01:28,520 function is used used to create test 8592 06:01:28,520 --> 06:01:31,000 Suites and basically all that means is 8593 06:01:31,000 --> 06:01:33,400 it allows you to group together your 8594 06:01:33,400 --> 06:01:36,160 tests into collections so you can better 8595 06:01:36,160 --> 06:01:38,760 organize and understand what's going on 8596 06:01:38,760 --> 06:01:40,440 so typically you want to give your test 8597 06:01:40,440 --> 06:01:43,240 Suite a generic name that represents 8598 06:01:43,240 --> 06:01:46,878 what you're trying to actually test for 8599 06:01:46,878 --> 06:01:47,920 so for 8600 06:01:47,920 --> 06:01:52,478 example if I want to let's see if I 8601 06:01:52,478 --> 06:01:55,680 wanted to test for a basic I'm trying to 8602 06:01:55,680 --> 06:01:56,360 look 8603 06:01:56,360 --> 06:01:57,920 for 8604 06:01:57,920 --> 06:02:01,280 something that we can actually test uh 8605 06:02:01,280 --> 06:02:02,798 let's 8606 06:02:02,798 --> 06:02:05,320 try let's do this endpoint so let's say 8607 06:02:05,320 --> 06:02:06,920 for example if I wanted to test this 8608 06:02:06,920 --> 06:02:09,320 endpoint where I wanted to get a user by 8609 06:02:09,320 --> 06:02:13,120 ID then I would just first name the test 8610 06:02:13,120 --> 06:02:16,478 Suite something like uh get users and 8611 06:02:16,478 --> 06:02:18,760 then pass in this callback function so 8612 06:02:18,760 --> 06:02:20,280 inside this callback function is where 8613 06:02:20,280 --> 06:02:22,240 you will actually create your test 8614 06:02:22,240 --> 06:02:24,718 closures and those are basically just 8615 06:02:24,718 --> 06:02:27,760 going to be what's actually going to run 8616 06:02:27,760 --> 06:02:29,638 your test and you're going to implement 8617 06:02:29,638 --> 06:02:31,958 the test logic in there so to create a 8618 06:02:31,958 --> 06:02:34,080 test closure you can use either the it 8619 06:02:34,080 --> 06:02:36,478 function or the test function they both 8620 06:02:36,478 --> 06:02:38,878 work the exact same way so I'm just 8621 06:02:38,878 --> 06:02:40,680 going to use it and then you want to 8622 06:02:40,680 --> 06:02:43,080 give the test name so I'll just say 8623 06:02:43,080 --> 06:02:47,558 something like should get user by ID and 8624 06:02:47,558 --> 06:02:49,638 then you want to pass in a callback 8625 06:02:49,638 --> 06:02:51,920 function and inside the Callback 8626 06:02:51,920 --> 06:02:53,840 function of your test closure so the 8627 06:02:53,840 --> 06:02:55,878 second argument for this it function 8628 06:02:55,878 --> 06:02:57,360 this is where you're actually going to 8629 06:02:57,360 --> 06:02:59,558 to invoke the function that you want to 8630 06:02:59,558 --> 06:03:01,440 test and then you want to actually write 8631 06:03:01,440 --> 06:03:03,760 assertions and verify that certain 8632 06:03:03,760 --> 06:03:07,080 things occurred okay so here's the next 8633 06:03:07,080 --> 06:03:09,200 part though we need to actually write a 8634 06:03:09,200 --> 06:03:12,120 test for some of our functions so for 8635 06:03:12,120 --> 06:03:14,280 example let's say I wanted to test this 8636 06:03:14,280 --> 06:03:17,920 router. getet API users ID endpoint so 8637 06:03:17,920 --> 06:03:20,400 this is getting a user by ID and this is 8638 06:03:20,400 --> 06:03:23,320 actually one common confusion that many 8639 06:03:23,320 --> 06:03:25,160 developers begin to have when it comes 8640 06:03:25,160 --> 06:03:27,400 to running tests they look at this and 8641 06:03:27,400 --> 06:03:29,000 they're like well how do I actually test 8642 06:03:29,000 --> 06:03:31,120 this whole endpoint because there's so 8643 06:03:31,120 --> 06:03:32,718 many things that are going on do I need 8644 06:03:32,718 --> 06:03:35,718 to actually make an HTTP post request or 8645 06:03:35,718 --> 06:03:37,760 get request to the actual endpoint that 8646 06:03:37,760 --> 06:03:40,360 I'm trying to test so first of all when 8647 06:03:40,360 --> 06:03:42,478 it comes to unit testing you want to 8648 06:03:42,478 --> 06:03:44,320 actually test only a single piece of 8649 06:03:44,320 --> 06:03:47,200 your code look for individual functions 8650 06:03:47,200 --> 06:03:49,600 that might be calling other functions 8651 06:03:49,600 --> 06:03:52,478 maybe they are calling a database maybe 8652 06:03:52,478 --> 06:03:54,120 they are calling an external API 8653 06:03:54,120 --> 06:03:56,798 whatever it is you want to test that 8654 06:03:56,798 --> 06:03:59,280 single function and you want to make 8655 06:03:59,280 --> 06:04:00,840 sure that the function does what it's 8656 06:04:00,840 --> 06:04:03,120 supposed to do so ideally you want to 8657 06:04:03,120 --> 06:04:05,200 verify that the function is hitting the 8658 06:04:05,200 --> 06:04:07,080 right IFL statements the conditions 8659 06:04:07,080 --> 06:04:08,920 correctly and then based on those 8660 06:04:08,920 --> 06:04:10,360 conditions that are being executed you 8661 06:04:10,360 --> 06:04:11,718 want to make sure that it is returning 8662 06:04:11,718 --> 06:04:14,000 the correct response so to give you a 8663 06:04:14,000 --> 06:04:16,760 very simple example let's take a look at 8664 06:04:16,760 --> 06:04:19,240 uh this function right of here okay so 8665 06:04:19,240 --> 06:04:21,240 you can see that this function is our 8666 06:04:21,240 --> 06:04:24,920 request Handler and it first takes this 8667 06:04:24,920 --> 06:04:27,760 F user index property from from request 8668 06:04:27,760 --> 06:04:30,280 and this comes from our resolve index by 8669 06:04:30,280 --> 06:04:32,000 user ID middleware so don't worry so 8670 06:04:32,000 --> 06:04:33,798 much about that right now but we grab 8671 06:04:33,798 --> 06:04:35,798 this property from the request object 8672 06:04:35,798 --> 06:04:39,000 and then we try to reference mock us 8673 06:04:39,000 --> 06:04:42,320 array and Index this user so we can grab 8674 06:04:42,320 --> 06:04:44,080 the user with its index so if the user 8675 06:04:44,080 --> 06:04:46,400 is not found in this mock users array 8676 06:04:46,400 --> 06:04:48,400 it's going to go ahead and return a 8677 06:04:48,400 --> 06:04:51,600 response and send a status of 404 if the 8678 06:04:51,600 --> 06:04:53,120 user is in fact found then it's going to 8679 06:04:53,120 --> 06:04:55,360 go ahead and call response. send find 8680 06:04:55,360 --> 06:04:57,360 user so what you need to understand is 8681 06:04:57,360 --> 06:04:59,120 this function is pretty simple it 8682 06:04:59,120 --> 06:05:02,360 doesn't really do too much stuff but the 8683 06:05:02,360 --> 06:05:04,160 point is is that we want to make sure 8684 06:05:04,160 --> 06:05:06,680 that we understand what are the possible 8685 06:05:06,680 --> 06:05:08,520 outcomes of our function in this 8686 06:05:08,520 --> 06:05:10,600 situation there are two possible 8687 06:05:10,600 --> 06:05:13,600 outcomes only only two either the user 8688 06:05:13,600 --> 06:05:15,520 is not found and we return a status of 8689 06:05:15,520 --> 06:05:18,400 404 or the user is found and then we 8690 06:05:18,400 --> 06:05:20,120 just send back the user that was found 8691 06:05:20,120 --> 06:05:22,360 there's only two outcomes sometimes your 8692 06:05:22,360 --> 06:05:24,958 functions might have many different 8693 06:05:24,958 --> 06:05:27,478 outcomes it might output many different 8694 06:05:27,478 --> 06:05:29,718 types of results based on the condition 8695 06:05:29,718 --> 06:05:31,240 and there are some general principles 8696 06:05:31,240 --> 06:05:33,638 when it comes to writing your code in a 8697 06:05:33,638 --> 06:05:35,240 way that makes it easily testable 8698 06:05:35,240 --> 06:05:38,080 because writing tests also depends on 8699 06:05:38,080 --> 06:05:40,360 the code quality as well if you have a 8700 06:05:40,360 --> 06:05:42,600 bunch of spaghetti code you're making it 8701 06:05:42,600 --> 06:05:44,798 really difficult to actually write tests 8702 06:05:44,798 --> 06:05:45,958 okay the purpose is not to actually 8703 06:05:45,958 --> 06:05:49,400 write the most complex code but to 8704 06:05:49,400 --> 06:05:52,520 separate concerns and make sure that you 8705 06:05:52,520 --> 06:05:54,840 have each function doing the main 8706 06:05:54,840 --> 06:05:57,200 responsibility and anything else you 8707 06:05:57,200 --> 06:05:59,558 turn it into a dependency function is 8708 06:05:59,558 --> 06:06:01,400 what we like to call it and that 8709 06:06:01,400 --> 06:06:02,520 basically just means that you're 8710 06:06:02,520 --> 06:06:04,478 invoking another function and letting 8711 06:06:04,478 --> 06:06:07,558 that function take care of the work and 8712 06:06:07,558 --> 06:06:09,160 then once that function is done it'll 8713 06:06:09,160 --> 06:06:11,160 return back the result and then you can 8714 06:06:11,160 --> 06:06:14,120 proceed so we'll start off with a very 8715 06:06:14,120 --> 06:06:16,478 easy example we'll use 8716 06:06:16,478 --> 06:06:19,478 this call function over here but now 8717 06:06:19,478 --> 06:06:20,798 here's the other problem though okay 8718 06:06:20,798 --> 06:06:22,718 when we actually want to write tests we 8719 06:06:22,718 --> 06:06:25,360 want to be able to import the function 8720 06:06:25,360 --> 06:06:26,840 or class or whatever it is that we're 8721 06:06:26,840 --> 06:06:30,280 try trying to test into our test file so 8722 06:06:30,280 --> 06:06:32,718 how do we import this Anonymous function 8723 06:06:32,718 --> 06:06:35,878 there's no way so what we need to do is 8724 06:06:35,878 --> 06:06:38,400 we need to copy this function and we 8725 06:06:38,400 --> 06:06:40,840 need to move it 8726 06:06:40,840 --> 06:06:43,160 into uh we we need to turn it into a 8727 06:06:43,160 --> 06:06:45,718 named function so what I'll do is I'm 8728 06:06:45,718 --> 06:06:47,040 going I'm going to actually create a new 8729 06:06:47,040 --> 06:06:49,798 folder called 8730 06:06:50,000 --> 06:06:51,680 handlers and then I'm going to create a 8731 06:06:51,680 --> 06:06:54,400 new file and I'm going to call this 8732 06:06:54,400 --> 06:06:55,920 users. 8733 06:06:55,920 --> 06:06:57,718 MJS 8734 06:06:57,718 --> 06:06:59,040 and then what I'm going to do is I'm 8735 06:06:59,040 --> 06:07:01,638 going to paste this function right here 8736 06:07:01,638 --> 06:07:03,240 and then right before the parenthesis 8737 06:07:03,240 --> 06:07:07,160 I'm going to do export con and since 8738 06:07:07,160 --> 06:07:09,920 this request handler was trying to get 8739 06:07:09,920 --> 06:07:12,040 the user by ID I'm going to go ahead and 8740 06:07:12,040 --> 06:07:17,280 call the function get user by ID 8741 06:07:17,280 --> 06:07:19,478 Handler and let's make sure that we are 8742 06:07:19,478 --> 06:07:21,718 importing the 8743 06:07:21,718 --> 06:07:24,840 correct value so we need mock users so 8744 06:07:24,840 --> 06:07:27,080 I'm going to import that up here and and 8745 06:07:27,080 --> 06:07:28,760 I think that's all we 8746 06:07:28,760 --> 06:07:31,280 need so let's go ahead and copy this 8747 06:07:31,280 --> 06:07:33,840 function and we want to make sure that 8748 06:07:33,840 --> 06:07:37,320 we pass it as an argument to our router. 8749 06:07:37,320 --> 06:07:39,520 getet method call so at the end like 8750 06:07:39,520 --> 06:07:40,478 this and then we're going to make sure 8751 06:07:40,478 --> 06:07:44,280 we import that up top over there as 8752 06:07:44,280 --> 06:07:47,520 well and then now we have 8753 06:07:47,520 --> 06:07:50,840 our function being passed as an argument 8754 06:07:50,840 --> 06:07:52,240 it's still going to work the same exact 8755 06:07:52,240 --> 06:07:53,558 way the only difference now is that we 8756 06:07:53,558 --> 06:07:57,000 can actually import it wherever we want 8757 06:07:57,000 --> 06:07:59,680 so I'm going to go into my users. spc. 8758 06:07:59,680 --> 06:08:02,000 Js and what I'm going to do is first I'm 8759 06:08:02,000 --> 06:08:02,920 going to 8760 06:08:02,920 --> 06:08:08,558 import that function so get user by ID 8761 06:08:08,558 --> 06:08:10,920 Handler and so this is going to be our 8762 06:08:10,920 --> 06:08:14,280 system under test or function under test 8763 06:08:14,280 --> 06:08:16,120 and now we want to actually call this 8764 06:08:16,120 --> 06:08:18,400 function okay but we need to also 8765 06:08:18,400 --> 06:08:21,360 remember that this function does take in 8766 06:08:21,360 --> 06:08:23,200 two arguments it takes in a request and 8767 06:08:23,200 --> 06:08:26,000 a response so this is where you start to 8768 06:08:26,000 --> 06:08:28,520 learn how to actually create fake data 8769 06:08:28,520 --> 06:08:31,478 or mocks so that way you can use them 8770 06:08:31,478 --> 06:08:33,760 for your test subjects in this case are 8771 06:08:33,760 --> 06:08:38,040 get user by ID handle function so when 8772 06:08:38,040 --> 06:08:39,718 it comes to creating mocks there are 8773 06:08:39,718 --> 06:08:41,440 ways to be smart with 8774 06:08:41,440 --> 06:08:44,478 it so what we'll do is we'll create two 8775 06:08:44,478 --> 06:08:47,840 simple objects let's call this mock 8776 06:08:47,840 --> 06:08:52,520 request and then mock 8777 06:08:53,200 --> 06:08:56,600 response and remember this the request 8778 06:08:56,600 --> 06:08:59,718 that response object resembles the 8779 06:08:59,718 --> 06:09:03,240 actual request type in 8780 06:09:03,240 --> 06:09:05,718 expressjs okay so if you were to 8781 06:09:05,718 --> 06:09:10,200 actually look at let me go over 8782 06:09:10,200 --> 06:09:12,798 to here because we're using JavaScript 8783 06:09:12,798 --> 06:09:15,440 okay so if I were to actually rightclick 8784 06:09:15,440 --> 06:09:17,920 the type definition of this request you 8785 06:09:17,920 --> 06:09:19,360 can see that we have this 8786 06:09:19,360 --> 06:09:22,760 interface and you can see that there are 8787 06:09:22,760 --> 06:09:24,440 a lot of 8788 06:09:24,440 --> 06:09:28,240 properties on the request object itself 8789 06:09:28,240 --> 06:09:31,718 okay you can see that we have uh host 8790 06:09:31,718 --> 06:09:34,520 cookies method prams these were similar 8791 06:09:34,520 --> 06:09:37,160 methods or properties that we looked at 8792 06:09:37,160 --> 06:09:39,600 earlier signed cookies as 8793 06:09:39,600 --> 06:09:41,478 well now you're probably wondering well 8794 06:09:41,478 --> 06:09:43,920 when I create my mock object like my 8795 06:09:43,920 --> 06:09:47,600 mock my mock request do I need to also 8796 06:09:47,600 --> 06:09:49,798 include all of those fields and the 8797 06:09:49,798 --> 06:09:52,520 answer to that question is actually no 8798 06:09:52,520 --> 06:09:55,040 so what you can do is you can only 8799 06:09:55,040 --> 06:09:57,760 Define the fields that you need 8800 06:09:57,760 --> 06:09:59,440 okay and again this works because we're 8801 06:09:59,440 --> 06:10:00,958 using JavaScript if you're using 8802 06:10:00,958 --> 06:10:04,080 typescript then you can use uh casting 8803 06:10:04,080 --> 06:10:05,680 for example you can type annotate the 8804 06:10:05,680 --> 06:10:08,240 request and force it to be an actual 8805 06:10:08,240 --> 06:10:10,080 request type even though it is missing 8806 06:10:10,080 --> 06:10:12,400 all of its Fields but since we're using 8807 06:10:12,400 --> 06:10:13,958 JavaScript we can do literally whatever 8808 06:10:13,958 --> 06:10:16,878 we want so for our mock request we want 8809 06:10:16,878 --> 06:10:18,680 to include the necessary 8810 06:10:18,680 --> 06:10:21,200 properties that our code is actually 8811 06:10:21,200 --> 06:10:23,958 referencing on the request object so 8812 06:10:23,958 --> 06:10:25,120 this is where you need to actually look 8813 06:10:25,120 --> 06:10:27,320 at your code look look at the function 8814 06:10:27,320 --> 06:10:29,400 that's being tested and you need to see 8815 06:10:29,400 --> 06:10:31,760 okay what is actually being referenced 8816 06:10:31,760 --> 06:10:34,240 on the request object so I can see right 8817 06:10:34,240 --> 06:10:37,478 over here one property is being 8818 06:10:37,478 --> 06:10:39,718 destructured from the request object so 8819 06:10:39,718 --> 06:10:41,440 we need to make sure that this whatever 8820 06:10:41,440 --> 06:10:43,240 this fine user index is we know that 8821 06:10:43,240 --> 06:10:45,200 it's a number but whatever it is we need 8822 06:10:45,200 --> 06:10:47,840 to make sure we Define that in the mock 8823 06:10:47,840 --> 06:10:50,360 request object um and it seems like 8824 06:10:50,360 --> 06:10:52,798 that's the only property that we are 8825 06:10:52,798 --> 06:10:54,840 referencing so I know that this is a 8826 06:10:54,840 --> 06:10:57,280 number because in case of you didn't 8827 06:10:57,280 --> 06:10:59,798 watch the early parts of the tutorial 8828 06:10:59,798 --> 06:11:01,920 this resolve index by user ID middleware 8829 06:11:01,920 --> 06:11:04,080 function what it does is it will go 8830 06:11:04,080 --> 06:11:06,520 ahead and grab the ID from the route 8831 06:11:06,520 --> 06:11:08,080 parameter and it'll take care of parsing 8832 06:11:08,080 --> 06:11:10,200 it to an actual integer and then it'll 8833 06:11:10,200 --> 06:11:13,160 attach it to the request object so what 8834 06:11:13,160 --> 06:11:17,600 I'll do is inside my mock request I'm 8835 06:11:17,600 --> 06:11:20,920 going to go ahead and Define find or 8836 06:11:20,920 --> 06:11:22,638 what was it called again uh find user 8837 06:11:22,638 --> 06:11:25,558 index yep find user 8838 06:11:25,558 --> 06:11:27,840 index and then you can give it literally 8839 06:11:27,840 --> 06:11:30,798 any number you want you can give it 1 2 8840 06:11:30,798 --> 06:11:33,840 3 100 literally anything you 8841 06:11:33,840 --> 06:11:36,600 want but here's the thing though we want 8842 06:11:36,600 --> 06:11:39,400 to give it an actual meaningful value 8843 06:11:39,400 --> 06:11:43,120 that is going to correlate with our code 8844 06:11:43,120 --> 06:11:46,080 so if you look at the rest of your code 8845 06:11:46,080 --> 06:11:48,320 you can see that fine user index is 8846 06:11:48,320 --> 06:11:51,520 being used to retrieve an element from 8847 06:11:51,520 --> 06:11:53,440 this mock users 8848 06:11:53,440 --> 06:11:56,120 array and this is our mock users array 8849 06:11:56,120 --> 06:11:57,200 right over here 8850 06:11:57,200 --> 06:11:59,680 here and if I were so for example if I 8851 06:11:59,680 --> 06:12:01,638 were to just give like just some bizarre 8852 06:12:01,638 --> 06:12:04,360 number like 100 it's pretty obvious that 8853 06:12:04,360 --> 06:12:07,840 that number would not actually give us 8854 06:12:07,840 --> 06:12:09,680 back anything inside mock users because 8855 06:12:09,680 --> 06:12:12,600 there is no element at subscript 100 8856 06:12:12,600 --> 06:12:14,958 that would give us back a 8857 06:12:14,958 --> 06:12:17,440 user okay so let's just do something 8858 06:12:17,440 --> 06:12:20,718 simple for now let's just do one so that 8859 06:12:20,718 --> 06:12:22,840 way I know that it'll give me back the 8860 06:12:22,840 --> 06:12:26,718 user Jack okay all right so we're done 8861 06:12:26,718 --> 06:12:28,120 with the mock request 8862 06:12:28,120 --> 06:12:30,240 object now let's go into the mock 8863 06:12:30,240 --> 06:12:32,958 response so this is going to be our 8864 06:12:32,958 --> 06:12:34,760 response object and now we have again we 8865 06:12:34,760 --> 06:12:35,878 have to do the same thing we have to 8866 06:12:35,878 --> 06:12:39,760 look at our code and look at what 8867 06:12:39,760 --> 06:12:42,478 response uh is and we have to also see 8868 06:12:42,478 --> 06:12:45,040 what is being referenced on response so 8869 06:12:45,040 --> 06:12:47,360 right over here I can see that response 8870 06:12:47,360 --> 06:12:50,000 is being referenced right over here and 8871 06:12:50,000 --> 06:12:52,680 the send status this is a method on the 8872 06:12:52,680 --> 06:12:55,160 response object is being called okay so 8873 06:12:55,160 --> 06:12:57,920 I know that send status is method so 8874 06:12:57,920 --> 06:13:00,520 that means that inside the mock response 8875 06:13:00,520 --> 06:13:04,080 object we should set this send status 8876 06:13:04,080 --> 06:13:06,280 property and have it mapped to a 8877 06:13:06,280 --> 06:13:07,920 function so you might think that it 8878 06:13:07,920 --> 06:13:10,000 would look something like this right 8879 06:13:10,000 --> 06:13:13,200 well no so it is going to be a function 8880 06:13:13,200 --> 06:13:15,160 but it's not going to be a regular 8881 06:13:15,160 --> 06:13:17,320 function it's going to be a mock 8882 06:13:17,320 --> 06:13:20,520 function so what we can do in just is we 8883 06:13:20,520 --> 06:13:24,040 can use this just namespace and I can 8884 06:13:24,040 --> 06:13:26,958 reference just. FN like this 8885 06:13:26,958 --> 06:13:28,638 and what this will do is it'll create a 8886 06:13:28,638 --> 06:13:31,558 mock function for me so this is where we 8887 06:13:31,558 --> 06:13:35,240 actually want to call a mocked function 8888 06:13:35,240 --> 06:13:38,000 of send status on the response object 8889 06:13:38,000 --> 06:13:41,200 and not the actual send status method 8890 06:13:41,200 --> 06:13:46,000 itself okay so we use just. FN to apply 8891 06:13:46,000 --> 06:13:48,840 this mock function to S status and then 8892 06:13:48,840 --> 06:13:51,120 we'll do the same thing for the send 8893 06:13:51,120 --> 06:13:54,160 method as well so let's do send and then 8894 06:13:54,160 --> 06:13:56,878 this will be just. FN 8895 06:13:56,878 --> 06:14:02,000 okay all right so now what we want to do 8896 06:14:02,000 --> 06:14:04,120 is we want to actually take both of our 8897 06:14:04,120 --> 06:14:06,478 objects mock request and mock response 8898 06:14:06,478 --> 06:14:08,798 and pass them as arguments to get user 8899 06:14:08,798 --> 06:14:11,718 by ID Handler so let's do that so mock 8900 06:14:11,718 --> 06:14:15,280 request and mock response okay so now if 8901 06:14:15,280 --> 06:14:17,400 I run my test right now the test 8902 06:14:17,400 --> 06:14:18,798 actually should just pass because we're 8903 06:14:18,798 --> 06:14:20,760 not actually testing for anything so in 8904 06:14:20,760 --> 06:14:23,478 our terminal we'll run npm run test and 8905 06:14:23,478 --> 06:14:25,280 this will execute that just 8906 06:14:25,280 --> 06:14:29,000 binary and you can see that our test 8907 06:14:29,000 --> 06:14:31,718 runs and it passes and just very quickly 8908 06:14:31,718 --> 06:14:34,600 I want to show you what would happen if 8909 06:14:34,600 --> 06:14:36,320 I were 8910 06:14:36,320 --> 06:14:38,360 to go 8911 06:14:38,360 --> 06:14:40,718 over here and just kind of like remove 8912 06:14:40,718 --> 06:14:41,520 this 8913 06:14:41,520 --> 06:14:45,958 part entirely and you'll see that it's 8914 06:14:45,958 --> 06:14:47,878 going to go ahead and 8915 06:14:47,878 --> 06:14:51,280 complain about all the stuff so that's 8916 06:14:51,280 --> 06:14:53,040 the reason why we needed this transform 8917 06:14:53,040 --> 06:14:56,760 part okay and again we can also run 8918 06:14:56,760 --> 06:14:58,520 our 8919 06:14:58,520 --> 06:15:01,040 application completely separate from our 8920 06:15:01,040 --> 06:15:03,478 tests like 8921 06:15:03,478 --> 06:15:07,040 this and it still would work just fine 8922 06:15:07,040 --> 06:15:10,400 okay so hopefully the setup with Babel 8923 06:15:10,400 --> 06:15:12,680 and everything else wasn't too complex 8924 06:15:12,680 --> 06:15:14,200 and we can still proceed to use es 8925 06:15:14,200 --> 06:15:16,440 modules and write tests okay you can see 8926 06:15:16,440 --> 06:15:17,600 it's currently running without any 8927 06:15:17,600 --> 06:15:20,680 issues well awesome so let's go ahead 8928 06:15:20,680 --> 06:15:22,718 and go back to our test and let's 8929 06:15:22,718 --> 06:15:25,840 actually write our first assertion so 8930 06:15:25,840 --> 06:15:29,360 again let's look at our code again okay 8931 06:15:29,360 --> 06:15:31,520 and what we can see is there's two 8932 06:15:31,520 --> 06:15:33,718 outputs there or two things that could 8933 06:15:33,718 --> 06:15:36,200 happen either we send a status of 404 or 8934 06:15:36,200 --> 06:15:39,160 we send the user back so this is where 8935 06:15:39,160 --> 06:15:41,360 we need to understand okay well what 8936 06:15:41,360 --> 06:15:44,958 should happen inside this test is 8937 06:15:44,958 --> 06:15:46,878 whatever it is that we wanted to happen 8938 06:15:46,878 --> 06:15:48,638 Okay so we have to figure out what it is 8939 06:15:48,638 --> 06:15:50,680 that we're trying to test for so in my 8940 06:15:50,680 --> 06:15:52,360 situation let's say I want to test to 8941 06:15:52,360 --> 06:15:54,718 ensure that a user was actually sent 8942 06:15:54,718 --> 06:15:57,240 back okay and and so we already set up 8943 06:15:57,240 --> 06:15:59,718 our mock request to have F user index to 8944 06:15:59,718 --> 06:16:02,958 be a value of one so when the function 8945 06:16:02,958 --> 06:16:06,718 is called it's going to use one as the 8946 06:16:06,718 --> 06:16:09,520 index and search for that mock 8947 06:16:09,520 --> 06:16:12,240 users so let's actually write our first 8948 06:16:12,240 --> 06:16:14,798 assertion so again we need to understand 8949 06:16:14,798 --> 06:16:17,160 what it is that we're trying to test in 8950 06:16:17,160 --> 06:16:19,718 my situation right now I want to test to 8951 06:16:19,718 --> 06:16:21,760 make sure that the user was actually 8952 06:16:21,760 --> 06:16:24,080 found and sent back with the response if 8953 06:16:24,080 --> 06:16:26,478 I look at the code okay how can I verify 8954 06:16:26,478 --> 06:16:29,360 that the user was sent back well what I 8955 06:16:29,360 --> 06:16:32,120 can do is I can write an assertion that 8956 06:16:32,120 --> 06:16:35,400 verifies that this response. send method 8957 06:16:35,400 --> 06:16:38,840 was called and it was passed it passed 8958 06:16:38,840 --> 06:16:41,320 this find user object as an argument and 8959 06:16:41,320 --> 06:16:42,718 I can also write another assertion to 8960 06:16:42,718 --> 06:16:46,040 verify that send status was not called 8961 06:16:46,040 --> 06:16:47,798 because send status would only be called 8962 06:16:47,798 --> 06:16:49,798 if fine user is 8963 06:16:49,798 --> 06:16:52,400 undefined and since we are setting fine 8964 06:16:52,400 --> 06:16:55,320 user index to be one it's going to use 8965 06:16:55,320 --> 06:16:57,840 that as the index to reference the user 8966 06:16:57,840 --> 06:17:01,160 at that position and mock users is just 8967 06:17:01,160 --> 06:17:03,478 this constant array that we have over 8968 06:17:03,478 --> 06:17:05,320 here so we can just reuse this data 8969 06:17:05,320 --> 06:17:06,558 because it's not like it's coming from a 8970 06:17:06,558 --> 06:17:10,520 database or some external API so we can 8971 06:17:10,520 --> 06:17:13,120 use this as mock data so let's go back 8972 06:17:13,120 --> 06:17:16,440 into our test and here's what we'll do 8973 06:17:16,440 --> 06:17:19,040 we're going to use this expect function 8974 06:17:19,040 --> 06:17:21,798 like this and we use the expect function 8975 06:17:21,798 --> 06:17:24,558 to actually test a value so for example 8976 06:17:24,558 --> 06:17:27,638 I want to test that mock 8977 06:17:27,638 --> 06:17:29,520 response. 8978 06:17:29,520 --> 06:17:31,920 send like this I want to test this 8979 06:17:31,920 --> 06:17:35,760 specific function and then at the end of 8980 06:17:35,760 --> 06:17:38,080 expect at the end of the uh parenthesis 8981 06:17:38,080 --> 06:17:40,520 we can use these just matchers and the 8982 06:17:40,520 --> 06:17:44,878 one that I want to use is to have been 8983 06:17:44,878 --> 06:17:47,958 called so what this means is we are 8984 06:17:47,958 --> 06:17:50,878 expecting mock response. send we're 8985 06:17:50,878 --> 06:17:52,680 expecting this function to have been 8986 06:17:52,680 --> 06:17:55,000 called okay so that asserts that the 8987 06:17:55,000 --> 06:17:57,400 function was called when we called this 8988 06:17:57,400 --> 06:17:59,840 get user by ID Handler so let's actually 8989 06:17:59,840 --> 06:18:01,760 test this out and see what happens so 8990 06:18:01,760 --> 06:18:05,080 let's run npm run 8991 06:18:05,600 --> 06:18:08,280 test okay and you see that it 8992 06:18:08,280 --> 06:18:11,478 passes okay so again this it takes some 8993 06:18:11,478 --> 06:18:13,160 time to really you know get used to some 8994 06:18:13,160 --> 06:18:15,920 of these matchers and knowing how to use 8995 06:18:15,920 --> 06:18:17,520 uh expect but as long as you just keep 8996 06:18:17,520 --> 06:18:19,360 practicing it's going to come in it's 8997 06:18:19,360 --> 06:18:21,040 going to come in handy and you're going 8998 06:18:21,040 --> 06:18:22,878 to develop an intuition behind it so you 8999 06:18:22,878 --> 06:18:25,478 can see that we definitely wrote an 9000 06:18:25,478 --> 06:18:28,160 assertion on this send function and we 9001 06:18:28,160 --> 06:18:30,760 asserted that this function was called 9002 06:18:30,760 --> 06:18:32,718 and you'll notice that right now if I 9003 06:18:32,718 --> 06:18:36,280 were to try to negate this matcher so if 9004 06:18:36,280 --> 06:18:39,160 I actually use doot do to have been 9005 06:18:39,160 --> 06:18:42,440 called that basically just asserts the 9006 06:18:42,440 --> 06:18:44,320 opposite so it's going to assert that 9007 06:18:44,320 --> 06:18:46,920 mock response.end was not called so if I 9008 06:18:46,920 --> 06:18:49,558 run the test it should fail because the 9009 06:18:49,558 --> 06:18:51,680 function itself was actually 9010 06:18:51,680 --> 06:18:53,600 called 9011 06:18:53,600 --> 06:18:56,120 okay hopefully that makes sense and then 9012 06:18:56,120 --> 06:18:58,558 it's sometimes good to do that because 9013 06:18:58,558 --> 06:19:01,360 it it helps you assure that you're not 9014 06:19:01,360 --> 06:19:03,400 having your test pass falsely if that 9015 06:19:03,400 --> 06:19:05,360 makes sense so you don't have any false 9016 06:19:05,360 --> 06:19:07,638 positives cuz sometimes your tests may 9017 06:19:07,638 --> 06:19:09,400 pass when they actually 9018 06:19:09,400 --> 06:19:11,520 shouldn't so let's actually be more 9019 06:19:11,520 --> 06:19:13,958 specific with our test so what I'll do 9020 06:19:13,958 --> 06:19:16,440 is I'll duplicate this line and I'm 9021 06:19:16,440 --> 06:19:21,200 going to use the matcher to have whoops 9022 06:19:21,200 --> 06:19:25,680 to have been called with and this is 9023 06:19:25,680 --> 06:19:27,840 what what I want to use whenever I want 9024 06:19:27,840 --> 06:19:31,600 to verify that a function was called 9025 06:19:31,600 --> 06:19:33,200 with specific 9026 06:19:33,200 --> 06:19:35,558 parameters so the send function is 9027 06:19:35,558 --> 06:19:38,478 called with only one 9028 06:19:38,478 --> 06:19:41,120 argument and this argument is a user 9029 06:19:41,120 --> 06:19:44,958 itself now again since we are using a 9030 06:19:44,958 --> 06:19:46,520 mock array I can very easily just 9031 06:19:46,520 --> 06:19:49,160 reference the array at the subscript of 9032 06:19:49,160 --> 06:19:51,840 whatever fine user index is which is one 9033 06:19:51,840 --> 06:19:54,280 so I can just go ahead and import this 9034 06:19:54,280 --> 06:19:56,958 mock users right over here 9035 06:19:56,958 --> 06:19:58,600 so let's import that and that gets 9036 06:19:58,600 --> 06:20:00,160 import up top here and then I'm just 9037 06:20:00,160 --> 06:20:03,600 going to access it like this okay or if 9038 06:20:03,600 --> 06:20:05,160 you wanted to you just literally copy 9039 06:20:05,160 --> 06:20:07,958 this whole thing and paste it in like 9040 06:20:07,958 --> 06:20:09,920 this either one will work the same way 9041 06:20:09,920 --> 06:20:12,360 cuz they're both going to be the same 9042 06:20:12,360 --> 06:20:14,200 object and the contents are the same 9043 06:20:14,200 --> 06:20:17,400 let's go ahead and run our test 9044 06:20:17,400 --> 06:20:20,478 now and you can see that it still passes 9045 06:20:20,478 --> 06:20:22,958 and you'll notice that if I tried to 9046 06:20:22,958 --> 06:20:24,440 let's say if I tried to pass in this 9047 06:20:24,440 --> 06:20:25,958 direct object and let's just say say if 9048 06:20:25,958 --> 06:20:28,600 I tried to change some property values 9049 06:20:28,600 --> 06:20:30,000 right of here let's change it let's 9050 06:20:30,000 --> 06:20:31,558 change the 9051 06:20:31,558 --> 06:20:33,320 username you're going to see that the 9052 06:20:33,320 --> 06:20:36,080 test actually fails because the value 9053 06:20:36,080 --> 06:20:38,638 that was actually passed as an argument 9054 06:20:38,638 --> 06:20:41,320 to the send function is not the same as 9055 06:20:41,320 --> 06:20:43,600 what we were asserting it to be so you 9056 06:20:43,600 --> 06:20:45,440 can see that the expected value we 9057 06:20:45,440 --> 06:20:48,680 expected uh username of Jack without the 9058 06:20:48,680 --> 06:20:51,840 K so jaac but then the actual received 9059 06:20:51,840 --> 06:20:55,240 value was username of Jack so again this 9060 06:20:55,240 --> 06:20:58,000 is good to make sure that your tests are 9061 06:20:58,000 --> 06:20:59,680 actually passing 9062 06:20:59,680 --> 06:21:01,638 correctly okay so let's run the test 9063 06:21:01,638 --> 06:21:04,120 again and it should just pass just like 9064 06:21:04,120 --> 06:21:06,600 that all right so hopefully this makes 9065 06:21:06,600 --> 06:21:07,958 sense and there's also one more 9066 06:21:07,958 --> 06:21:10,558 assertion that I want to show you so we 9067 06:21:10,558 --> 06:21:11,558 can 9068 06:21:11,558 --> 06:21:14,958 also check to see the amount of times 9069 06:21:14,958 --> 06:21:16,680 this send function was called so I can 9070 06:21:16,680 --> 06:21:20,680 do to have been called times and I can 9071 06:21:20,680 --> 06:21:23,280 specify the expected amount so if I want 9072 06:21:23,280 --> 06:21:25,120 to verify that this send function was 9073 06:21:25,120 --> 06:21:26,440 called 9074 06:21:26,440 --> 06:21:29,120 once then I can just pass one and it 9075 06:21:29,120 --> 06:21:31,798 should only be called once especially in 9076 06:21:31,798 --> 06:21:33,040 Express because if you try to call it 9077 06:21:33,040 --> 06:21:35,718 twice that will actually throw an 9078 06:21:35,718 --> 06:21:39,280 error so let's run the test again and of 9079 06:21:39,280 --> 06:21:41,440 course if I try to pass in like 9080 06:21:41,440 --> 06:21:44,120 two it'll throw an error or the test 9081 06:21:44,120 --> 06:21:46,478 will fail you can see over here the 9082 06:21:46,478 --> 06:21:49,478 actual received number of calls was one 9083 06:21:49,478 --> 06:21:52,120 okay so hopefully that makes 9084 06:21:52,120 --> 06:21:54,558 sense and one more thing as well that I 9085 06:21:54,558 --> 06:21:56,160 forgot to mention is that with this two 9086 06:21:56,160 --> 06:21:58,120 have been called with matcher you can 9087 06:21:58,120 --> 06:22:01,040 actually pass in the number of arguments 9088 06:22:01,040 --> 06:22:03,080 that the function is called with so we 9089 06:22:03,080 --> 06:22:04,840 only have one over here but if you had a 9090 06:22:04,840 --> 06:22:05,920 function that was called with like 9091 06:22:05,920 --> 06:22:08,718 multiple arguments you can pass in the 9092 06:22:08,718 --> 06:22:11,520 exact amount okay so let's go ahead and 9093 06:22:11,520 --> 06:22:14,120 move on let's write another test to 9094 06:22:14,120 --> 06:22:16,000 handle the second scenario of our 9095 06:22:16,000 --> 06:22:18,000 function so the other scenario would be 9096 06:22:18,000 --> 06:22:20,240 where fine user is not found and it 9097 06:22:20,240 --> 06:22:22,200 sends back a status code of 9098 06:22:22,200 --> 06:22:25,600 404 so I'm going to write another test 9099 06:22:25,600 --> 06:22:26,920 using the it function and I'm going to 9100 06:22:26,920 --> 06:22:28,878 goe and say it 9101 06:22:28,878 --> 06:22:31,718 should uh call send 9102 06:22:31,718 --> 06:22:34,520 status with 9103 06:22:34,520 --> 06:22:38,320 44 when user not 9104 06:22:38,320 --> 06:22:40,400 found okay you want to keep your test 9105 06:22:40,400 --> 06:22:43,400 names short and 9106 06:22:43,400 --> 06:22:45,680 simple and we're going to go ahead and 9107 06:22:45,680 --> 06:22:49,320 write our test so now here's the thing 9108 06:22:49,320 --> 06:22:52,440 though okay if I were to call get user 9109 06:22:52,440 --> 06:22:55,360 by ID Handler again and then if I were 9110 06:22:55,360 --> 06:22:58,520 to pass in the same mock objects it's 9111 06:22:58,520 --> 06:23:01,520 going to actually give us this same 9112 06:23:01,520 --> 06:23:04,040 response that we have up over here but 9113 06:23:04,040 --> 06:23:05,718 the problem is now we want to actually 9114 06:23:05,718 --> 06:23:10,280 get it so that we can set up our code to 9115 06:23:10,280 --> 06:23:12,798 ensure that the send status function is 9116 06:23:12,798 --> 06:23:16,120 called instead of the send function okay 9117 06:23:16,120 --> 06:23:18,120 if I were to just leave mock request 9118 06:23:18,120 --> 06:23:20,600 alone you can see that it's still using 9119 06:23:20,600 --> 06:23:23,200 the same value for fine user index and 9120 06:23:23,200 --> 06:23:25,080 in the code it'll actually look for a 9121 06:23:25,080 --> 06:23:27,400 user and return that user so it would 9122 06:23:27,400 --> 06:23:30,920 never hit this part at all line six so 9123 06:23:30,920 --> 06:23:33,000 we need to actually modify the mock 9124 06:23:33,000 --> 06:23:37,638 request object so what we need to do is 9125 06:23:37,638 --> 06:23:38,638 there there are a couple ways that you 9126 06:23:38,638 --> 06:23:42,120 can do this if you prefer to not modify 9127 06:23:42,120 --> 06:23:44,920 the object directly like this and set it 9128 06:23:44,920 --> 06:23:47,120 to a value like this you can always just 9129 06:23:47,120 --> 06:23:48,760 create like a copy of it so you can do 9130 06:23:48,760 --> 06:23:51,920 something like const and then you can 9131 06:23:51,920 --> 06:23:56,400 do mock request or let's just do 9132 06:23:56,400 --> 06:23:58,120 copy mock 9133 06:23:58,120 --> 06:24:00,638 request equals and then you can do 9134 06:24:00,638 --> 06:24:03,680 something like find user index is equal 9135 06:24:03,680 --> 06:24:07,040 to 100 and then of course you can also 9136 06:24:07,040 --> 06:24:09,878 destructure mock request because there 9137 06:24:09,878 --> 06:24:12,240 might be some properties that you want 9138 06:24:12,240 --> 06:24:15,638 to uh copy over to the copy of the mock 9139 06:24:15,638 --> 06:24:16,798 request so you can do something like 9140 06:24:16,798 --> 06:24:19,160 this and then passing copy mock request 9141 06:24:19,160 --> 06:24:21,718 to here for mock response we don't 9142 06:24:21,718 --> 06:24:22,760 really have anything that we need to 9143 06:24:22,760 --> 06:24:24,120 change because we still have the same 9144 06:24:24,120 --> 06:24:27,400 functions so it doesn't really matter so 9145 06:24:27,400 --> 06:24:29,878 we can leave that alone okay so we're 9146 06:24:29,878 --> 06:24:32,320 going to call get user by ID Handler 9147 06:24:32,320 --> 06:24:35,680 with a new request object but we're 9148 06:24:35,680 --> 06:24:37,160 going to set the find user index 9149 06:24:37,160 --> 06:24:40,878 property to a value that I know that 9150 06:24:40,878 --> 06:24:42,280 will not give us back a user in this 9151 06:24:42,280 --> 06:24:45,160 case we'll set it to 100 okay so when we 9152 06:24:45,160 --> 06:24:46,760 call this function we now want to write 9153 06:24:46,760 --> 06:24:50,320 some assertions so what I can do is I 9154 06:24:50,320 --> 06:24:53,240 can write an assertion to verify that 9155 06:24:53,240 --> 06:24:55,638 this send status function was called so 9156 06:24:55,638 --> 06:24:59,478 let's do expect mock response send 9157 06:24:59,478 --> 06:25:03,840 status to have been 9158 06:25:03,840 --> 06:25:07,920 called and then let's run the 9159 06:25:08,160 --> 06:25:10,478 test and you can see our second test 9160 06:25:10,478 --> 06:25:12,400 passes without any issues let's write a 9161 06:25:12,400 --> 06:25:15,080 few more assertions so let's do expect 9162 06:25:15,080 --> 06:25:19,040 mock response send status to have been 9163 06:25:19,040 --> 06:25:21,600 called with 9164 06:25:21,600 --> 06:25:24,040 404 let's run the test 9165 06:25:24,040 --> 06:25:26,840 again pass passes and I can just throw 9166 06:25:26,840 --> 06:25:28,718 in a value that I know is actually not 9167 06:25:28,718 --> 06:25:30,040 going to 9168 06:25:30,040 --> 06:25:33,160 pass you can see that it fails so that's 9169 06:25:33,160 --> 06:25:35,798 good because it shouldn't be sending a 9170 06:25:35,798 --> 06:25:37,558 400 it should be sending a 9171 06:25:37,558 --> 06:25:42,000 404 and I can also do 9172 06:25:42,000 --> 06:25:47,160 to let's do to have been called times 9173 06:25:47,160 --> 06:25:51,520 let's do one let's TR the test 9174 06:25:51,520 --> 06:25:54,760 again and let's also write an assertion 9175 06:25:54,760 --> 06:25:56,760 where we verify that mock response not 9176 06:25:56,760 --> 06:26:00,638 send was not called so expect mock 9177 06:26:00,638 --> 06:26:05,558 response send not to have been 9178 06:26:05,558 --> 06:26:08,760 called let's run the 9179 06:26:08,760 --> 06:26:12,878 test wonderful so all of our assertions 9180 06:26:12,878 --> 06:26:15,360 are good now one more thing that I do 9181 06:26:15,360 --> 06:26:18,080 want to do though is if you go inside 9182 06:26:18,080 --> 06:26:21,400 your just config file and if you look 9183 06:26:21,400 --> 06:26:23,680 for Clear mock you want to make sure 9184 06:26:23,680 --> 06:26:25,840 this is set true early ear when we were 9185 06:26:25,840 --> 06:26:28,240 configuring just we had pressed yes when 9186 06:26:28,240 --> 06:26:31,240 it asked us if we wanted to configure um 9187 06:26:31,240 --> 06:26:33,680 our test to clear all mock calls 9188 06:26:33,680 --> 06:26:35,798 instances and context and results before 9189 06:26:35,798 --> 06:26:38,000 every test because what happens is if 9190 06:26:38,000 --> 06:26:39,000 this is 9191 06:26:39,000 --> 06:26:41,558 false the next test will actually carry 9192 06:26:41,558 --> 06:26:44,120 over data from the previous test so if I 9193 06:26:44,120 --> 06:26:46,240 actually try to run my test 9194 06:26:46,240 --> 06:26:48,558 now you're going to see now our second 9195 06:26:48,558 --> 06:26:51,080 test actually fails and it's actually 9196 06:26:51,080 --> 06:26:53,400 failing because right over here on line 9197 06:26:53,400 --> 06:26:56,760 32 where I have this assertion expect 9198 06:26:56,760 --> 06:26:58,840 mock response. send not to have been 9199 06:26:58,840 --> 06:27:01,160 called you can see that it's failing 9200 06:27:01,160 --> 06:27:03,000 because it says receive number of calls 9201 06:27:03,000 --> 06:27:05,840 one so it in fact was called but if you 9202 06:27:05,840 --> 06:27:09,040 think about it your code itself says 9203 06:27:09,040 --> 06:27:10,638 otherwise because I know for a fact that 9204 06:27:10,638 --> 06:27:13,040 in my code response. send should not be 9205 06:27:13,040 --> 06:27:15,240 called because we are returning 9206 06:27:15,240 --> 06:27:17,200 response. send status so this code 9207 06:27:17,200 --> 06:27:20,638 doesn't get executed when F user is uh 9208 06:27:20,638 --> 06:27:24,120 not defined Okay the reason 9209 06:27:24,120 --> 06:27:26,478 why it's failing this test right over 9210 06:27:26,478 --> 06:27:28,280 here and it's treating it as if sen was 9211 06:27:28,280 --> 06:27:29,958 actually called is because in the 9212 06:27:29,958 --> 06:27:32,200 previous test the sen function was 9213 06:27:32,200 --> 06:27:35,478 called and it was called one time okay 9214 06:27:35,478 --> 06:27:37,520 so since it was called one time and we 9215 06:27:37,520 --> 06:27:40,240 didn't clear the mock it carried over to 9216 06:27:40,240 --> 06:27:43,160 the next test right over here inside 9217 06:27:43,160 --> 06:27:45,558 this second test over here so there's 9218 06:27:45,558 --> 06:27:47,200 several ways they can do this the first 9219 06:27:47,200 --> 06:27:49,200 thing that you can do is obviously what 9220 06:27:49,200 --> 06:27:52,040 we already had was having clear MOX set 9221 06:27:52,040 --> 06:27:55,558 to true instead of false the other thing 9222 06:27:55,558 --> 06:27:56,920 that you can do is you can use this 9223 06:27:56,920 --> 06:27:58,958 before each 9224 06:27:58,958 --> 06:28:01,160 hook and this takes in a callback 9225 06:28:01,160 --> 06:28:03,718 function and basically you can execute 9226 06:28:03,718 --> 06:28:06,600 some logic that happens before each one 9227 06:28:06,600 --> 06:28:08,718 of your tests so you can initialize a 9228 06:28:08,718 --> 06:28:10,760 variable you can connect to a database 9229 06:28:10,760 --> 06:28:12,478 whatever is that you want so you can 9230 06:28:12,478 --> 06:28:17,000 actually use this just. clear all MOX 9231 06:28:17,000 --> 06:28:20,600 function call and now if I run my 9232 06:28:20,600 --> 06:28:24,200 test it passes okay so this is manually 9233 06:28:24,200 --> 06:28:26,680 clearing all the mod before each test 9234 06:28:26,680 --> 06:28:28,240 sometimes you might not want to clear 9235 06:28:28,240 --> 06:28:29,798 all the mocks because you might want to 9236 06:28:29,798 --> 06:28:32,200 depend on previous function calls from 9237 06:28:32,200 --> 06:28:34,878 the previous test so you might want to 9238 06:28:34,878 --> 06:28:37,718 turn off clear Mock and set it to false 9239 06:28:37,718 --> 06:28:38,760 sometimes you just want to manually 9240 06:28:38,760 --> 06:28:40,200 clear the mock so you can use this 9241 06:28:40,200 --> 06:28:42,120 before each it's up to you but I just 9242 06:28:42,120 --> 06:28:43,840 wanted to mention that as well so I'm 9243 06:28:43,840 --> 06:28:45,680 just going to remove this before each 9244 06:28:45,680 --> 06:28:48,400 and I'm going to set clear MOX back to 9245 06:28:48,400 --> 06:28:51,478 true so hopefully these two tests that 9246 06:28:51,478 --> 06:28:55,040 we wrote makes sense now I have one more 9247 06:28:55,040 --> 06:28:56,798 example where we can write a unit test 9248 06:28:56,798 --> 06:28:58,520 for another one of our endpoints but 9249 06:28:58,520 --> 06:28:59,558 this one's this one's going to be a 9250 06:28:59,558 --> 06:29:00,878 little bit longer so I just wanted to 9251 06:29:00,878 --> 06:29:02,478 warn you all but I'll try my best to 9252 06:29:02,478 --> 06:29:04,840 keep it as concise as possible and I 9253 06:29:04,840 --> 06:29:06,478 want to do this one because there's a 9254 06:29:06,478 --> 06:29:07,798 lot of stuff going on inside this 9255 06:29:07,798 --> 06:29:09,798 function and I think it would be really 9256 06:29:09,798 --> 06:29:11,638 great for you all to learn how to test 9257 06:29:11,638 --> 06:29:13,320 different scenarios okay so what I'm 9258 06:29:13,320 --> 06:29:15,718 going to do is for my post endpoint for 9259 06:29:15,718 --> 06:29:18,478 API users this request Handler creates 9260 06:29:18,478 --> 06:29:20,440 the user and saves it to a database so 9261 06:29:20,440 --> 06:29:21,760 you're going to learn how to mock 9262 06:29:21,760 --> 06:29:23,760 functions that come from modules you're 9263 06:29:23,760 --> 06:29:26,558 going to learn how to to mock uh classes 9264 06:29:26,558 --> 06:29:28,920 es6 classes you're going to learn how to 9265 06:29:28,920 --> 06:29:30,600 mock the database call so that way we 9266 06:29:30,600 --> 06:29:32,600 don't actually hit the actual database 9267 06:29:32,600 --> 06:29:34,240 because when it comes to unit tests you 9268 06:29:34,240 --> 06:29:35,878 don't want to actually invoke your 9269 06:29:35,878 --> 06:29:38,440 database or call an external API so 9270 06:29:38,440 --> 06:29:39,718 you're going to learn how to do a lot of 9271 06:29:39,718 --> 06:29:41,840 stuff with this next test so we need to 9272 06:29:41,840 --> 06:29:44,760 make sure we get this function um out of 9273 06:29:44,760 --> 06:29:47,240 this uh out of this format we don't want 9274 06:29:47,240 --> 06:29:49,240 it to be an anonymous function so I'm 9275 06:29:49,240 --> 06:29:51,958 going to just copy that and I'm going to 9276 06:29:51,958 --> 06:29:55,000 go inside uh let's see not here but 9277 06:29:55,000 --> 06:29:57,840 inside my handlers file or the users. 9278 06:29:57,840 --> 06:30:00,360 MJS file inside handlers and I'm going 9279 06:30:00,360 --> 06:30:03,360 to go ahead and paste this here and I'll 9280 06:30:03,360 --> 06:30:06,320 name this function create user 9281 06:30:06,320 --> 06:30:08,638 Handler like 9282 06:30:08,638 --> 06:30:12,680 this and uh before I import all of the 9283 06:30:12,680 --> 06:30:14,878 necessary functions into this file let 9284 06:30:14,878 --> 06:30:17,320 me just take this and let me just paste 9285 06:30:17,320 --> 06:30:21,600 it here and import it up top over 9286 06:30:21,600 --> 06:30:25,120 here okay and now let's go ahead and go 9287 06:30:25,120 --> 06:30:27,878 back to our users. MJS file inside the 9288 06:30:27,878 --> 06:30:29,958 handlist folder and we need to make sure 9289 06:30:29,958 --> 06:30:32,080 we import all of the necessary functions 9290 06:30:32,080 --> 06:30:34,240 that we imported that were in the other 9291 06:30:34,240 --> 06:30:36,600 files so validation result comes from 9292 06:30:36,600 --> 06:30:39,520 Express validator that's a validation 9293 06:30:39,520 --> 06:30:42,520 library that can validate post requests 9294 06:30:42,520 --> 06:30:45,478 um request bodies query parameters all 9295 06:30:45,478 --> 06:30:47,680 that kind of stuff we need to also 9296 06:30:47,680 --> 06:30:50,200 import match data that's also a function 9297 06:30:50,200 --> 06:30:52,798 from Express validator hash password is 9298 06:30:52,798 --> 06:30:55,398 a function that we wrote Our elves let's 9299 06:30:55,398 --> 06:30:59,200 import that right over here uh user is 9300 06:30:59,200 --> 06:31:02,520 the model so this is what actually 9301 06:31:02,520 --> 06:31:04,440 allows us to interact with our database 9302 06:31:04,440 --> 06:31:06,320 but we're not actually going to call the 9303 06:31:06,320 --> 06:31:09,040 database we're going to mock out this as 9304 06:31:09,040 --> 06:31:13,320 well um and I think that should be it 9305 06:31:13,320 --> 06:31:17,958 okay so now let's go ahead and go into 9306 06:31:17,958 --> 06:31:22,360 our users. spc. JS file and then now I'm 9307 06:31:22,360 --> 06:31:24,280 going to create a new test Suite so 9308 06:31:24,280 --> 06:31:27,280 outside of this describe get users I'm 9309 06:31:27,280 --> 06:31:28,798 going to go ahead and create another 9310 06:31:28,798 --> 06:31:30,760 describe and I'm going to call this 9311 06:31:30,760 --> 06:31:34,120 create users so you can think of these 9312 06:31:34,120 --> 06:31:37,080 individual it functions these individual 9313 06:31:37,080 --> 06:31:40,760 tests as your scenarios 9314 06:31:40,760 --> 06:31:43,320 okay so inside the create users test 9315 06:31:43,320 --> 06:31:44,798 Suite I'm going to go ahead and create a 9316 06:31:44,798 --> 06:31:47,520 simple test and I'll just say it 9317 06:31:47,520 --> 06:31:50,558 should let's see let's go back into our 9318 06:31:50,558 --> 06:31:53,320 function okay so this is a pretty big 9319 06:31:53,320 --> 06:31:55,638 function it does a lot so I take this 9320 06:31:55,638 --> 06:31:57,840 step by step so that way you all don't 9321 06:31:57,840 --> 06:31:59,718 get confused first let me just remove 9322 06:31:59,718 --> 06:32:01,320 these console logs cuz we're not going 9323 06:32:01,320 --> 06:32:02,600 to need 9324 06:32:02,600 --> 06:32:05,840 them whoops so we want to take this step 9325 06:32:05,840 --> 06:32:07,520 by step because there are a lot of 9326 06:32:07,520 --> 06:32:10,280 things that are going on so let's hand 9327 06:32:10,280 --> 06:32:12,360 let's take a look at our code and 9328 06:32:12,360 --> 06:32:13,680 understand what's going on so when we 9329 06:32:13,680 --> 06:32:15,840 call this create user Handler um when 9330 06:32:15,840 --> 06:32:17,558 Express calls this function to handle 9331 06:32:17,558 --> 06:32:19,840 the uh create user endpoint it's going 9332 06:32:19,840 --> 06:32:22,160 to go ahead and call validation result 9333 06:32:22,160 --> 06:32:23,920 so when you're looking at the code the 9334 06:32:23,920 --> 06:32:25,440 important part is not to really worry 9335 06:32:25,440 --> 06:32:27,600 about what your what these functions 9336 06:32:27,600 --> 06:32:29,440 these external functions cuz we don't 9337 06:32:29,440 --> 06:32:30,840 know what these functions let's just 9338 06:32:30,840 --> 06:32:32,200 pretend we don't know what it does you 9339 06:32:32,200 --> 06:32:34,080 don't have to care about what this 9340 06:32:34,080 --> 06:32:35,240 function does you have to care about 9341 06:32:35,240 --> 06:32:36,600 what it returns because we're going to 9342 06:32:36,600 --> 06:32:38,718 mock the function anyways we're going to 9343 06:32:38,718 --> 06:32:40,680 mock the implementation we're going to 9344 06:32:40,680 --> 06:32:42,920 mock the return value of the function so 9345 06:32:42,920 --> 06:32:45,120 in the end it doesn't really matter okay 9346 06:32:45,120 --> 06:32:47,240 what I care about is what it returns so 9347 06:32:47,240 --> 06:32:49,040 result you might not know what it is you 9348 06:32:49,040 --> 06:32:50,840 might think it's an object it's a number 9349 06:32:50,840 --> 06:32:53,040 it's a string you might not know but you 9350 06:32:53,040 --> 06:32:54,878 can very easily tell if you just look at 9351 06:32:54,878 --> 06:32:57,558 the rest of the code and look at where 9352 06:32:57,558 --> 06:32:59,080 result is being referenced so you can 9353 06:32:59,080 --> 06:33:02,200 see right over here that result has this 9354 06:33:02,200 --> 06:33:03,878 is empty method that's being called on 9355 06:33:03,878 --> 06:33:06,080 it and I immediately know that this is 9356 06:33:06,080 --> 06:33:08,840 an object okay uh there's also this 9357 06:33:08,840 --> 06:33:12,120 array method on result as well so what I 9358 06:33:12,120 --> 06:33:14,240 can do is I can go ahead and actually 9359 06:33:14,240 --> 06:33:16,200 mock out validation result because this 9360 06:33:16,200 --> 06:33:18,878 is known as a dependency it's a function 9361 06:33:18,878 --> 06:33:21,080 that that can perform some kind of side 9362 06:33:21,080 --> 06:33:23,120 effect and we don't want to actually 9363 06:33:23,120 --> 06:33:25,080 have any side effects in our unit test 9364 06:33:25,080 --> 06:33:26,958 we want to make sure that we mock them 9365 06:33:26,958 --> 06:33:29,440 all out and we only want to test the 9366 06:33:29,440 --> 06:33:32,240 function itself we only care about the 9367 06:33:32,240 --> 06:33:34,600 possible paths our function that we're 9368 06:33:34,600 --> 06:33:37,200 trying to test and take so again you can 9369 06:33:37,200 --> 06:33:39,440 see right over here this is our first if 9370 06:33:39,440 --> 06:33:42,718 condition if result uh dot is empty if 9371 06:33:42,718 --> 06:33:44,840 it is uh let's say if this results to 9372 06:33:44,840 --> 06:33:47,160 false and then we negate that value so 9373 06:33:47,160 --> 06:33:49,680 if it is not empty then we're going to 9374 06:33:49,680 --> 06:33:52,958 return this response status of 400 and 9375 06:33:52,958 --> 06:33:55,000 then send back whatever this value is 9376 06:33:55,000 --> 06:33:57,000 again I know it's an array cuz we wrote 9377 06:33:57,000 --> 06:33:58,558 this out ourselves but we're just 9378 06:33:58,558 --> 06:34:00,680 pretending that we don't know okay so 9379 06:34:00,680 --> 06:34:02,440 this is the first thing that it could do 9380 06:34:02,440 --> 06:34:05,160 the second thing that could happen is 9381 06:34:05,160 --> 06:34:07,718 right down over here so you can see that 9382 06:34:07,718 --> 06:34:10,520 we have this new user. saave being 9383 06:34:10,520 --> 06:34:13,638 called and then it sends back a status 9384 06:34:13,638 --> 06:34:16,320 of 2011 and it sends the user okay so 9385 06:34:16,320 --> 06:34:18,558 this is the second scenario the third 9386 06:34:18,558 --> 06:34:21,798 scenario is that an error could be 9387 06:34:21,798 --> 06:34:24,600 thrown inside this Tri block so then the 9388 06:34:24,600 --> 06:34:26,240 save method could have been could have 9389 06:34:26,240 --> 06:34:28,360 thrown an error and then we catch it 9390 06:34:28,360 --> 06:34:29,638 down over here and then we send back a 9391 06:34:29,638 --> 06:34:32,920 status of 400 okay now there actually 9392 06:34:32,920 --> 06:34:34,840 should be another scenario where the 9393 06:34:34,840 --> 06:34:37,478 hashing password uh failed and we 9394 06:34:37,478 --> 06:34:39,160 actually should have added this inside 9395 06:34:39,160 --> 06:34:40,920 the try catch but we're just going to 9396 06:34:40,920 --> 06:34:43,718 leave it the way it is right now okay so 9397 06:34:43,718 --> 06:34:48,040 hopefully all of this makes sense so now 9398 06:34:48,040 --> 06:34:50,760 if I were to go back up top over here 9399 06:34:50,760 --> 06:34:52,718 again the validation result is one 9400 06:34:52,718 --> 06:34:55,200 dependency Okay so what we want to do 9401 06:34:55,200 --> 06:34:57,878 right now is we want to test our 9402 06:34:57,878 --> 06:34:59,840 function to handle this first scenario 9403 06:34:59,840 --> 06:35:02,160 first because instead of just having to 9404 06:35:02,160 --> 06:35:03,558 do everything all at once we're going to 9405 06:35:03,558 --> 06:35:05,958 take it step by step okay so what I'm 9406 06:35:05,958 --> 06:35:07,798 going to do is I'm going to write a test 9407 06:35:07,798 --> 06:35:09,638 that is going to mock out this 9408 06:35:09,638 --> 06:35:11,718 validation result function call we're 9409 06:35:11,718 --> 06:35:14,360 going to have it return uh literally 9410 06:35:14,360 --> 06:35:16,680 whatever object we want but that object 9411 06:35:16,680 --> 06:35:19,558 specifically needs to have this is empty 9412 06:35:19,558 --> 06:35:22,558 method as well as this result uh as well 9413 06:35:22,558 --> 06:35:24,638 as this array method on the result 9414 06:35:24,638 --> 06:35:26,240 object cuz they are both going to be 9415 06:35:26,240 --> 06:35:29,280 call okay so we need to make sure that 9416 06:35:29,280 --> 06:35:31,600 whatever is empty is going to return it 9417 06:35:31,600 --> 06:35:33,958 returns false for this case because I 9418 06:35:33,958 --> 06:35:36,160 can see that we negate the value of 9419 06:35:36,160 --> 06:35:39,160 result. is empty so for example if there 9420 06:35:39,160 --> 06:35:41,280 are no errors that's what this isempty 9421 06:35:41,280 --> 06:35:43,160 method means if there are no errors you 9422 06:35:43,160 --> 06:35:45,320 can see right of here it returns true if 9423 06:35:45,320 --> 06:35:47,958 there are no errors so if this is true 9424 06:35:47,958 --> 06:35:50,280 the negation of this would uh not 9425 06:35:50,280 --> 06:35:51,680 execute this line and it would just go 9426 06:35:51,680 --> 06:35:52,680 down over here which means that there 9427 06:35:52,680 --> 06:35:55,478 are no errors however is 9428 06:35:55,478 --> 06:35:57,080 however if is empty is 9429 06:35:57,080 --> 06:35:59,760 false then result that is empty would 9430 06:35:59,760 --> 06:36:01,440 return false which means that we negate 9431 06:36:01,440 --> 06:36:03,760 that which means it would return this 9432 06:36:03,760 --> 06:36:05,958 part over here so for our first scenario 9433 06:36:05,958 --> 06:36:08,718 we want is empty to return false just so 9434 06:36:08,718 --> 06:36:10,958 that we can Target this line over here 9435 06:36:10,958 --> 06:36:13,280 so what I'm going to do inside my users. 9436 06:36:13,280 --> 06:36:16,958 spc. Js file is this so I actually 9437 06:36:16,958 --> 06:36:19,718 already have some mock data defined up 9438 06:36:19,718 --> 06:36:22,240 there um just to keep things simple I'm 9439 06:36:22,240 --> 06:36:23,798 actually just going to Define it inside 9440 06:36:23,798 --> 06:36:25,000 test Suite 9441 06:36:25,000 --> 06:36:27,240 so const mock 9442 06:36:27,240 --> 06:36:31,600 request and then const mock response and 9443 06:36:31,600 --> 06:36:34,478 then it'll be within the closure of this 9444 06:36:34,478 --> 06:36:35,718 callback function so we don't have to 9445 06:36:35,718 --> 06:36:38,080 really worry about the ones defined up 9446 06:36:38,080 --> 06:36:41,600 there so what I'll do is for the test 9447 06:36:41,600 --> 06:36:44,398 scenario I will say it should return a 9448 06:36:44,398 --> 06:36:46,798 status of 9449 06:36:46,798 --> 06:36:51,080 400 when there are 9450 06:36:51,080 --> 06:36:53,280 errors and we'll pass on our callback 9451 06:36:53,280 --> 06:36:55,040 function 9452 06:36:55,040 --> 06:36:57,840 okay so now we need to go ahead and set 9453 06:36:57,840 --> 06:37:00,958 up our mocks our our mock request and 9454 06:37:00,958 --> 06:37:03,920 mock response to actually get this to 9455 06:37:03,920 --> 06:37:05,600 work so again we have to look at our 9456 06:37:05,600 --> 06:37:08,200 request object and we want to see Within 9457 06:37:08,200 --> 06:37:10,840 These two lines where is our request 9458 06:37:10,840 --> 06:37:12,798 object being referenced you can see that 9459 06:37:12,798 --> 06:37:14,638 it's being passed as an argument to 9460 06:37:14,638 --> 06:37:16,718 validation result however it's not going 9461 06:37:16,718 --> 06:37:18,000 to really matter because we're going to 9462 06:37:18,000 --> 06:37:20,798 mock validation result so we actually 9463 06:37:20,798 --> 06:37:22,000 aren't really going to need to do 9464 06:37:22,000 --> 06:37:23,680 anything with the mock request for this 9465 06:37:23,680 --> 06:37:26,680 specific scenario okay so let's look at 9466 06:37:26,680 --> 06:37:29,200 response I can see that response is 9467 06:37:29,200 --> 06:37:31,040 referencing this status method right 9468 06:37:31,040 --> 06:37:33,398 over here so right over here we're going 9469 06:37:33,398 --> 06:37:37,080 to go ahead and do status just. FN and 9470 06:37:37,080 --> 06:37:38,718 you know what I actually could just 9471 06:37:38,718 --> 06:37:41,840 reuse mock response up top over here 9472 06:37:41,840 --> 06:37:43,478 because we're going to be using some of 9473 06:37:43,478 --> 06:37:45,920 these similar methods 9474 06:37:45,920 --> 06:37:48,558 anyways so you know what what I'll do is 9475 06:37:48,558 --> 06:37:50,718 I'm just going to remove this and I'll 9476 06:37:50,718 --> 06:37:53,280 just reuse the mock response up here and 9477 06:37:53,280 --> 06:37:54,558 it shouldn't matter because we are 9478 06:37:54,558 --> 06:37:56,878 clearing all of the mocks uh anyways 9479 06:37:56,878 --> 06:37:59,040 before every test so we won't have any 9480 06:37:59,040 --> 06:38:02,040 leftover data from the previous test 9481 06:38:02,040 --> 06:38:04,840 okay so let's go ahead and implement the 9482 06:38:04,840 --> 06:38:05,958 status 9483 06:38:05,958 --> 06:38:07,958 method and this is also going to be a 9484 06:38:07,958 --> 06:38:10,638 mock function so we'll use just. FN okay 9485 06:38:10,638 --> 06:38:11,920 now one more thing that I do want to 9486 06:38:11,920 --> 06:38:14,200 point out is that after we call the 9487 06:38:14,200 --> 06:38:16,478 status method we actually are calling 9488 06:38:16,478 --> 06:38:18,600 the send method as well because when you 9489 06:38:18,600 --> 06:38:20,360 call this status method it actually 9490 06:38:20,360 --> 06:38:21,958 Returns the instance of the response 9491 06:38:21,958 --> 06:38:23,878 itself and you can actually call the 9492 06:38:23,878 --> 06:38:25,520 stat status method on it as many times 9493 06:38:25,520 --> 06:38:27,478 you want because it just Returns the 9494 06:38:27,478 --> 06:38:30,680 response instance itself so in order for 9495 06:38:30,680 --> 06:38:33,558 us to actually detect calls on the send 9496 06:38:33,558 --> 06:38:37,080 function inside uh status over 9497 06:38:37,080 --> 06:38:40,638 here inside just. FN for status we 9498 06:38:40,638 --> 06:38:42,638 actually want to pass a callback 9499 06:38:42,638 --> 06:38:45,240 function and we want to actually return 9500 06:38:45,240 --> 06:38:47,080 mock response so basically returning 9501 06:38:47,080 --> 06:38:50,040 itself so that way by returning itself 9502 06:38:50,040 --> 06:38:52,200 mock response would then have access to 9503 06:38:52,200 --> 06:38:54,360 all these other methods as well and if 9504 06:38:54,360 --> 06:38:55,680 we don't do this we wouldn't be able to 9505 06:38:55,680 --> 06:38:59,000 actually test to see if send was called 9506 06:38:59,000 --> 06:39:00,600 after status and I'll show you that when 9507 06:39:00,600 --> 06:39:03,240 we actually write the test okay so we're 9508 06:39:03,240 --> 06:39:05,520 not done yet we're done with setting up 9509 06:39:05,520 --> 06:39:07,920 our mock request and our mock response 9510 06:39:07,920 --> 06:39:11,120 we need to now mock this validation 9511 06:39:11,120 --> 06:39:12,718 result function and you're probably 9512 06:39:12,718 --> 06:39:14,398 wondering well how do I do that this is 9513 06:39:14,398 --> 06:39:16,760 a function that comes from a thirdparty 9514 06:39:16,760 --> 06:39:19,798 module Express validator well I'll show 9515 06:39:19,798 --> 06:39:23,558 you so you can mock modules and what 9516 06:39:23,558 --> 06:39:25,920 that basically means is you can override 9517 06:39:25,920 --> 06:39:28,280 the implementation and the functionality 9518 06:39:28,280 --> 06:39:30,520 that that module that module's API 9519 06:39:30,520 --> 06:39:33,478 provides so for example inside the 9520 06:39:33,478 --> 06:39:35,760 express validator module we have a bunch 9521 06:39:35,760 --> 06:39:37,558 of functions we have a bunch of 9522 06:39:37,558 --> 06:39:39,240 different things that we can import that 9523 06:39:39,240 --> 06:39:41,558 we can use in our application however we 9524 06:39:41,558 --> 06:39:44,840 want now for our case we only are using 9525 06:39:44,840 --> 06:39:47,878 validation result currently so we want 9526 06:39:47,878 --> 06:39:50,558 to mock validation results specifically 9527 06:39:50,558 --> 06:39:53,320 so all the way up top over here what I 9528 06:39:53,320 --> 06:39:56,160 can do is I can reference 9529 06:39:56,160 --> 06:39:59,280 jmck and then I want to specify the name 9530 06:39:59,280 --> 06:40:01,638 of the module so I'm going to go ahead 9531 06:40:01,638 --> 06:40:04,920 and say Express validator like this so 9532 06:40:04,920 --> 06:40:07,398 This Will Mock that mpm module you can 9533 06:40:07,398 --> 06:40:09,520 also mock your own modules that you 9534 06:40:09,520 --> 06:40:11,958 create as well so for example uh later 9535 06:40:11,958 --> 06:40:13,958 on when we need to mock uh the hash 9536 06:40:13,958 --> 06:40:16,440 password function we're also going to 9537 06:40:16,440 --> 06:40:19,718 mock this helpers module as well okay 9538 06:40:19,718 --> 06:40:22,360 and that's our local module so as a 9539 06:40:22,360 --> 06:40:24,718 second argument after we pass 9540 06:40:24,718 --> 06:40:27,040 Express validator for the first one the 9541 06:40:27,040 --> 06:40:28,240 second argument whoops the second 9542 06:40:28,240 --> 06:40:30,080 argument is going to be a 9543 06:40:30,080 --> 06:40:33,840 factory so this is where you're going to 9544 06:40:33,840 --> 06:40:35,000 return an 9545 06:40:35,000 --> 06:40:37,840 object because if you think about it 9546 06:40:37,840 --> 06:40:39,718 your modules are kind of like objects 9547 06:40:39,718 --> 06:40:42,840 and those objects have um the named 9548 06:40:42,840 --> 06:40:46,160 exports okay if it assuming it does so 9549 06:40:46,160 --> 06:40:47,638 what we want to do is we want to return 9550 06:40:47,638 --> 06:40:49,840 this object like this so notice how I 9551 06:40:49,840 --> 06:40:51,840 have the parenthesis wrapped around the 9552 06:40:51,840 --> 06:40:54,478 curly brace so that just allows me to do 9553 06:40:54,478 --> 06:40:56,040 it shorthand instead of having to do 9554 06:40:56,040 --> 06:40:59,440 something like this okay so what I'll do 9555 06:40:59,440 --> 06:41:03,718 is inside the object I want to mock the 9556 06:41:03,718 --> 06:41:07,760 validation result function so I'm going 9557 06:41:07,760 --> 06:41:11,600 to type validation result like that and 9558 06:41:11,600 --> 06:41:15,040 I'm going to do just. FN like that so 9559 06:41:15,040 --> 06:41:17,200 now I have successfully mocked the 9560 06:41:17,200 --> 06:41:19,080 validation result function for the 9561 06:41:19,080 --> 06:41:21,958 express validator module and now this 9562 06:41:21,958 --> 06:41:23,558 mock function we want it to actually 9563 06:41:23,558 --> 06:41:24,878 return 9564 06:41:24,878 --> 06:41:27,760 something so that way we can 9565 06:41:27,760 --> 06:41:30,638 actually uh have this result value be 9566 06:41:30,638 --> 06:41:32,798 something that is defined with the 9567 06:41:32,798 --> 06:41:36,680 correct is empty and array methods so we 9568 06:41:36,680 --> 06:41:40,600 can do that inside our Justa FN function 9569 06:41:40,600 --> 06:41:43,000 we can pass in a call back that also 9570 06:41:43,000 --> 06:41:44,718 returns an object as 9571 06:41:44,718 --> 06:41:47,878 well and I can go ahead and Define the 9572 06:41:47,878 --> 06:41:50,798 is empty method so is empty is going to 9573 06:41:50,798 --> 06:41:54,200 be a mock function as well but this mock 9574 06:41:54,200 --> 06:41:57,040 function remember we're going to have it 9575 06:41:57,040 --> 06:41:59,478 return uh 9576 06:41:59,478 --> 06:42:02,958 false okay so passing that call back 9577 06:42:02,958 --> 06:42:05,280 return false like this and then we also 9578 06:42:05,280 --> 06:42:08,840 want to implement this array method on 9579 06:42:08,840 --> 06:42:11,558 the return value of validation 9580 06:42:11,558 --> 06:42:14,920 result so this is a method and uh it 9581 06:42:14,920 --> 06:42:18,280 does return an array so we will have it 9582 06:42:18,280 --> 06:42:19,280 return an 9583 06:42:19,280 --> 06:42:22,120 array like this and then what you can do 9584 06:42:22,120 --> 06:42:26,000 is you can add some random object with 9585 06:42:26,000 --> 06:42:27,920 random Fields so you can just say 9586 06:42:27,920 --> 06:42:30,600 message invalid username just like an 9587 06:42:30,600 --> 06:42:31,798 example you can literally put whatever 9588 06:42:31,798 --> 06:42:33,520 you want like invalid field whatever it 9589 06:42:33,520 --> 06:42:36,520 is okay because we can always actually 9590 06:42:36,520 --> 06:42:39,840 modify uh we can always override the 9591 06:42:39,840 --> 06:42:41,558 return value of validation result and 9592 06:42:41,558 --> 06:42:43,478 I'll show you how to do that as 9593 06:42:43,478 --> 06:42:47,798 well okay so I think we're good to go 9594 06:42:47,798 --> 06:42:50,000 let's actually go ahead and test 9595 06:42:50,000 --> 06:42:52,798 everything out okay so what I'm going to 9596 06:42:52,798 --> 06:42:55,280 do is I'm going to go ahead and call 9597 06:42:55,280 --> 06:42:57,558 create user Handler so let's go ahead 9598 06:42:57,558 --> 06:42:59,798 and import that up top over here oh 9599 06:42:59,798 --> 06:43:01,520 whoops I can just import that from right 9600 06:43:01,520 --> 06:43:04,360 over here wonderful cuz we're in it's 9601 06:43:04,360 --> 06:43:07,520 from it's coming from the same file okay 9602 06:43:07,520 --> 06:43:09,080 so let's go ahead and call create user 9603 06:43:09,080 --> 06:43:11,760 Handler let's pass in the mock request 9604 06:43:11,760 --> 06:43:13,958 and notice how we're referencing this 9605 06:43:13,958 --> 06:43:15,920 local mock request cuz this is the 9606 06:43:15,920 --> 06:43:17,320 closure we're inside this closure over 9607 06:43:17,320 --> 06:43:19,360 here and then we're going to reference 9608 06:43:19,360 --> 06:43:21,040 the mock response which is all the way 9609 06:43:21,040 --> 06:43:23,200 up there so we are reusing that same 9610 06:43:23,200 --> 06:43:25,718 mock resp responds object from earlier 9611 06:43:25,718 --> 06:43:28,160 okay and remember that this function is 9612 06:43:28,160 --> 06:43:31,398 asynchronous so we should also await the 9613 06:43:31,398 --> 06:43:33,520 call and we need to make sure we add the 9614 06:43:33,520 --> 06:43:35,280 Asing keyword in front of this callback 9615 06:43:35,280 --> 06:43:39,558 function for the it function okay all 9616 06:43:39,558 --> 06:43:41,440 right so let's just make sure everything 9617 06:43:41,440 --> 06:43:45,000 is okay so if I run npm run 9618 06:43:45,000 --> 06:43:47,520 test the test does pass but we aren't 9619 06:43:47,520 --> 06:43:50,040 writing any assertion so that's okay and 9620 06:43:50,040 --> 06:43:51,760 you'll notice how now if I actually try 9621 06:43:51,760 --> 06:43:54,360 to kind of like remove all 9622 06:43:54,360 --> 06:43:57,520 this all the mocks that we just did 9623 06:43:57,520 --> 06:43:59,240 you'll start to notice that a lot of 9624 06:43:59,240 --> 06:44:01,440 stuff uh starts to error out and you can 9625 06:44:01,440 --> 06:44:03,558 see that um it's actually trying to go 9626 06:44:03,558 --> 06:44:06,478 to line seems like like line8 over here 9627 06:44:06,478 --> 06:44:09,958 inside the helpers MJS so it's actually 9628 06:44:09,958 --> 06:44:11,440 without mocking anything it actually 9629 06:44:11,440 --> 06:44:15,680 tries to call the actual uh Express 9630 06:44:15,680 --> 06:44:18,398 validator uh Library so it's actually 9631 06:44:18,398 --> 06:44:21,040 calling the functions okay and then it's 9632 06:44:21,040 --> 06:44:22,600 going to try to execute everything the 9633 06:44:22,600 --> 06:44:24,760 way it is but you can see over here it 9634 06:44:24,760 --> 06:44:27,478 tries to throw this error right over 9635 06:44:27,478 --> 06:44:29,120 here because we don't have a value for 9636 06:44:29,120 --> 06:44:30,840 password but we'll get to that because 9637 06:44:30,840 --> 06:44:33,120 right now our request body has uh 9638 06:44:33,120 --> 06:44:35,878 nothing okay and we also aren't mocking 9639 06:44:35,878 --> 06:44:38,080 match data yet so don't worry step by 9640 06:44:38,080 --> 06:44:39,160 step just wanted to show you what would 9641 06:44:39,160 --> 06:44:42,478 happen if we removed the mock stuff okay 9642 06:44:42,478 --> 06:44:45,478 so let's add that back in and you can 9643 06:44:45,478 --> 06:44:47,680 see that everything will work the same 9644 06:44:47,680 --> 06:44:49,638 way all right so now let's actually 9645 06:44:49,638 --> 06:44:51,638 write an assertion so the first thing 9646 06:44:51,638 --> 06:44:54,718 that I want to do is this so when it 9647 06:44:54,718 --> 06:44:56,760 comes to testing there are different 9648 06:44:56,760 --> 06:44:59,160 ways that you can approach testing there 9649 06:44:59,160 --> 06:45:01,240 is there's implementation testing and 9650 06:45:01,240 --> 06:45:03,478 that's where you're actually testing the 9651 06:45:03,478 --> 06:45:04,958 actual implementation so you're trying 9652 06:45:04,958 --> 06:45:07,080 to verify that certain functions are 9653 06:45:07,080 --> 06:45:09,040 called certain functions are called with 9654 06:45:09,040 --> 06:45:10,840 certain parameters that's known as 9655 06:45:10,840 --> 06:45:12,718 implementation testing there's also 9656 06:45:12,718 --> 06:45:14,718 behavioral testing where you just give 9657 06:45:14,718 --> 06:45:16,600 it some input you don't care what it 9658 06:45:16,600 --> 06:45:18,600 does as long as it gives you an output 9659 06:45:18,600 --> 06:45:20,360 so there's different ways on how you can 9660 06:45:20,360 --> 06:45:22,680 test this with backend you can't really 9661 06:45:22,680 --> 06:45:24,280 go wrong with either either one usually 9662 06:45:24,280 --> 06:45:26,520 on the front end like if you're testing 9663 06:45:26,520 --> 06:45:29,120 apps and react you would want to use 9664 06:45:29,120 --> 06:45:31,080 behavioral driven testing because you 9665 06:45:31,080 --> 06:45:33,440 want to test based on output and you 9666 06:45:33,440 --> 06:45:35,440 don't care about what actually happens 9667 06:45:35,440 --> 06:45:37,398 underneath the hood with all of the 9668 06:45:37,398 --> 06:45:39,600 state and all of the uh you know form 9669 06:45:39,600 --> 06:45:41,120 fields and whatever is being called to 9670 06:45:41,120 --> 06:45:43,320 the API stuff like that so I just wanted 9671 06:45:43,320 --> 06:45:45,958 to point that out okay but since we're 9672 06:45:45,958 --> 06:45:47,120 just getting started I'm just going to 9673 06:45:47,120 --> 06:45:49,280 show you very basic how to write 9674 06:45:49,280 --> 06:45:51,920 assertions so what I want to do is I 9675 06:45:51,920 --> 06:45:54,558 want to verify that validation result 9676 06:45:54,558 --> 06:45:57,920 was called with the request object okay 9677 06:45:57,920 --> 06:46:00,440 so the question is how do I actually 9678 06:46:00,440 --> 06:46:02,320 write the assertion for validation 9679 06:46:02,320 --> 06:46:04,680 result because in my whole test file I 9680 06:46:04,680 --> 06:46:07,080 don't have any access to validation 9681 06:46:07,080 --> 06:46:09,798 result well I'll show you so the first 9682 06:46:09,798 --> 06:46:11,878 thing that we can do and I'll import 9683 06:46:11,878 --> 06:46:13,878 this over here on line one is I'm going 9684 06:46:13,878 --> 06:46:17,398 to import the entire Express validated 9685 06:46:17,398 --> 06:46:19,320 library but I'm going to import it as 9686 06:46:19,320 --> 06:46:20,600 like a default import like this so 9687 06:46:20,600 --> 06:46:24,240 import validator from Express validator 9688 06:46:24,240 --> 06:46:27,600 like this and then now down over here in 9689 06:46:27,600 --> 06:46:30,360 our test after I call the create user 9690 06:46:30,360 --> 06:46:32,160 Handler function I can write an 9691 06:46:32,160 --> 06:46:34,600 assertion and I can reference validator 9692 06:46:34,600 --> 06:46:38,760 do validation result to have been called 9693 06:46:38,760 --> 06:46:40,440 times 9694 06:46:40,440 --> 06:46:44,280 one and let's run our 9695 06:46:44,280 --> 06:46:47,440 test and you can see that it passes and 9696 06:46:47,440 --> 06:46:50,080 if I were to set this to zero to see if 9697 06:46:50,080 --> 06:46:52,638 it actually fails and it should fail you 9698 06:46:52,638 --> 06:46:54,240 can see that it was actually called once 9699 06:46:54,240 --> 06:46:55,558 so that verifies that it was actually 9700 06:46:55,558 --> 06:46:58,600 called and if I were to actually remove 9701 06:46:58,600 --> 06:47:00,520 uh if I were to actually just kind of 9702 06:47:00,520 --> 06:47:05,160 like remove this whole thing right over 9703 06:47:05,840 --> 06:47:08,680 here you can see that our test begins to 9704 06:47:08,680 --> 06:47:10,398 fail and it's not recognizing validation 9705 06:47:10,398 --> 06:47:12,478 result as a function if I remove this 9706 06:47:12,478 --> 06:47:15,120 whole just. Mock and if I try to run the 9707 06:47:15,120 --> 06:47:16,160 test 9708 06:47:16,160 --> 06:47:18,080 again you're going to see that other 9709 06:47:18,080 --> 06:47:20,520 stuff starts to fail as well okay so 9710 06:47:20,520 --> 06:47:21,878 hopefully this helps you understand the 9711 06:47:21,878 --> 06:47:24,200 importance of mocking out your modules 9712 06:47:24,200 --> 06:47:25,878 and hopefully this helps understand how 9713 06:47:25,878 --> 06:47:29,520 to how to mock out uh these functions 9714 06:47:29,520 --> 06:47:31,360 that come from the Express validator 9715 06:47:31,360 --> 06:47:33,958 package or just any package in general 9716 06:47:33,958 --> 06:47:36,840 okay let's go ahead and continue so I'll 9717 06:47:36,840 --> 06:47:39,798 do validator dot validation results so 9718 06:47:39,798 --> 06:47:42,398 I'm going to write an assertion to say 9719 06:47:42,398 --> 06:47:44,520 that to have been 9720 06:47:44,520 --> 06:47:48,160 called with mock request because if you 9721 06:47:48,160 --> 06:47:50,080 look right over here we are passing 9722 06:47:50,080 --> 06:47:52,760 whatever request object this is to 9723 06:47:52,760 --> 06:47:54,958 validation result so we'll write the 9724 06:47:54,958 --> 06:47:57,360 assertion 9725 06:47:57,360 --> 06:48:00,000 there it passes and let me just change 9726 06:48:00,000 --> 06:48:02,558 this to two have been called instead of 9727 06:48:02,558 --> 06:48:04,760 two I've been called times 9728 06:48:04,760 --> 06:48:07,240 one okay that's a lot more better 9729 06:48:07,240 --> 06:48:08,280 because this basically implies it's 9730 06:48:08,280 --> 06:48:10,200 going to be called once or two have been 9731 06:48:10,200 --> 06:48:11,840 called at least 9732 06:48:11,840 --> 06:48:15,958 once okay so now uh we want to also 9733 06:48:15,958 --> 06:48:19,200 write an assertion that response. status 9734 06:48:19,200 --> 06:48:21,398 was called with a status code of 9735 06:48:21,398 --> 06:48:24,760 400 so let's do that so 9736 06:48:24,760 --> 06:48:28,638 expect response dot I think it was 9737 06:48:28,638 --> 06:48:32,320 status and it's mock 9738 06:48:34,080 --> 06:48:38,320 response to have been called 9739 06:48:38,320 --> 06:48:41,478 with a 9740 06:48:41,878 --> 06:48:47,160 400 mock response okay let's run our 9741 06:48:48,638 --> 06:48:52,320 test okay it passes good and let's go 9742 06:48:52,320 --> 06:48:54,398 ahead and write a search to verify that 9743 06:48:54,398 --> 06:48:57,440 the do send method was was called so we 9744 06:48:57,440 --> 06:48:59,558 can do expect mock 9745 06:48:59,558 --> 06:49:02,160 response and this will be mock response. 9746 06:49:02,160 --> 06:49:05,280 send not mock response. status. send 9747 06:49:05,280 --> 06:49:06,600 because remember that when we call the 9748 06:49:06,600 --> 06:49:08,120 send method we're still calling it on 9749 06:49:08,120 --> 06:49:11,320 the instance of the response method so 9750 06:49:11,320 --> 06:49:14,320 we would have to reference mock response 9751 06:49:14,320 --> 06:49:17,840 so to have been called with and then we 9752 06:49:17,840 --> 06:49:19,840 want to verify that it was called with 9753 06:49:19,840 --> 06:49:20,718 this 9754 06:49:20,718 --> 06:49:23,600 array and that's whatever we had mocked 9755 06:49:23,600 --> 06:49:26,600 over here so I'm just going to take that 9756 06:49:26,600 --> 06:49:29,558 copy and paste it down over 9757 06:49:29,558 --> 06:49:32,558 here let's run the test 9758 06:49:32,558 --> 06:49:35,320 again and it passes and again I can go 9759 06:49:35,320 --> 06:49:37,398 ahead and just change everything up and 9760 06:49:37,398 --> 06:49:39,440 make sure that our test is passing 9761 06:49:39,440 --> 06:49:41,280 correctly and you can see it throws an 9762 06:49:41,280 --> 06:49:43,520 error when I pass in the invalid 9763 06:49:43,520 --> 06:49:45,760 expected argument okay so that's pretty 9764 06:49:45,760 --> 06:49:48,040 good let's go ahead and move on now 9765 06:49:48,040 --> 06:49:49,718 because we've covered this whole part 9766 06:49:49,718 --> 06:49:52,040 over here so now we have to cover the 9767 06:49:52,040 --> 06:49:54,760 next few lines over here everything up 9768 06:49:54,760 --> 06:49:58,440 until down over here so we are calling 9769 06:49:58,440 --> 06:50:00,200 this match data function but we aren't 9770 06:50:00,200 --> 06:50:01,718 mocking it okay so it's going to 9771 06:50:01,718 --> 06:50:03,958 actually call the actual match data 9772 06:50:03,958 --> 06:50:05,680 function which comes from Express 9773 06:50:05,680 --> 06:50:07,638 validator so we want to mock it because 9774 06:50:07,638 --> 06:50:09,200 what I want to do is I want to mock this 9775 06:50:09,200 --> 06:50:12,360 function to return uh some object which 9776 06:50:12,360 --> 06:50:14,240 is going to be the actual user data so 9777 06:50:14,240 --> 06:50:16,520 again to give you context uh what this 9778 06:50:16,520 --> 06:50:18,160 function does is it takes the request 9779 06:50:18,160 --> 06:50:21,478 body and it gets you it gives you the 9780 06:50:21,478 --> 06:50:23,600 validation result okay so if there are 9781 06:50:23,600 --> 06:50:25,840 no errors then what happens over here 9782 06:50:25,840 --> 06:50:28,000 with match data is match data will 9783 06:50:28,000 --> 06:50:30,360 extract all of the valid fields in the 9784 06:50:30,360 --> 06:50:33,280 request body and store inside this data 9785 06:50:33,280 --> 06:50:35,718 object so all we really need to do is 9786 06:50:35,718 --> 06:50:38,638 just mock match data to 9787 06:50:38,638 --> 06:50:43,600 return a user a valid user object so 9788 06:50:43,600 --> 06:50:47,920 let's do this okay so going back up over 9789 06:50:47,920 --> 06:50:53,520 here I'm going to do match data just. FN 9790 06:50:53,520 --> 06:50:58,040 and we're going to have it return an 9791 06:50:58,040 --> 06:51:00,200 object and keep in mind that whatever 9792 06:51:00,200 --> 06:51:03,398 the return value of matched data is is 9793 06:51:03,398 --> 06:51:04,798 actually just going to be an object so 9794 06:51:04,798 --> 06:51:08,240 there's no methods on it that we would 9795 06:51:08,240 --> 06:51:09,360 need to worry 9796 06:51:09,360 --> 06:51:11,878 about because we're not referencing 9797 06:51:11,878 --> 06:51:16,320 anything except for just the password 9798 06:51:16,320 --> 06:51:20,360 field so let's just do uh let's see 9799 06:51:20,360 --> 06:51:24,958 username let's do test 9800 06:51:25,240 --> 06:51:27,160 password uh 9801 06:51:27,160 --> 06:51:29,080 password and then I think the other 9802 06:51:29,080 --> 06:51:32,638 field was uh I think it was display name 9803 06:51:32,638 --> 06:51:35,160 if I remember correctly let me check if 9804 06:51:35,160 --> 06:51:36,878 I go inside my validation schema so we 9805 06:51:36,878 --> 06:51:39,638 have username display name and password 9806 06:51:39,638 --> 06:51:41,398 yep so 9807 06:51:41,398 --> 06:51:43,398 display 9808 06:51:43,398 --> 06:51:47,360 name just do test name now some of you 9809 06:51:47,360 --> 06:51:49,280 might be asking well what's the point of 9810 06:51:49,280 --> 06:51:51,200 doing all this mocking because doesn't 9811 06:51:51,200 --> 06:51:52,680 that kind of defeat the whole purpose of 9812 06:51:52,680 --> 06:51:54,798 test testing well no not for unit 9813 06:51:54,798 --> 06:51:56,958 testing because remember our goal is not 9814 06:51:56,958 --> 06:51:59,958 to actually test to make sure that you 9815 06:51:59,958 --> 06:52:02,160 know Express validat Works our goal is 9816 06:52:02,160 --> 06:52:04,440 to test create user handle Works our own 9817 06:52:04,440 --> 06:52:07,600 function this is a unit test okay when 9818 06:52:07,600 --> 06:52:09,320 you want to actually test the entire 9819 06:52:09,320 --> 06:52:11,200 application that is where you want to 9820 06:52:11,200 --> 06:52:13,398 actually set up an integration test or 9821 06:52:13,398 --> 06:52:15,120 an end to end test which you will learn 9822 06:52:15,120 --> 06:52:17,478 about it later okay so hopefully that 9823 06:52:17,478 --> 06:52:19,840 kind of clarifies the confusion between 9824 06:52:19,840 --> 06:52:20,478 uh 9825 06:52:20,478 --> 06:52:22,558 mocking and actually testing the entire 9826 06:52:22,558 --> 06:52:25,040 thing with without mocking it okay all 9827 06:52:25,040 --> 06:52:26,718 right so now that we've mocked match 9828 06:52:26,718 --> 06:52:29,558 data this function to return a mocked 9829 06:52:29,558 --> 06:52:33,200 result so we can safely uh run the rest 9830 06:52:33,200 --> 06:52:35,520 of our code and then we want to make 9831 06:52:35,520 --> 06:52:37,240 sure that this match data function is 9832 06:52:37,240 --> 06:52:39,160 going to be called but we also want to 9833 06:52:39,160 --> 06:52:40,920 make sure that we don't hit this if 9834 06:52:40,920 --> 06:52:43,320 condition and make this true so we need 9835 06:52:43,320 --> 06:52:45,600 to actually make is empty return true 9836 06:52:45,600 --> 06:52:47,398 this time which means that there are no 9837 06:52:47,398 --> 06:52:50,558 errors but the thing is though I mocked 9838 06:52:50,558 --> 06:52:53,440 it up top over here to return false so 9839 06:52:53,440 --> 06:52:56,120 what if I changed it to true but then 9840 06:52:56,120 --> 06:52:59,360 that would break my first test over here 9841 06:52:59,360 --> 06:53:01,760 so we can't do that so I'll show you 9842 06:53:01,760 --> 06:53:05,398 what we can do to actually change the 9843 06:53:05,398 --> 06:53:09,718 return value for our specific tests okay 9844 06:53:09,718 --> 06:53:11,840 because we want this is empty function 9845 06:53:11,840 --> 06:53:15,478 to return uh true for whenever we call 9846 06:53:15,478 --> 06:53:19,000 it validation result so that way this if 9847 06:53:19,000 --> 06:53:21,638 condition results to false and then 9848 06:53:21,638 --> 06:53:23,360 it'll go down over here and call match 9849 06:53:23,360 --> 06:53:26,280 data okay so let me just make sure that 9850 06:53:26,280 --> 06:53:29,080 we have our match data function mocked 9851 06:53:29,080 --> 06:53:31,718 properly okay this is good let's go down 9852 06:53:31,718 --> 06:53:34,638 over here and let's rate our next test 9853 06:53:34,638 --> 06:53:37,120 and I'm going to go ahead and call this 9854 06:53:37,120 --> 06:53:40,600 test uh let's see so I guess the overall 9855 06:53:40,600 --> 06:53:43,398 thing that we are trying to test for is 9856 06:53:43,398 --> 06:53:46,360 making sure that the response. status is 9857 06:53:46,360 --> 06:53:48,240 sending back a 2011 and it's sending 9858 06:53:48,240 --> 06:53:49,958 back the user that was created so 9859 06:53:49,958 --> 06:53:51,440 essentially we're we're testing for 9860 06:53:51,440 --> 06:53:54,000 success for creating user so I'll just 9861 06:53:54,000 --> 06:53:55,200 say it 9862 06:53:55,200 --> 06:54:03,878 should return status of to1 and the user 9863 06:54:03,878 --> 06:54:05,798 created okay and let me actually go up 9864 06:54:05,798 --> 06:54:07,878 here and let me fix this should return 9865 06:54:07,878 --> 06:54:09,040 status of 9866 06:54:09,040 --> 06:54:12,638 400 okay so let's add the Asing keyword 9867 06:54:12,638 --> 06:54:14,718 in front of our callback function for 9868 06:54:14,718 --> 06:54:17,398 this test and now we're going to go 9869 06:54:17,398 --> 06:54:21,000 ahead and call create user Handler so 9870 06:54:21,000 --> 06:54:23,600 after we call our function 9871 06:54:23,600 --> 06:54:27,320 we want to actually mock out the is 9872 06:54:27,320 --> 06:54:31,200 empty method to return true so that way 9873 06:54:31,200 --> 06:54:33,878 goes to the match data function call so 9874 06:54:33,878 --> 06:54:36,160 inside our test here's what we're going 9875 06:54:36,160 --> 06:54:36,878 to 9876 06:54:36,878 --> 06:54:40,280 do we're going to use just. 9877 06:54:40,280 --> 06:54:43,840 spyon and I'm going to go and reference 9878 06:54:43,840 --> 06:54:46,160 um this import up here validator so 9879 06:54:46,160 --> 06:54:50,280 that's the entire Express validator 9880 06:54:50,398 --> 06:54:53,200 package and then we're going to spy on 9881 06:54:53,200 --> 06:54:57,478 on uh this function right over here it's 9882 06:54:57,478 --> 06:54:59,478 validation 9883 06:54:59,478 --> 06:55:03,680 result like this okay and then what I 9884 06:55:03,680 --> 06:55:05,878 can do is I can 9885 06:55:05,878 --> 06:55:09,080 use this mock implementation 9886 06:55:09,080 --> 06:55:11,638 once and then in here this is where I 9887 06:55:11,638 --> 06:55:13,478 can actually mock the actual 9888 06:55:13,478 --> 06:55:15,280 implementation of the 9889 06:55:15,280 --> 06:55:18,320 function and I can have a 9890 06:55:18,320 --> 06:55:21,120 return an object so the same result 9891 06:55:21,120 --> 06:55:23,160 object but this time that object will 9892 06:55:23,160 --> 06:55:25,558 have this is empty 9893 06:55:25,558 --> 06:55:27,680 function so this is also going to be a 9894 06:55:27,680 --> 06:55:30,760 mock function and it will return true 9895 06:55:30,760 --> 06:55:34,240 like that okay so hopefully that makes 9896 06:55:34,240 --> 06:55:36,040 sense so this is how I can 9897 06:55:36,040 --> 06:55:39,360 override the mocked value of validation 9898 06:55:39,360 --> 06:55:42,000 result into something else so now what 9899 06:55:42,000 --> 06:55:44,360 will happen is create user Handler will 9900 06:55:44,360 --> 06:55:46,638 be called it's going to go ahead and 9901 06:55:46,638 --> 06:55:48,760 call validation result and then result 9902 06:55:48,760 --> 06:55:51,478 that is empty will be called and we can 9903 06:55:51,478 --> 06:55:54,040 see that's going to a result to true and 9904 06:55:54,040 --> 06:55:55,798 then negating that will cause this whole 9905 06:55:55,798 --> 06:55:58,638 if condition to be false and then it'll 9906 06:55:58,638 --> 06:56:01,040 go down over here so what I can do is I 9907 06:56:01,040 --> 06:56:03,478 can write an assertion to verify that 9908 06:56:03,478 --> 06:56:07,120 matched data is called so let's do 9909 06:56:07,120 --> 06:56:09,760 expect so remember match data is a 9910 06:56:09,760 --> 06:56:12,000 function that comes from the Express 9911 06:56:12,000 --> 06:56:13,600 validator package so I'm going to 9912 06:56:13,600 --> 06:56:18,160 reference validator do matched 9913 06:56:18,160 --> 06:56:22,600 data to have been called and let's save 9914 06:56:22,600 --> 06:56:24,680 and let's run our test and let's make 9915 06:56:24,680 --> 06:56:26,958 sure that it 9916 06:56:26,958 --> 06:56:31,558 passes okay so it actually is um let's 9917 06:56:31,558 --> 06:56:34,080 see okay yeah don't worry about this so 9918 06:56:34,080 --> 06:56:37,000 it is actually supposed to pass but the 9919 06:56:37,000 --> 06:56:39,240 reason why it's failing is because right 9920 06:56:39,240 --> 06:56:42,240 now it's actually trying to uh call the 9921 06:56:42,240 --> 06:56:43,718 next few functions it's trying to call 9922 06:56:43,718 --> 06:56:46,040 Hash password and then it's also trying 9923 06:56:46,040 --> 06:56:48,320 to call the database by Saving this new 9924 06:56:48,320 --> 06:56:51,080 user to the database so don't worry uh 9925 06:56:51,080 --> 06:56:54,798 it is working it's just that um we're 9926 06:56:54,798 --> 06:56:56,040 it's trying to connect to the database 9927 06:56:56,040 --> 06:56:57,520 and you can see that it actually timed 9928 06:56:57,520 --> 06:56:59,280 out because we don't actually have a 9929 06:56:59,280 --> 06:57:01,360 database connection so don't worry we 9930 06:57:01,360 --> 06:57:03,240 actually did the right thing we just 9931 06:57:03,240 --> 06:57:05,840 need to mock the rest of our function 9932 06:57:05,840 --> 06:57:07,760 okay so we know that match data is being 9933 06:57:07,760 --> 06:57:09,798 called and you know what let me just 9934 06:57:09,798 --> 06:57:12,478 comment all this out and run the test 9935 06:57:12,478 --> 06:57:14,360 again because I just want to show you 9936 06:57:14,360 --> 06:57:18,760 all that it passes great so now let's go 9937 06:57:18,760 --> 06:57:21,080 ahead and mock hash password because 9938 06:57:21,080 --> 06:57:23,240 again we don't want to actually call 9939 06:57:23,240 --> 06:57:24,958 these functions because remember these 9940 06:57:24,958 --> 06:57:27,080 functions these dependencies produce 9941 06:57:27,080 --> 06:57:30,958 side effects okay so we're going to go 9942 06:57:30,958 --> 06:57:34,240 up top over here and now I want to mock 9943 06:57:34,240 --> 06:57:36,840 this hash password function that comes 9944 06:57:36,840 --> 06:57:40,040 from my helpers 9945 06:57:40,160 --> 06:57:43,160 module so what I'm going to do is I'm 9946 06:57:43,160 --> 06:57:45,840 going to call just. Mock and I want to 9947 06:57:45,840 --> 06:57:49,478 pass the path to that module so we're in 9948 06:57:49,478 --> 06:57:51,360 the test folder so we need to go out one 9949 06:57:51,360 --> 06:57:53,080 folder 9950 06:57:53,080 --> 06:57:57,958 into utils helpers MJS like this okay 9951 06:57:57,958 --> 06:58:00,200 and what I want to do is I want to have 9952 06:58:00,200 --> 06:58:01,240 hash 9953 06:58:01,240 --> 06:58:03,878 password um I want to I want to mock the 9954 06:58:03,878 --> 06:58:07,360 implementation so let me pass in the 9955 06:58:07,360 --> 06:58:09,718 factory and return an 9956 06:58:09,718 --> 06:58:13,200 object and just overwrite hash password 9957 06:58:13,200 --> 06:58:14,638 and so this will be a mock function but 9958 06:58:14,638 --> 06:58:18,240 I I actually want to have it return some 9959 06:58:18,240 --> 06:58:20,760 custom logic and all I want to make it 9960 06:58:20,760 --> 06:58:23,878 do is takeing an argument so I'll just 9961 06:58:23,878 --> 06:58:26,798 do password so inside this callback 9962 06:58:26,798 --> 06:58:28,360 function I can pass in this argument 9963 06:58:28,360 --> 06:58:31,478 password and all I'm going to do is just 9964 06:58:31,478 --> 06:58:33,120 concatenate 9965 06:58:33,120 --> 06:58:34,760 um 9966 06:58:34,760 --> 06:58:37,200 hashed with password like this I'm just 9967 06:58:37,200 --> 06:58:39,160 going to literally prefix the password 9968 06:58:39,160 --> 06:58:40,760 with hash so that way it at least does 9969 06:58:40,760 --> 06:58:42,398 something 9970 06:58:42,398 --> 06:58:46,040 okay all right so we just mocked hash 9971 06:58:46,040 --> 06:58:49,638 password or hash password so now let's 9972 06:58:49,638 --> 06:58:51,240 go ahead 9973 06:58:51,240 --> 06:58:55,000 and import up top over here we're going 9974 06:58:55,000 --> 06:58:56,360 to import 9975 06:58:56,360 --> 06:58:58,798 helpers 9976 06:58:58,798 --> 06:59:02,478 from utils helpers okay and this would 9977 06:59:02,478 --> 06:59:05,558 allow me to reference helpers like an 9978 06:59:05,558 --> 06:59:07,398 object and I should be able to reference 9979 06:59:07,398 --> 06:59:09,360 hash password or maybe I need to do 9980 06:59:09,360 --> 06:59:12,478 import as 9981 06:59:12,478 --> 06:59:15,718 helpers yeah there we go okay I think I 9982 06:59:15,718 --> 06:59:17,558 may have need to 9983 06:59:17,558 --> 06:59:20,798 do that as well for validator I'm not 9984 06:59:20,798 --> 06:59:24,398 too sure let me just run everything and 9985 06:59:24,398 --> 06:59:27,000 make sure it's 9986 06:59:33,680 --> 06:59:36,920 good let me go over here and just come 9987 06:59:36,920 --> 06:59:39,440 this out real 9988 06:59:40,120 --> 06:59:43,680 quick okay there we go perfect okay just 9989 06:59:43,680 --> 06:59:45,878 wanted to make sure that didn't break 9990 06:59:45,878 --> 06:59:48,680 okay so now we're going to go down over 9991 06:59:48,680 --> 06:59:52,200 here and we have wrote an assertion for 9992 06:59:52,200 --> 06:59:55,320 for the Matched data function okay to 9993 06:59:55,320 --> 06:59:58,280 have been called uh let me also change 9994 06:59:58,280 --> 07:00:00,958 this to have been called with mock 9995 07:00:00,958 --> 07:00:04,080 request to be more 9996 07:00:04,080 --> 07:00:06,080 specific and then we're going to go 9997 07:00:06,080 --> 07:00:08,840 ahead and write an assertion for hash 9998 07:00:08,840 --> 07:00:11,000 password so what I'm going to do is I'm 9999 07:00:11,000 --> 07:00:15,760 going to expect helpers do password to 10000 07:00:15,760 --> 07:00:18,200 have been called and I want to verify 10001 07:00:18,200 --> 07:00:21,000 that it was called with data. password 10002 07:00:21,000 --> 07:00:22,878 so data. password password is going to 10003 07:00:22,878 --> 07:00:25,280 be whatever matched data returned so up 10004 07:00:25,280 --> 07:00:28,920 top over here matched data returned 10005 07:00:28,920 --> 07:00:30,920 password so I'm going to say to have 10006 07:00:30,920 --> 07:00:34,840 been called with password like 10007 07:00:34,840 --> 07:00:36,878 that you know what for the rest of this 10008 07:00:36,878 --> 07:00:39,240 test let me just comment out all this 10009 07:00:39,240 --> 07:00:40,718 stuff over here because I do want to 10010 07:00:40,718 --> 07:00:44,398 actually see our assertions passing okay 10011 07:00:44,398 --> 07:00:47,160 good so to have been called with 10012 07:00:47,160 --> 07:00:50,878 password and then I also want to make 10013 07:00:50,878 --> 07:00:55,200 sure that hash password so helpers hash 10014 07:00:55,200 --> 07:00:58,558 password returns with the correct value 10015 07:00:58,558 --> 07:01:03,360 so to return to have 10016 07:01:04,478 --> 07:01:09,798 returned returned with hashed password 10017 07:01:09,798 --> 07:01:11,920 and I know I'm just hardcoding these 10018 07:01:11,920 --> 07:01:14,398 values but ideally for you you would 10019 07:01:14,398 --> 07:01:15,760 probably not want to hardcode these 10020 07:01:15,760 --> 07:01:17,398 values so that way you can make it more 10021 07:01:17,398 --> 07:01:19,558 Dynamic but I'm just doing this for 10022 07:01:19,558 --> 07:01:21,558 testing purposes okay just to show you 10023 07:01:21,558 --> 07:01:22,638 an example 10024 07:01:22,638 --> 07:01:24,320 so if I run the test you can see that it 10025 07:01:24,320 --> 07:01:28,200 will in fact return hashed password and 10026 07:01:28,200 --> 07:01:29,760 that's just our mock 10027 07:01:29,760 --> 07:01:31,798 implementation right over here nothing 10028 07:01:31,798 --> 07:01:34,000 special but at least it is returning 10029 07:01:34,000 --> 07:01:35,478 with something so we can verify that 10030 07:01:35,478 --> 07:01:39,160 data. password is uh storing the correct 10031 07:01:39,160 --> 07:01:42,680 return value okay so now the next part 10032 07:01:42,680 --> 07:01:43,558 we want 10033 07:01:43,558 --> 07:01:46,478 to um test this part over here you see 10034 07:01:46,478 --> 07:01:48,840 how we are creating a new user so this 10035 07:01:48,840 --> 07:01:51,798 is where we are invoking the Constructor 10036 07:01:51,798 --> 07:01:52,958 of class so now you're probably 10037 07:01:52,958 --> 07:01:56,040 wondering well how do we test that so 10038 07:01:56,040 --> 07:01:58,398 what we can do is we can actually mock 10039 07:01:58,398 --> 07:02:01,440 these es6 classes it's actually not that 10040 07:02:01,440 --> 07:02:03,478 difficult and I'll show you how to do it 10041 07:02:03,478 --> 07:02:06,680 so what we can do is right over in our 10042 07:02:06,680 --> 07:02:12,080 test file up top over here I can go here 10043 07:02:12,080 --> 07:02:16,160 just. Mock and I can just pass the path 10044 07:02:16,160 --> 07:02:20,638 to our our user right over here this 10045 07:02:20,638 --> 07:02:24,440 this user. MJS file inside schemas 10046 07:02:24,440 --> 07:02:26,600 so I need to go out One Directory to 10047 07:02:26,600 --> 07:02:30,398 schemas user. 10048 07:02:30,398 --> 07:02:33,638 MJS okay and then up top over here you 10049 07:02:33,638 --> 07:02:35,360 know let me remove this mock users 10050 07:02:35,360 --> 07:02:40,160 import let me import user 10051 07:02:40,160 --> 07:02:45,160 from the uh mongu schemas user. MJS file 10052 07:02:45,160 --> 07:02:47,760 okay so I just mocked that whole module 10053 07:02:47,760 --> 07:02:51,680 right over here and now what we can do 10054 07:02:51,680 --> 07:02:54,280 is is so I uncommented this line over 10055 07:02:54,280 --> 07:02:56,840 here where we call the new user 10056 07:02:56,840 --> 07:02:59,840 Constructor and I can write an 10057 07:02:59,840 --> 07:03:02,320 assertion and I can say expect user so 10058 07:03:02,320 --> 07:03:05,000 I'm referencing the entire model which 10059 07:03:05,000 --> 07:03:07,920 is a class and that's coming from this 10060 07:03:07,920 --> 07:03:10,280 input right over here okay this 10061 07:03:10,280 --> 07:03:13,120 is our model right over 10062 07:03:13,120 --> 07:03:15,478 here and then what I'm going to do is 10063 07:03:15,478 --> 07:03:19,920 I'm going to use the two have been 10064 07:03:19,920 --> 07:03:22,520 called assertion 10065 07:03:22,520 --> 07:03:24,718 and I'm going to run the test and it's 10066 07:03:24,718 --> 07:03:27,798 going to pass so this verifies that the 10067 07:03:27,798 --> 07:03:30,878 Constructor was actually invoked if I 10068 07:03:30,878 --> 07:03:32,558 comment this out you're going to see 10069 07:03:32,558 --> 07:03:35,240 that it's going to fail because it it 10070 07:03:35,240 --> 07:03:37,360 actually was called or it actually 10071 07:03:37,360 --> 07:03:39,360 wasn't called but we wrote the assertion 10072 07:03:39,360 --> 07:03:41,840 to see that it was called so let me 10073 07:03:41,840 --> 07:03:43,920 uncomment that and let's just change 10074 07:03:43,920 --> 07:03:46,798 this to to have been called with the 10075 07:03:46,798 --> 07:03:48,240 correct arguments so we want to make 10076 07:03:48,240 --> 07:03:50,440 sure that it was called with the correct 10077 07:03:50,440 --> 07:03:52,320 username display name 10078 07:03:52,320 --> 07:03:56,040 and the hashed password okay so in our 10079 07:03:56,040 --> 07:03:58,558 test let's go back up over here so 10080 07:03:58,558 --> 07:04:02,240 notice how this is the return value of 10081 07:04:02,240 --> 07:04:05,120 matched data because we are passing uh 10082 07:04:05,120 --> 07:04:07,638 matched data whatever this return value 10083 07:04:07,638 --> 07:04:11,440 is into the user Constructor only with 10084 07:04:11,440 --> 07:04:13,920 the password being hashed so what I can 10085 07:04:13,920 --> 07:04:16,398 do is I can pass in that object and just 10086 07:04:16,398 --> 07:04:19,840 change this to Hash Hash password and 10087 07:04:19,840 --> 07:04:22,398 remember this value comes from our mock 10088 07:04:22,398 --> 07:04:24,478 function over here we just prefixed 10089 07:04:24,478 --> 07:04:26,478 hashed uncore with whatever the password 10090 07:04:26,478 --> 07:04:31,200 is so let's go ahead and run the 10091 07:04:31,760 --> 07:04:34,920 test and you can see that now it passes 10092 07:04:34,920 --> 07:04:37,320 so that's good and now one more thing 10093 07:04:37,320 --> 07:04:38,798 that I also want to show you is if you 10094 07:04:38,798 --> 07:04:40,798 wanted to access the instance that was 10095 07:04:40,798 --> 07:04:43,958 created so the user instance you can do 10096 07:04:43,958 --> 07:04:46,840 that by referencing user and it's going 10097 07:04:46,840 --> 07:04:48,638 to attach this mock property onto that 10098 07:04:48,638 --> 07:04:50,680 user and then you can reference 10099 07:04:50,680 --> 07:04:53,520 instances at subscript zero because we 10100 07:04:53,520 --> 07:04:56,160 only created one instance and then let's 10101 07:04:56,160 --> 07:04:58,878 run the test 10102 07:04:59,240 --> 07:05:01,878 again and you can see right over here we 10103 07:05:01,878 --> 07:05:05,240 have the instance and this instance has 10104 07:05:05,240 --> 07:05:08,760 this save method as 10105 07:05:08,760 --> 07:05:11,520 well and you could of course also 10106 07:05:11,520 --> 07:05:15,680 reference the properties on 10107 07:05:15,760 --> 07:05:18,920 it so let's go ahead and continue 10108 07:05:18,920 --> 07:05:21,280 testing the rest of our code so I'm 10109 07:05:21,280 --> 07:05:23,878 going to comment this out and since 10110 07:05:23,878 --> 07:05:26,160 we've already mocked out the user 10111 07:05:26,160 --> 07:05:29,478 model or mongus model I can actually 10112 07:05:29,478 --> 07:05:31,638 just run my test and we shouldn't get 10113 07:05:31,638 --> 07:05:33,760 the same errors that we had before 10114 07:05:33,760 --> 07:05:36,040 because all that is mocked out already 10115 07:05:36,040 --> 07:05:38,320 okay this save method is mocked 10116 07:05:38,320 --> 07:05:40,958 everything so what I want to do is I 10117 07:05:40,958 --> 07:05:43,360 want to verify that the save method was 10118 07:05:43,360 --> 07:05:46,080 called on the instance this new user 10119 07:05:46,080 --> 07:05:47,798 instance 10120 07:05:47,798 --> 07:05:50,398 so what I can do is this I can actually 10121 07:05:50,398 --> 07:05:53,760 create another spy on this save method 10122 07:05:53,760 --> 07:05:56,120 so I can keep track of its calls because 10123 07:05:56,120 --> 07:05:58,360 there's no way for me to directly ask 10124 07:05:58,360 --> 07:06:01,280 this unless if I get the instance so for 10125 07:06:01,280 --> 07:06:04,600 example if I wanted to I could do user. 10126 07:06:04,600 --> 07:06:05,680 mo. 10127 07:06:05,680 --> 07:06:10,798 instances zero do saave to have been 10128 07:06:10,798 --> 07:06:13,958 called like that and this should work 10129 07:06:13,958 --> 07:06:16,600 without any 10130 07:06:16,638 --> 07:06:19,080 problem yep and then if I try to negate 10131 07:06:19,080 --> 07:06:22,080 this call to verify that it's it's not 10132 07:06:22,080 --> 07:06:23,920 passing incorrectly you can see that it 10133 07:06:23,920 --> 07:06:25,520 actually was called so this is one way 10134 07:06:25,520 --> 07:06:27,558 that you can do this the other way that 10135 07:06:27,558 --> 07:06:29,718 you can do this is you can use a spy and 10136 07:06:29,718 --> 07:06:32,398 I'll show you how to do that so I'll 10137 07:06:32,398 --> 07:06:35,840 create a variable I'll call this save 10138 07:06:35,840 --> 07:06:39,920 method equals just. spy on and I can 10139 07:06:39,920 --> 07:06:41,680 reference user. 10140 07:06:41,680 --> 07:06:45,040 prototype like that and then what I can 10141 07:06:45,040 --> 07:06:47,920 do is I can specify the actual method 10142 07:06:47,920 --> 07:06:50,760 itself on the instance so save is one of 10143 07:06:50,760 --> 07:06:52,320 the methods 10144 07:06:52,320 --> 07:06:54,920 and then leave it like 10145 07:06:54,920 --> 07:06:57,080 that and what I can 10146 07:06:57,080 --> 07:07:00,638 do is I can go ahead and replace this 10147 07:07:00,638 --> 07:07:03,440 part to be expect save method to have 10148 07:07:03,440 --> 07:07:04,718 been 10149 07:07:04,718 --> 07:07:08,160 called okay and if I run test again the 10150 07:07:08,160 --> 07:07:10,280 test will pass and if I try 10151 07:07:10,280 --> 07:07:13,200 to add the not operator in front of 10152 07:07:13,200 --> 07:07:16,120 it you can see that by adding not 10153 07:07:16,120 --> 07:07:17,878 operator the test fails because it 10154 07:07:17,878 --> 07:07:21,920 actually was called so what I want to do 10155 07:07:21,920 --> 07:07:23,558 specifically is I want to actually mock 10156 07:07:23,558 --> 07:07:26,398 the return value of save so what I'm 10157 07:07:26,398 --> 07:07:31,398 going to do is at the end of just. spyon 10158 07:07:31,398 --> 07:07:36,080 I'm going to call mock resolved value 10159 07:07:36,080 --> 07:07:39,200 once and I want save to just return the 10160 07:07:39,200 --> 07:07:41,478 user that should be saved to the 10161 07:07:41,478 --> 07:07:43,878 database but I'm just going to 10162 07:07:43,878 --> 07:07:47,160 attach an ID you don't have to do this 10163 07:07:47,160 --> 07:07:48,840 but I'm just going to do it 10164 07:07:48,840 --> 07:07:51,798 anyways and then I'm going to copy that 10165 07:07:51,798 --> 07:07:53,200 so that way like the reason why I'm 10166 07:07:53,200 --> 07:07:54,558 attaching the ID is that way it makes it 10167 07:07:54,558 --> 07:07:55,840 feel like it's being saved to the 10168 07:07:55,840 --> 07:07:58,280 database even though it's not because 10169 07:07:58,280 --> 07:08:00,878 mongodb generates an ID for 10170 07:08:00,878 --> 07:08:04,798 us so now what I'll do is I'm going to 10171 07:08:04,798 --> 07:08:06,360 go down over 10172 07:08:06,360 --> 07:08:09,120 here and what I'm going to do is I'm 10173 07:08:09,120 --> 07:08:10,558 going 10174 07:08:10,558 --> 07:08:13,680 to let's see so we are asserting that 10175 07:08:13,680 --> 07:08:16,160 save was called so now let's write an 10176 07:08:16,160 --> 07:08:19,000 assertion on response. status we'll 10177 07:08:19,000 --> 07:08:20,840 verify that it was called with the 10178 07:08:20,840 --> 07:08:22,878 status code of 2011 and then we'll 10179 07:08:22,878 --> 07:08:24,638 verify that the response object was 10180 07:08:24,638 --> 07:08:27,240 called with the send method or called it 10181 07:08:27,240 --> 07:08:29,638 called the send method with the saved 10182 07:08:29,638 --> 07:08:31,840 user which means that it includes that 10183 07:08:31,840 --> 07:08:34,240 user over here that we are mocking the 10184 07:08:34,240 --> 07:08:39,000 resolved value so let's do expect mock 10185 07:08:39,000 --> 07:08:42,878 response. status to have been called 10186 07:08:42,878 --> 07:08:45,160 with 10187 07:08:45,160 --> 07:08:49,000 2011 and then let me just go down 10188 07:08:49,000 --> 07:08:51,360 here let me change this to mock resp 10189 07:08:51,360 --> 07:08:55,280 response. send and let me just copy this 10190 07:08:55,280 --> 07:08:57,360 object paste it 10191 07:08:57,360 --> 07:09:00,558 here and we should be good so let's test 10192 07:09:00,558 --> 07:09:05,320 this out let me run my 10193 07:09:05,520 --> 07:09:08,320 test and you can see that our test 10194 07:09:08,320 --> 07:09:11,760 passes so that's good all right so we've 10195 07:09:11,760 --> 07:09:14,280 covered two cases so far for our crate 10196 07:09:14,280 --> 07:09:16,240 user Handler let's go ahead and cover 10197 07:09:16,240 --> 07:09:17,600 the last case this one's going to be 10198 07:09:17,600 --> 07:09:20,280 pretty easy we just need to get it to 10199 07:09:20,280 --> 07:09:22,600 actually throw this error so here's what 10200 07:09:22,600 --> 07:09:24,920 we're going to do we're going to go 10201 07:09:24,920 --> 07:09:27,680 ahead and create a new 10202 07:09:27,680 --> 07:09:31,280 test so I'll go ahead and call this test 10203 07:09:31,280 --> 07:09:35,080 let's see um it should throw error when 10204 07:09:35,080 --> 07:09:37,478 save or let's see it should send a 10205 07:09:37,478 --> 07:09:40,280 status code of 400 when save uh errors 10206 07:09:40,280 --> 07:09:42,240 out okay so everything else is good 10207 07:09:42,240 --> 07:09:44,398 except for when uh the save method is 10208 07:09:44,398 --> 07:09:45,680 called maybe something failed the 10209 07:09:45,680 --> 07:09:48,120 database and then it throws us th throws 10210 07:09:48,120 --> 07:09:52,160 this error and it sends a status of 400 10211 07:09:52,160 --> 07:09:57,160 so let's do should send status of 400 10212 07:09:57,160 --> 07:10:01,680 when database fails to save user so I 10213 07:10:01,680 --> 07:10:04,558 think that's good enough and let's make 10214 07:10:04,558 --> 07:10:07,320 this an async method so we're going 10215 07:10:07,320 --> 07:10:11,160 to uh let's see we're going to copy this 10216 07:10:11,160 --> 07:10:13,600 create user Handler right here so 10217 07:10:13,600 --> 07:10:16,120 everything is going to be roughly the 10218 07:10:16,120 --> 07:10:19,080 same as the previous test so I do also 10219 07:10:19,080 --> 07:10:23,120 want to copy this just 10220 07:10:23,120 --> 07:10:25,280 spyon because we do want to make sure 10221 07:10:25,280 --> 07:10:28,680 that we are going past this part we're 10222 07:10:28,680 --> 07:10:30,040 going all the way down but then it we 10223 07:10:30,040 --> 07:10:31,958 just want it to fail when it calls the 10224 07:10:31,958 --> 07:10:34,718 save method okay so the thing is though 10225 07:10:34,718 --> 07:10:37,240 we don't need to rewrite all of these uh 10226 07:10:37,240 --> 07:10:39,160 assertions because we already did 10227 07:10:39,160 --> 07:10:43,718 before so let me do this um con save 10228 07:10:43,718 --> 07:10:46,478 method yep so let me copy this save 10229 07:10:46,478 --> 07:10:49,558 method equals just. spyon let me copy 10230 07:10:49,558 --> 07:10:53,160 this part 10231 07:10:53,558 --> 07:10:54,558 and 10232 07:10:54,558 --> 07:10:57,160 now what we are going to 10233 07:10:57,160 --> 07:11:00,718 do is we're going to go 10234 07:11:00,718 --> 07:11:04,320 ahead and for the save method for this 10235 07:11:04,320 --> 07:11:06,320 spy we're going to mock the 10236 07:11:06,320 --> 07:11:09,680 implementation one time and remember 10237 07:11:09,680 --> 07:11:11,320 this save method 10238 07:11:11,320 --> 07:11:14,120 throws it it could throw an error but 10239 07:11:14,120 --> 07:11:15,000 we're just going to mock the 10240 07:11:15,000 --> 07:11:16,760 implementation so when it does throw an 10241 07:11:16,760 --> 07:11:20,798 error then it's going to handle this 10242 07:11:20,798 --> 07:11:23,080 catch block over here but since this 10243 07:11:23,080 --> 07:11:25,840 save method uh is asynchronous it 10244 07:11:25,840 --> 07:11:28,160 returns a promise so we have to do 10245 07:11:28,160 --> 07:11:31,440 promise do reject and we'll just say 10246 07:11:31,440 --> 07:11:35,240 failed to save user okay so now we're 10247 07:11:35,240 --> 07:11:37,040 basically overriding the save method to 10248 07:11:37,040 --> 07:11:39,398 actually error out 10249 07:11:39,398 --> 07:11:43,398 return um return an error so everything 10250 07:11:43,398 --> 07:11:46,000 in our code should be good and then once 10251 07:11:46,000 --> 07:11:48,040 it gets to this save method it's going 10252 07:11:48,040 --> 07:11:49,920 to go ahead and error out and then we're 10253 07:11:49,920 --> 07:11:51,760 going to go ahead and 10254 07:11:51,760 --> 07:11:53,440 write an assertion on response and 10255 07:11:53,440 --> 07:11:57,120 Status okay so let's go ahead and do 10256 07:11:57,120 --> 07:12:01,040 this let's do expect save method to have 10257 07:12:01,040 --> 07:12:03,080 been 10258 07:12:03,080 --> 07:12:05,638 called and then what we'll do is we'll 10259 07:12:05,638 --> 07:12:09,878 do expect mock response that's send or 10260 07:12:09,878 --> 07:12:11,360 I'm sorry send 10261 07:12:11,360 --> 07:12:14,840 status right over here to have been 10262 07:12:14,840 --> 07:12:18,000 called with 400 and let's go ahead and 10263 07:12:18,000 --> 07:12:20,040 run our 10264 07:12:20,040 --> 07:12:21,760 test 10265 07:12:21,760 --> 07:12:24,040 let's make sure that it works and there 10266 07:12:24,040 --> 07:12:26,718 we go we have covered all three cases if 10267 07:12:26,718 --> 07:12:28,680 you want you can go ahead and write some 10268 07:12:28,680 --> 07:12:30,200 assertions for some of these functions 10269 07:12:30,200 --> 07:12:32,280 to verify that they were called but you 10270 07:12:32,280 --> 07:12:34,840 don't really have to is it would just 10271 07:12:34,840 --> 07:12:36,240 kind of be redundant with what we have 10272 07:12:36,240 --> 07:12:38,040 up here CU at this point in this test we 10273 07:12:38,040 --> 07:12:39,478 know that all these functions are being 10274 07:12:39,478 --> 07:12:41,080 called it's just that we're trying to 10275 07:12:41,080 --> 07:12:42,680 get to the point right down over here to 10276 07:12:42,680 --> 07:12:45,200 make sure that we are testing for uh 10277 07:12:45,200 --> 07:12:47,478 this send status being called with a 10278 07:12:47,478 --> 07:12:50,398 status code of 400 and if I were to pass 10279 07:12:50,398 --> 07:12:52,160 401 10280 07:12:52,160 --> 07:12:53,320 that test would fail because it is 10281 07:12:53,320 --> 07:12:54,360 supposed to be 10282 07:12:54,360 --> 07:12:57,798 400 and there you go so hopefully all 10283 07:12:57,798 --> 07:12:59,760 this makes sense I know this this part 10284 07:12:59,760 --> 07:13:02,680 of the tutorial was pretty long but 10285 07:13:02,680 --> 07:13:04,638 that's because there's a lot of stuff 10286 07:13:04,638 --> 07:13:06,320 that I wanted to cover and I felt like 10287 07:13:06,320 --> 07:13:09,160 with this specific test you learn so 10288 07:13:09,160 --> 07:13:11,760 much with how to mock modules how to 10289 07:13:11,760 --> 07:13:14,718 mock classes how to mock your own 10290 07:13:14,718 --> 07:13:17,718 modules that you create yourself on your 10291 07:13:17,718 --> 07:13:19,718 uh on your in in your source code so 10292 07:13:19,718 --> 07:13:21,718 your local modules 10293 07:13:21,718 --> 07:13:24,280 um and just a lot of stuff about unit 10294 07:13:24,280 --> 07:13:26,718 testing so hopefully this tutorial made 10295 07:13:26,718 --> 07:13:29,320 sense and I would highly encourage you 10296 07:13:29,320 --> 07:13:32,120 to practice writing more tests more unit 10297 07:13:32,120 --> 07:13:35,000 tests for your other functions as well 10298 07:13:35,000 --> 07:13:36,920 so for example one thing that I can 10299 07:13:36,920 --> 07:13:39,680 encourage you to do is maybe for the 10300 07:13:39,680 --> 07:13:41,240 local strategy if this is something that 10301 07:13:41,240 --> 07:13:44,520 you have you can go ahead and take let's 10302 07:13:44,520 --> 07:13:46,558 see you can probably take 10303 07:13:46,558 --> 07:13:50,120 this part out you can take this 10304 07:13:50,120 --> 07:13:52,638 Anonymous function out put it in a 10305 07:13:52,638 --> 07:13:54,798 separate file and import it in here and 10306 07:13:54,798 --> 07:13:56,280 then that way you can actually import 10307 07:13:56,280 --> 07:13:58,920 that function into a test file and test 10308 07:13:58,920 --> 07:14:00,680 the logic in here and using the logic 10309 07:14:00,680 --> 07:14:03,798 that I showed you with how to Mock and 10310 07:14:03,798 --> 07:14:07,080 how to override values of functions by 10311 07:14:07,080 --> 07:14:09,360 mocking them out you can write those 10312 07:14:09,360 --> 07:14:11,558 unit tests so hopefully all of this 10313 07:14:11,558 --> 07:14:13,920 makes 10314 07:14:15,240 --> 07:14:17,878 sense in this part of the expressjs 10315 07:14:17,878 --> 07:14:19,320 tutorial I'm going to teach you how to 10316 07:14:19,320 --> 07:14:22,558 write integration and end2end tests for 10317 07:14:22,558 --> 07:14:25,360 your Express server so integration tests 10318 07:14:25,360 --> 07:14:28,200 and unit tests basically involve testing 10319 07:14:28,200 --> 07:14:29,920 your entire application at least with 10320 07:14:29,920 --> 07:14:32,520 integration tests you typically are 10321 07:14:32,520 --> 07:14:35,120 testing certain scenarios and flows in 10322 07:14:35,120 --> 07:14:37,200 your application like for example create 10323 07:14:37,200 --> 07:14:40,360 a user and then expect that user to uh 10324 07:14:40,360 --> 07:14:42,478 return as a response and then try 10325 07:14:42,478 --> 07:14:44,840 logging in as that user after you've 10326 07:14:44,840 --> 07:14:47,398 created that's an example of writing an 10327 07:14:47,398 --> 07:14:49,040 into and test okay you have different 10328 07:14:49,040 --> 07:14:51,280 scenarios it's a lot different that unit 10329 07:14:51,280 --> 07:14:54,040 tests where unit tests you are only 10330 07:14:54,040 --> 07:14:56,320 testing one piece of your entire code 10331 07:14:56,320 --> 07:14:58,440 base like one single function and many 10332 07:14:58,440 --> 07:14:59,878 of you who have seen the previous 10333 07:14:59,878 --> 07:15:01,398 section where I showed you how to write 10334 07:15:01,398 --> 07:15:04,080 unit tests may actually find writing 10335 07:15:04,080 --> 07:15:05,440 integration tests to be a lot more 10336 07:15:05,440 --> 07:15:06,760 easier because you don't have to worry 10337 07:15:06,760 --> 07:15:09,240 about setting up a bunch of mocks okay 10338 07:15:09,240 --> 07:15:10,878 some people do get confused with how to 10339 07:15:10,878 --> 07:15:12,680 Mock and what to actually mock but with 10340 07:15:12,680 --> 07:15:14,478 integration and ENT test you just call 10341 07:15:14,478 --> 07:15:16,878 your API and then you write assertions 10342 07:15:16,878 --> 07:15:18,680 so the first thing that I'm going to do 10343 07:15:18,680 --> 07:15:20,840 is I'm going to actually disable the OA 10344 07:15:20,840 --> 07:15:22,680 2 configuration with the Discord 10345 07:15:22,680 --> 07:15:23,840 strategy right over here I'm going to 10346 07:15:23,840 --> 07:15:25,120 comment it out and I'm going to 10347 07:15:25,120 --> 07:15:26,920 uncomment out the local strategy because 10348 07:15:26,920 --> 07:15:28,920 I want to actually use local 10349 07:15:28,920 --> 07:15:30,360 authentication we're not going to be 10350 07:15:30,360 --> 07:15:32,600 testing oath 2 we're going to be testing 10351 07:15:32,600 --> 07:15:35,040 actual local authentication where we 10352 07:15:35,040 --> 07:15:37,000 create a user and then we can log in 10353 07:15:37,000 --> 07:15:38,440 with that user and then we will verify 10354 07:15:38,440 --> 07:15:40,360 that the user was created and that we've 10355 07:15:40,360 --> 07:15:43,798 logged in so let me just do that so just 10356 07:15:43,798 --> 07:15:45,638 uncomment this file everything else is 10357 07:15:45,638 --> 07:15:47,958 set up the way that it should be and 10358 07:15:47,958 --> 07:15:50,798 then we should have our both serialized 10359 07:15:50,798 --> 07:15:53,520 these serialized user functions okay so 10360 07:15:53,520 --> 07:15:54,920 we're done with this part just want to 10361 07:15:54,920 --> 07:15:57,520 do that very quickly now let's go ahead 10362 07:15:57,520 --> 07:16:00,000 and install super test so you're going 10363 07:16:00,000 --> 07:16:03,440 to type npmi hyphen 10364 07:16:03,440 --> 07:16:06,360 D super test and we're going to use 10365 07:16:06,360 --> 07:16:08,600 super test with just they both work very 10366 07:16:08,600 --> 07:16:10,680 well with each 10367 07:16:10,680 --> 07:16:13,920 other and we already have just set up 10368 07:16:13,920 --> 07:16:15,638 already so if you missed a previous 10369 07:16:15,638 --> 07:16:17,360 section of this tutorial where we 10370 07:16:17,360 --> 07:16:20,120 configure just and wrote some unit test 10371 07:16:20,120 --> 07:16:21,320 check out at least at least the first 10 10372 07:16:21,320 --> 07:16:23,160 minutes of the video where or maybe like 10373 07:16:23,160 --> 07:16:25,398 the first 5 to 6 minutes where we set up 10374 07:16:25,398 --> 07:16:28,200 just okay then come back to this video 10375 07:16:28,200 --> 07:16:30,478 but all the code is going to be in a 10376 07:16:30,478 --> 07:16:32,320 GitHub Link in the description so check 10377 07:16:32,320 --> 07:16:34,000 that out so you don't have to like you 10378 07:16:34,000 --> 07:16:35,398 know watch the whole thing you just copy 10379 07:16:35,398 --> 07:16:37,520 the code and get the setup and then come 10380 07:16:37,520 --> 07:16:40,360 back to this video so I installed super 10381 07:16:40,360 --> 07:16:43,360 test now let's go ahead and go back to 10382 07:16:43,360 --> 07:16:45,840 our code what we're going to do is we're 10383 07:16:45,840 --> 07:16:47,360 going to create a folder inside the 10384 07:16:47,360 --> 07:16:48,920 source folder and I'm going to call it 10385 07:16:48,920 --> 07:16:51,718 e2e like this cuz I want to keep all of 10386 07:16:51,718 --> 07:16:55,080 my unit tests separate from my end to 10387 07:16:55,080 --> 07:16:56,760 end tests because it's good practice to 10388 07:16:56,760 --> 07:16:59,040 do that and it's also industry standard 10389 07:16:59,040 --> 07:17:00,878 as well and I'm going to create a new 10390 07:17:00,878 --> 07:17:03,080 file and I'm going to call this index. 10391 07:17:03,080 --> 07:17:05,958 spc. JS or you can call it index. 10392 07:17:05,958 --> 07:17:08,040 test.ts if you 10393 07:17:08,040 --> 07:17:10,478 prefer okay and I'm not going to I'm 10394 07:17:10,478 --> 07:17:11,520 going to leave this blank for now I'm 10395 07:17:11,520 --> 07:17:12,920 not going to write anything just yet 10396 07:17:12,920 --> 07:17:14,240 inside this new 10397 07:17:14,240 --> 07:17:16,920 file what we need to do is we're going 10398 07:17:16,920 --> 07:17:19,920 to go into our package.json file and 10399 07:17:19,920 --> 07:17:23,478 we're going to add a a new test script 10400 07:17:23,478 --> 07:17:25,798 but I'm going to call it test colon e2e 10401 07:17:25,798 --> 07:17:28,120 so that way I can run either only unit 10402 07:17:28,120 --> 07:17:31,440 tests or only endtoend tests for the 10403 07:17:31,440 --> 07:17:33,200 test E2 script the way it's going to 10404 07:17:33,200 --> 07:17:34,958 look like is we're still going to be 10405 07:17:34,958 --> 07:17:37,000 using the just binary so again make sure 10406 07:17:37,000 --> 07:17:40,120 you have just installed and configured 10407 07:17:40,120 --> 07:17:42,558 and then we're going to use this test 10408 07:17:42,558 --> 07:17:45,638 path pattern flag and I have it point to 10409 07:17:45,638 --> 07:17:50,680 the source The Source SL folder so that 10410 07:17:50,680 --> 07:17:53,798 way when I run test colon e2e it's going 10411 07:17:53,798 --> 07:17:56,040 to go ahead and only run the tests 10412 07:17:56,040 --> 07:17:59,240 inside this folder over here okay so we 10413 07:17:59,240 --> 07:18:02,040 can run just the end to end tests and 10414 07:18:02,040 --> 07:18:06,040 not the unit tests with it okay so we're 10415 07:18:06,040 --> 07:18:08,000 done with this now we're going to go 10416 07:18:08,000 --> 07:18:09,160 ahead 10417 07:18:09,160 --> 07:18:12,798 into our index. spc. JS file and what 10418 07:18:12,798 --> 07:18:14,280 I'm going to do is I'm going to import 10419 07:18:14,280 --> 07:18:17,958 Super Test from Super Test like 10420 07:18:17,958 --> 07:18:20,240 this and then I'm just going to show you 10421 07:18:20,240 --> 07:18:22,638 a very very easy example right now I'm 10422 07:18:22,638 --> 07:18:25,200 going to import Express and I'll set up 10423 07:18:25,200 --> 07:18:27,680 a very simple Express server with 10424 07:18:27,680 --> 07:18:30,558 literally nothing no middleware nothing 10425 07:18:30,558 --> 07:18:33,200 okay whoops didn't mean to do that so 10426 07:18:33,200 --> 07:18:35,360 I'm going to call the Express function 10427 07:18:35,360 --> 07:18:37,398 and I'm going to register a 10428 07:18:37,398 --> 07:18:39,520 simple test routes so I'll call that 10429 07:18:39,520 --> 07:18:40,520 route 10430 07:18:40,520 --> 07:18:43,520 hello and we're just going to send back 10431 07:18:43,520 --> 07:18:47,638 a status code of 200 okay and then now 10432 07:18:47,638 --> 07:18:50,920 to actually use super test the first 10433 07:18:50,920 --> 07:18:51,760 thing that I'm going to do is I'm going 10434 07:18:51,760 --> 07:18:53,600 to use the describe function again this 10435 07:18:53,600 --> 07:18:55,878 comes from just so make sure you have 10436 07:18:55,878 --> 07:18:58,080 just configured and I'm going to go 10437 07:18:58,080 --> 07:19:02,520 ahead and call this hello 10438 07:19:02,520 --> 07:19:04,558 endpoint just something 10439 07:19:04,558 --> 07:19:06,638 simple and then I'm going to passing 10440 07:19:06,638 --> 07:19:08,760 that callback function and inside the 10441 07:19:08,760 --> 07:19:10,600 Callback function for describe this is 10442 07:19:10,600 --> 07:19:12,240 where we're going to have all of our 10443 07:19:12,240 --> 07:19:14,120 tests so I'm going to use the it 10444 07:19:14,120 --> 07:19:17,200 function that's part of just to write my 10445 07:19:17,200 --> 07:19:18,240 actual 10446 07:19:18,240 --> 07:19:21,878 test so I'm going to go ahe and just say 10447 07:19:21,878 --> 07:19:24,478 get hello end 10448 07:19:24,478 --> 07:19:28,360 point well actually let me do get hello 10449 07:19:28,360 --> 07:19:31,680 and expect 10450 07:19:31,840 --> 07:19:36,040 200 okay so I have one test inside my 10451 07:19:36,040 --> 07:19:39,040 describe callback function over here and 10452 07:19:39,040 --> 07:19:41,680 I can add more test if I want to so this 10453 07:19:41,680 --> 07:19:43,878 is where we're going to actually call 10454 07:19:43,878 --> 07:19:47,440 our API so we're going to use this 10455 07:19:47,440 --> 07:19:49,000 function actually actually I'm sorry 10456 07:19:49,000 --> 07:19:51,000 it's not super test from super test it's 10457 07:19:51,000 --> 07:19:54,600 import request from Super test or that's 10458 07:19:54,600 --> 07:19:56,080 how you should name it import request 10459 07:19:56,080 --> 07:19:57,558 from Super test because this is actually 10460 07:19:57,558 --> 07:20:00,040 a function so let me just I just fix 10461 07:20:00,040 --> 07:20:02,120 that real quick sorry about that so let 10462 07:20:02,120 --> 07:20:04,120 me just invoke this request function 10463 07:20:04,120 --> 07:20:06,120 which is this super test module over 10464 07:20:06,120 --> 07:20:07,920 here again this is the top level 10465 07:20:07,920 --> 07:20:10,040 function that we're importing and then 10466 07:20:10,040 --> 07:20:12,798 we're just going to pass our app 10467 07:20:12,798 --> 07:20:14,280 instance like 10468 07:20:14,280 --> 07:20:16,958 that and then what we're going to do is 10469 07:20:16,958 --> 07:20:19,320 we're going to go ahead and call the get 10470 07:20:19,320 --> 07:20:20,558 method so whatever endpoint point that 10471 07:20:20,558 --> 07:20:22,798 we want to make a request to and 10472 07:20:22,798 --> 07:20:25,478 whatever method type you would use that 10473 07:20:25,478 --> 07:20:27,638 one so for a get request I would use get 10474 07:20:27,638 --> 07:20:30,920 for post I would do post so I'll do get 10475 07:20:30,920 --> 07:20:33,160 and then the URL so 10476 07:20:33,160 --> 07:20:35,958 hello and then you want to pass in a 10477 07:20:35,958 --> 07:20:37,760 callback function if you need one but I 10478 07:20:37,760 --> 07:20:41,120 don't need one right now so after the do 10479 07:20:41,120 --> 07:20:44,478 getet I can go ahead and use uh this 10480 07:20:44,478 --> 07:20:46,478 expect method at the end so I'm chaining 10481 07:20:46,478 --> 07:20:49,000 this expect expect method on the return 10482 07:20:49,000 --> 07:20:52,638 value of get and then what I can do is I 10483 07:20:52,638 --> 07:20:55,280 can expect a status code like this I can 10484 07:20:55,280 --> 07:20:58,398 do expect 200 and now let's go ahead and 10485 07:20:58,398 --> 07:21:00,520 run the test so we're going to run the 10486 07:21:00,520 --> 07:21:03,000 test by using this test e2e command that 10487 07:21:03,000 --> 07:21:06,680 I just set up earlier so npm run test 10488 07:21:06,680 --> 07:21:12,240 colon e2e like that and our test should 10489 07:21:13,200 --> 07:21:16,558 pass there we 10490 07:21:19,240 --> 07:21:21,920 go 10491 07:21:21,920 --> 07:21:24,558 okay perfect awesome not sure why it 10492 07:21:24,558 --> 07:21:28,360 took forever it might be because we did 10493 07:21:28,360 --> 07:21:29,478 not end the 10494 07:21:29,478 --> 07:21:33,160 request but I can do at the end of the 10495 07:21:33,160 --> 07:21:36,360 expect call I can do end and then error 10496 07:21:36,360 --> 07:21:40,440 res and and do if error throw error like 10497 07:21:40,440 --> 07:21:44,000 this and that should fix that error in 10498 07:21:44,000 --> 07:21:47,280 in the last part yep there we go perfect 10499 07:21:47,280 --> 07:21:48,920 so you can see that the test passes and 10500 07:21:48,920 --> 07:21:51,840 of course if I were to change the status 10501 07:21:51,840 --> 07:21:54,798 code inside this app.get inside this 10502 07:21:54,798 --> 07:21:57,600 request Handler I change the 2011 to 10503 07:21:57,600 --> 07:21:59,240 kind of like throw off this test over 10504 07:21:59,240 --> 07:22:01,398 here and show you what happens so let's 10505 07:22:01,398 --> 07:22:04,398 do npm run test e so the test should 10506 07:22:04,398 --> 07:22:07,440 fail you'll see that the error actually 10507 07:22:07,440 --> 07:22:09,718 uh is thrown right over here the problem 10508 07:22:09,718 --> 07:22:11,920 here though is that even though we are 10509 07:22:11,920 --> 07:22:13,520 expecting 200 over here and keep in mind 10510 07:22:13,520 --> 07:22:15,718 this is the expect method that is on 10511 07:22:15,718 --> 07:22:19,240 Super Test not from just okay you can 10512 07:22:19,240 --> 07:22:21,040 see that right now over here it still 10513 07:22:21,040 --> 07:22:23,680 treats the test as passing even though 10514 07:22:23,680 --> 07:22:27,240 the test shouldn't pass and um even if I 10515 07:22:27,240 --> 07:22:30,040 were to write an assertion inside this 10516 07:22:30,040 --> 07:22:33,638 call back function for the end method 10517 07:22:33,638 --> 07:22:36,478 call so we to do expect and then we have 10518 07:22:36,478 --> 07:22:39,080 this res argument which is the response 10519 07:22:39,080 --> 07:22:42,878 so res. status code to 10520 07:22:42,878 --> 07:22:46,840 be 200 and if I run the test again the 10521 07:22:46,840 --> 07:22:49,080 test actually will still be considered 10522 07:22:49,080 --> 07:22:53,600 passing let me remove this expect as 10523 07:22:53,798 --> 07:22:56,920 well okay you can see that the test 10524 07:22:56,920 --> 07:22:59,000 actually is still considered passing 10525 07:22:59,000 --> 07:23:00,718 right over here even though it actually 10526 07:23:00,718 --> 07:23:03,360 is supposed to fail now to fix this 10527 07:23:03,360 --> 07:23:06,600 instead of having to do uh do end like 10528 07:23:06,600 --> 07:23:08,680 right over here I'm going to go ahead 10529 07:23:08,680 --> 07:23:11,718 and remove that and so instead of that 10530 07:23:11,718 --> 07:23:14,318 we're going to go ahead and actually 10531 07:23:14,318 --> 07:23:16,958 await this doget call because this 10532 07:23:16,958 --> 07:23:19,240 actually returns a promise if you look 10533 07:23:19,240 --> 07:23:21,920 at the end over here you can use then or 10534 07:23:21,920 --> 07:23:24,318 catch like that so I'm going to add the 10535 07:23:24,318 --> 07:23:25,920 ASN keyword in front of the call 10536 07:23:25,920 --> 07:23:29,240 function for the it function or test and 10537 07:23:29,240 --> 07:23:31,798 I'm going to go ahead and do const res 10538 07:23:31,798 --> 07:23:34,280 equals await so this is the response 10539 07:23:34,280 --> 07:23:36,318 object right over here you can see that 10540 07:23:36,318 --> 07:23:38,638 the return value the resolved value is a 10541 07:23:38,638 --> 07:23:40,318 response let me name it 10542 07:23:40,318 --> 07:23:43,318 response okay and now what I can do is I 10543 07:23:43,318 --> 07:23:45,200 can write an assertion on the 10544 07:23:45,200 --> 07:23:47,398 response the status 10545 07:23:47,398 --> 07:23:50,160 code and I'll say response. status let 10546 07:23:50,160 --> 07:23:52,878 go 2 B 200 so this should fail because 10547 07:23:52,878 --> 07:23:56,080 the end point actually returns a 2011 10548 07:23:56,080 --> 07:23:58,240 and now the test actually fails so this 10549 07:23:58,240 --> 07:24:01,120 is exactly what we want of course if you 10550 07:24:01,120 --> 07:24:04,080 wanted to write an assertion on the 10551 07:24:04,080 --> 07:24:06,440 response body there is this body 10552 07:24:06,440 --> 07:24:08,760 property on the response object and then 10553 07:24:08,760 --> 07:24:11,798 you can use assertions such as to b or 10554 07:24:11,798 --> 07:24:15,958 to equal or to strict equal or to 10555 07:24:15,958 --> 07:24:17,840 contain whichever mat you feel like you 10556 07:24:17,840 --> 07:24:20,600 need so for example I can 10557 07:24:20,600 --> 07:24:21,798 do 10558 07:24:21,798 --> 07:24:26,760 Tob an empty object and let's see what 10559 07:24:27,398 --> 07:24:31,920 happens uh let me first fix this back to 10560 07:24:32,840 --> 07:24:35,040 200 and of course you can see the test 10561 07:24:35,040 --> 07:24:36,398 fails because we're not returning a 10562 07:24:36,398 --> 07:24:38,558 response body so let's fix that let me 10563 07:24:38,558 --> 07:24:41,080 go ahead and set the status to 200 and 10564 07:24:41,080 --> 07:24:44,398 also send some Json and empty response 10565 07:24:44,398 --> 07:24:47,958 body and let's run the test 10566 07:24:47,958 --> 07:24:51,080 again and let's see 10567 07:24:51,080 --> 07:24:55,280 uh I think let's do two equal since it's 10568 07:24:55,280 --> 07:24:57,398 an 10569 07:24:57,398 --> 07:25:00,000 object okay there we go now it passes 10570 07:25:00,000 --> 07:25:01,760 okay it does suggest to use two strict 10571 07:25:01,760 --> 07:25:03,958 equal as well right up top over here 10572 07:25:03,958 --> 07:25:06,840 that's why it was erroring 10573 07:25:07,760 --> 07:25:11,520 out okay and if I tried to pass in the 10574 07:25:11,520 --> 07:25:16,280 wrong object like this let's run the 10575 07:25:16,280 --> 07:25:19,280 test and you can see that the assertion 10576 07:25:19,280 --> 07:25:21,638 fails so hopefully this makes sense now 10577 07:25:21,638 --> 07:25:22,638 I just want to show you this quick 10578 07:25:22,638 --> 07:25:24,600 example we're now going to go ahead and 10579 07:25:24,600 --> 07:25:27,638 actually set up our end to end tests for 10580 07:25:27,638 --> 07:25:30,080 our application so this will actually 10581 07:25:30,080 --> 07:25:31,920 require us 10582 07:25:31,920 --> 07:25:35,280 to grab our app instance right over here 10583 07:25:35,280 --> 07:25:38,040 because what we need to do is similar to 10584 07:25:38,040 --> 07:25:39,760 um what we're doing in here we need to 10585 07:25:39,760 --> 07:25:43,520 pass that app instance into the request 10586 07:25:43,520 --> 07:25:45,318 function as an argument as you can see 10587 07:25:45,318 --> 07:25:50,718 over here okay so inside our application 10588 07:25:50,718 --> 07:25:53,718 right over here inside index. MJS this 10589 07:25:53,718 --> 07:25:56,280 is where we actually have our Express 10590 07:25:56,280 --> 07:25:57,958 app created and we have all of our 10591 07:25:57,958 --> 07:26:00,520 middleware set up so the thing is though 10592 07:26:00,520 --> 07:26:04,280 we need to make sure that we can export 10593 07:26:04,280 --> 07:26:07,280 our app and import it into our test 10594 07:26:07,280 --> 07:26:08,958 files our endtoend test files so that 10595 07:26:08,958 --> 07:26:12,160 way all of the routes and all of the uh 10596 07:26:12,160 --> 07:26:15,558 middlewares and anything else is 10597 07:26:15,558 --> 07:26:18,000 actually going to be registered the 10598 07:26:18,000 --> 07:26:21,478 other issue is that because in our file 10599 07:26:21,478 --> 07:26:22,840 we're not only just registering 10600 07:26:22,840 --> 07:26:25,600 middleware but you can see right over 10601 07:26:25,600 --> 07:26:29,478 here I am trying to also invoke the 10602 07:26:29,478 --> 07:26:32,080 database as well so there are actually a 10603 07:26:32,080 --> 07:26:33,600 few things that we need to do just to 10604 07:26:33,600 --> 07:26:35,440 get this to work and so the good thing 10605 07:26:35,440 --> 07:26:37,878 is it's not going to be a big deal to do 10606 07:26:37,878 --> 07:26:40,440 this so what I'll do is I'll create a 10607 07:26:40,440 --> 07:26:44,558 new file and I'll call this uh let's see 10608 07:26:44,558 --> 07:26:48,200 create app. 10609 07:26:48,478 --> 07:26:51,398 MJS and all I'm going to do is just 10610 07:26:51,398 --> 07:26:54,318 export this 10611 07:26:54,840 --> 07:26:58,638 function create app like 10612 07:26:58,638 --> 07:27:01,520 that and I'm just going to literally 10613 07:27:01,520 --> 07:27:04,958 take let's see I'm going to take all 10614 07:27:04,958 --> 07:27:08,280 this stuff right over 10615 07:27:08,280 --> 07:27:11,240 here and I'm going to paste that in 10616 07:27:11,240 --> 07:27:15,040 here and then what I'm going to do is 10617 07:27:15,040 --> 07:27:18,718 this I'm going to go ahead 10618 07:27:18,718 --> 07:27:20,398 and 10619 07:27:20,398 --> 07:27:24,000 pass an instance of app like that so 10620 07:27:24,000 --> 07:27:26,000 then I can reference app right over here 10621 07:27:26,000 --> 07:27:27,240 and I also need to make sure I'm 10622 07:27:27,240 --> 07:27:29,280 registering all of my middleware which I 10623 07:27:29,280 --> 07:27:31,200 am and I also need to import all of the 10624 07:27:31,200 --> 07:27:33,840 middleware as well all the Imports right 10625 07:27:33,840 --> 07:27:34,920 over 10626 07:27:34,920 --> 07:27:37,440 here as well as right 10627 07:27:37,440 --> 07:27:41,478 here okay and um let me see uh 10628 07:27:41,478 --> 07:27:42,798 Additionally you know 10629 07:27:42,798 --> 07:27:44,718 what actually I'm going to create the 10630 07:27:44,718 --> 07:27:47,520 app inside the function instead and let 10631 07:27:47,520 --> 07:27:48,638 me import 10632 07:27:48,638 --> 07:27:51,760 Express up over here so let me delete 10633 07:27:51,760 --> 07:27:53,638 all of these Imports in the index files 10634 07:27:53,638 --> 07:27:56,318 because we won't need them 10635 07:27:56,318 --> 07:27:59,558 anymore okay so what I can do now is I 10636 07:27:59,558 --> 07:28:02,200 can call create app and I want to 10637 07:28:02,200 --> 07:28:05,000 actually return the app instance so when 10638 07:28:05,000 --> 07:28:06,840 I call create app it'll run through all 10639 07:28:06,840 --> 07:28:09,120 of these it it'll call all of these 10640 07:28:09,120 --> 07:28:10,760 functions it's going to register all of 10641 07:28:10,760 --> 07:28:12,878 the middlewares right over here all of 10642 07:28:12,878 --> 07:28:15,440 the routes and it's going to uh return 10643 07:28:15,440 --> 07:28:19,120 app so once all of the middlewares and 10644 07:28:19,120 --> 07:28:20,638 all the rout or registered it's going to 10645 07:28:20,638 --> 07:28:22,360 return this app instance to wherever we 10646 07:28:22,360 --> 07:28:24,680 called it so now what I can do is inside 10647 07:28:24,680 --> 07:28:28,040 the index. MJS file I can go ahead and 10648 07:28:28,040 --> 07:28:30,160 first let me 10649 07:28:30,160 --> 07:28:34,718 import create app like that and then I'm 10650 07:28:34,718 --> 07:28:37,440 going to go ahead and do const app 10651 07:28:37,440 --> 07:28:40,318 equals create 10652 07:28:40,398 --> 07:28:43,200 app and we don't have any 10653 07:28:43,200 --> 07:28:45,520 asynchronous a logic going on here so 10654 07:28:45,520 --> 07:28:47,638 that's good so we can just call create 10655 07:28:47,638 --> 07:28:51,958 app and now the next thing is is we need 10656 07:28:51,958 --> 07:28:54,840 to also make it so that when we run our 10657 07:28:54,840 --> 07:28:57,040 end to end tests or integration tests 10658 07:28:57,040 --> 07:28:58,520 that we're also able to connect to the 10659 07:28:58,520 --> 07:29:01,240 database as well so this alone is just 10660 07:29:01,240 --> 07:29:05,240 fine for our development application 10661 07:29:05,240 --> 07:29:07,600 like if I were to just run my code right 10662 07:29:07,600 --> 07:29:10,478 now for just 10663 07:29:10,478 --> 07:29:12,360 development everything would still work 10664 07:29:12,360 --> 07:29:13,600 the same way you can see that still 10665 07:29:13,600 --> 07:29:16,840 going to uh reference the app and then 10666 07:29:16,840 --> 07:29:18,920 call app. listen to actually start up 10667 07:29:18,920 --> 07:29:21,760 the server is not defined I think 10668 07:29:21,760 --> 07:29:24,398 oh I think let me see where where was 10669 07:29:24,398 --> 07:29:25,798 that over 10670 07:29:25,798 --> 07:29:29,080 here um did I reference that in here oh 10671 07:29:29,080 --> 07:29:30,760 yeah let me import Mongoose in here as 10672 07:29:30,760 --> 07:29:33,760 well inside the create app MJS method 10673 07:29:33,760 --> 07:29:35,318 because we do reference it down over 10674 07:29:35,318 --> 07:29:38,760 here forgot about that and then cannot 10675 07:29:38,760 --> 07:29:41,160 in a client please provide correct 10676 07:29:41,160 --> 07:29:43,520 options okay so the other problem here 10677 07:29:43,520 --> 07:29:45,878 is that it does require the 10678 07:29:45,878 --> 07:29:48,240 connection so what I can do is I can 10679 07:29:48,240 --> 07:29:51,360 move the connection top over here 10680 07:29:51,360 --> 07:29:53,280 and now it works okay so it's just an 10681 07:29:53,280 --> 07:29:55,200 it's just a matter of ordering things so 10682 07:29:55,200 --> 07:29:56,878 first I'm connecting to the mongodb 10683 07:29:56,878 --> 07:29:58,920 database I'm calling mango. connect then 10684 07:29:58,920 --> 07:30:01,000 I am initializing the app because the 10685 07:30:01,000 --> 07:30:02,718 application does require the connection 10686 07:30:02,718 --> 07:30:06,478 to exist first so now what I can do is 10687 07:30:06,478 --> 07:30:07,360 let me 10688 07:30:07,360 --> 07:30:12,000 remove uh this app.get let me remove all 10689 07:30:12,000 --> 07:30:14,280 of this for now and instead of just 10690 07:30:14,280 --> 07:30:16,240 calling Express I'm going to remove that 10691 07:30:16,240 --> 07:30:19,558 let me remove this import of Express I'm 10692 07:30:19,558 --> 07:30:21,318 going to go ahead and import the create 10693 07:30:21,318 --> 07:30:23,040 app like 10694 07:30:23,040 --> 07:30:25,840 this okay so now we actually have our 10695 07:30:25,840 --> 07:30:28,600 Express app in this variable like our 10696 07:30:28,600 --> 07:30:30,200 actual app not the fake one that we just 10697 07:30:30,200 --> 07:30:32,840 created just now but we still need a 10698 07:30:32,840 --> 07:30:35,160 connection to the database and that's 10699 07:30:35,160 --> 07:30:36,878 okay because what I can do is I can 10700 07:30:36,878 --> 07:30:39,318 actually just copy all this stuff over 10701 07:30:39,318 --> 07:30:41,558 here and I can just paste it right in 10702 07:30:41,558 --> 07:30:46,000 here and I can just import mongus from 10703 07:30:46,000 --> 07:30:49,558 mongus and I can change the database 10704 07:30:49,558 --> 07:30:51,558 that it is connecting to so I can change 10705 07:30:51,558 --> 07:30:53,280 it from Express tutorial to express 10706 07:30:53,280 --> 07:30:56,318 tutorial test like that and I can say 10707 07:30:56,318 --> 07:30:58,120 connected to test 10708 07:30:58,120 --> 07:31:01,520 database and now let's go ahead and try 10709 07:31:01,520 --> 07:31:06,080 to test a very simple endpoint so I have 10710 07:31:06,080 --> 07:31:07,680 let's 10711 07:31:07,680 --> 07:31:10,280 see do I have any simple end points that 10712 07:31:10,280 --> 07:31:11,638 I can set 10713 07:31:11,638 --> 07:31:13,600 up 10714 07:31:13,600 --> 07:31:17,280 um let's do this one let's do the API 10715 07:31:17,280 --> 07:31:19,798 off/ status endpoint let me remove all 10716 07:31:19,798 --> 07:31:22,360 these logs so what this should do is 10717 07:31:22,360 --> 07:31:23,760 we're going to write an assertion to 10718 07:31:23,760 --> 07:31:25,000 return a 401 because we're not 10719 07:31:25,000 --> 07:31:28,600 authenticated currently so inside our 10720 07:31:28,600 --> 07:31:31,520 test file let's go ahead and do 10721 07:31:31,520 --> 07:31:34,840 describe API 10722 07:31:34,840 --> 07:31:38,040 off and then what I'll do is say it 10723 07:31:38,040 --> 07:31:42,958 should return 401 when not logged 10724 07:31:42,958 --> 07:31:45,440 in and let me add the Asing keyword in 10725 07:31:45,440 --> 07:31:47,160 front of the call 10726 07:31:47,160 --> 07:31:49,958 function for our test and I'm going to 10727 07:31:49,958 --> 07:31:53,160 do const response equals await so I'm 10728 07:31:53,160 --> 07:31:54,638 going to call the request function 10729 07:31:54,638 --> 07:31:57,440 that's super test right over here we 10730 07:31:57,440 --> 07:31:59,360 going to pass in our 10731 07:31:59,360 --> 07:32:02,440 app and then we need to go ahead and 10732 07:32:02,440 --> 07:32:03,638 call 10733 07:32:03,638 --> 07:32:06,080 get and then go ahead and pass in the 10734 07:32:06,080 --> 07:32:09,318 URL that we want to visit so/ 10735 07:32:09,318 --> 07:32:13,000 API SL status just like 10736 07:32:13,000 --> 07:32:15,440 that and then now I can write an 10737 07:32:15,440 --> 07:32:17,398 assertion on the 10738 07:32:17,398 --> 07:32:20,040 response do status code 10739 07:32:20,040 --> 07:32:24,840 to be 401 so let's actually run our test 10740 07:32:24,840 --> 07:32:28,478 and see what happens so npm run test or 10741 07:32:28,478 --> 07:32:32,360 I'm sorry npm run test 10742 07:32:32,958 --> 07:32:35,080 e2e okay and you can 10743 07:32:35,080 --> 07:32:37,520 see uh let's see right over here you can 10744 07:32:37,520 --> 07:32:40,680 see that it is logging in the console 10745 07:32:40,680 --> 07:32:42,478 right over here it says connected to 10746 07:32:42,478 --> 07:32:45,280 test database and our tests are passing 10747 07:32:45,280 --> 07:32:47,160 so that's good so it is actually calling 10748 07:32:47,160 --> 07:32:50,120 the endpoint um and it passes because we 10749 07:32:50,120 --> 07:32:52,680 are expecting the status code of 401 10750 07:32:52,680 --> 07:32:54,558 which it is actually giving it to us and 10751 07:32:54,558 --> 07:32:57,680 of course we get this issue where um the 10752 07:32:57,680 --> 07:33:01,000 test did not exit after the test 10753 07:33:01,000 --> 07:33:04,520 completed so I think it's likely because 10754 07:33:04,520 --> 07:33:06,798 of our database connection right over 10755 07:33:06,798 --> 07:33:08,878 here mongus doc connect let me actually 10756 07:33:08,878 --> 07:33:11,638 do this I'm going to copy this and I'm 10757 07:33:11,638 --> 07:33:13,878 going to go inside the describe block 10758 07:33:13,878 --> 07:33:15,718 and then right before the 10759 07:33:15,718 --> 07:33:18,000 test I'm going to go ahead and use this 10760 07:33:18,000 --> 07:33:21,080 before all function this is a life cycle 10761 07:33:21,080 --> 07:33:24,040 hook so basically this function takes on 10762 07:33:24,040 --> 07:33:27,240 callback function and before all of your 10763 07:33:27,240 --> 07:33:29,760 tests that's why it's called before all 10764 07:33:29,760 --> 07:33:32,360 it will run whatever is inside this 10765 07:33:32,360 --> 07:33:34,958 logic over here you can see before all 10766 07:33:34,958 --> 07:33:36,680 of our test it's going to connect to the 10767 07:33:36,680 --> 07:33:38,840 database so there's before all there's 10768 07:33:38,840 --> 07:33:41,440 also before each before each will run 10769 07:33:41,440 --> 07:33:43,200 before every test and we don't want that 10770 07:33:43,200 --> 07:33:44,520 because we only want to connect to the 10771 07:33:44,520 --> 07:33:47,478 database one time and then let me just 10772 07:33:47,478 --> 07:33:49,718 make sure this still works 10773 07:33:49,718 --> 07:33:51,958 as 10774 07:33:51,958 --> 07:33:56,000 expected uh let's see um okay so I think 10775 07:33:56,000 --> 07:33:58,080 the problem is oh wait 10776 07:33:58,080 --> 07:34:01,200 whoops let me do this uh it does require 10777 07:34:01,200 --> 07:34:03,080 a little bit of tinkering but what I 10778 07:34:03,080 --> 07:34:04,718 need to do is because we're trying to 10779 07:34:04,718 --> 07:34:06,398 create the app first before the database 10780 07:34:06,398 --> 07:34:09,920 connection so let me actually do this 10781 07:34:09,920 --> 07:34:12,318 let me declare variable up here called 10782 07:34:12,318 --> 07:34:15,360 app like that using the let keyword and 10783 07:34:15,360 --> 07:34:17,160 I'm going to 10784 07:34:17,160 --> 07:34:19,318 reassign uh the value of what whatever 10785 07:34:19,318 --> 07:34:22,558 create app returns to app and so that 10786 07:34:22,558 --> 07:34:25,520 way I can reference it inside here okay 10787 07:34:25,520 --> 07:34:26,600 there's a bunch of different ways you 10788 07:34:26,600 --> 07:34:29,200 can set this up so this is not only one 10789 07:34:29,200 --> 07:34:30,600 way this is not the only way to do it 10790 07:34:30,600 --> 07:34:31,840 there are different ways of how you can 10791 07:34:31,840 --> 07:34:35,638 configure this but this should 10792 07:34:35,638 --> 07:34:38,440 work okay perfect and now to fix this 10793 07:34:38,440 --> 07:34:41,120 part we're going to go at the end of our 10794 07:34:41,120 --> 07:34:43,240 test I'm going to go ahead and use this 10795 07:34:43,240 --> 07:34:44,520 after all 10796 07:34:44,520 --> 07:34:46,680 hook okay so it's pretty much the 10797 07:34:46,680 --> 07:34:48,680 opposite of before all so after all your 10798 07:34:48,680 --> 07:34:49,920 tests 10799 07:34:49,920 --> 07:34:52,240 what we want to do and this is the part 10800 07:34:52,240 --> 07:34:54,120 where I mentioned earlier or maybe in 10801 07:34:54,120 --> 07:34:56,478 the previous section of this tutorial 10802 07:34:56,478 --> 07:34:58,920 where you want to actually drop the 10803 07:34:58,920 --> 07:35:01,080 database as well as close the connection 10804 07:35:01,080 --> 07:35:03,600 to the database so you can do that by 10805 07:35:03,600 --> 07:35:06,680 referencing doc connection drop 10806 07:35:06,680 --> 07:35:08,600 database um and then you can see it says 10807 07:35:08,600 --> 07:35:10,200 helper for drop database deletes the 10808 07:35:10,200 --> 07:35:12,600 given database including all collections 10809 07:35:12,600 --> 07:35:15,040 documents and indexes so that will drop 10810 07:35:15,040 --> 07:35:16,520 the database that we are connected to 10811 07:35:16,520 --> 07:35:18,718 which is the test database and this 10812 07:35:18,718 --> 07:35:20,920 returns a promise so we will need to 10813 07:35:20,920 --> 07:35:23,360 await that so let's add the Asing 10814 07:35:23,360 --> 07:35:25,878 keyword and await this and after we drop 10815 07:35:25,878 --> 07:35:28,878 the database let's go ahead 10816 07:35:28,878 --> 07:35:32,360 and close the connection yeah I think 10817 07:35:32,360 --> 07:35:36,840 it's yep drop not drop sorry 10818 07:35:36,840 --> 07:35:39,160 close and then this will close the 10819 07:35:39,160 --> 07:35:42,520 connection right over here and then this 10820 07:35:42,520 --> 07:35:46,200 should fix that uh warning that we have 10821 07:35:46,200 --> 07:35:47,000 over 10822 07:35:47,000 --> 07:35:49,520 here okay there you go you see how 10823 07:35:49,520 --> 07:35:52,160 now the tests actually gracefully exit 10824 07:35:52,160 --> 07:35:54,440 and this is important because if you 10825 07:35:54,440 --> 07:35:56,478 have your end to endend test or really 10826 07:35:56,478 --> 07:35:58,958 any test running in a pipeline it's 10827 07:35:58,958 --> 07:36:00,798 going to block your pipeline from 10828 07:36:00,798 --> 07:36:03,000 proceeding to the next job so it's very 10829 07:36:03,000 --> 07:36:05,200 important that you resolve these 10830 07:36:05,200 --> 07:36:07,080 situations okay but we have a pretty 10831 07:36:07,080 --> 07:36:09,398 good setup right now so let's go ahead 10832 07:36:09,398 --> 07:36:11,398 and actually continue so what I'm going 10833 07:36:11,398 --> 07:36:13,878 to do is I'm going to go ahead and write 10834 07:36:13,878 --> 07:36:16,240 an end to end test I'm going to I'm 10835 07:36:16,240 --> 07:36:18,240 going to write a scenario okay so what 10836 07:36:18,240 --> 07:36:20,920 I'm going to do is I'm going to go ahead 10837 07:36:20,920 --> 07:36:23,878 and create a user first verify that the 10838 07:36:23,878 --> 07:36:26,958 user is created and then we're going to 10839 07:36:26,958 --> 07:36:28,600 try to log in and then verify that we're 10840 07:36:28,600 --> 07:36:30,360 logged in I know it's a lot but don't 10841 07:36:30,360 --> 07:36:32,840 worry we'll take it step by step ideally 10842 07:36:32,840 --> 07:36:34,600 you always want to make sure you are 10843 07:36:34,600 --> 07:36:37,318 separating your tests you don't want to 10844 07:36:37,318 --> 07:36:40,360 like have them all together so what I 10845 07:36:40,360 --> 07:36:43,478 can do is I will go ahead and create a 10846 07:36:43,478 --> 07:36:48,360 new file I'll call this user dope. JS or 10847 07:36:48,360 --> 07:36:49,360 really whatever ever it is that you want 10848 07:36:49,360 --> 07:36:50,680 to describe it 10849 07:36:50,680 --> 07:36:53,440 as and we can just literally copy this 10850 07:36:53,440 --> 07:36:55,080 whole thing and paste it 10851 07:36:55,080 --> 07:36:59,080 here so I'll just call this uh create 10852 07:36:59,080 --> 07:37:00,920 user and 10853 07:37:00,920 --> 07:37:03,440 login and just have the same exact setup 10854 07:37:03,440 --> 07:37:06,520 like this um let's see yep everything 10855 07:37:06,520 --> 07:37:08,760 else is good and then let's 10856 07:37:08,760 --> 07:37:11,040 just delete that because we're our test 10857 07:37:11,040 --> 07:37:13,360 is going to be different okay so the 10858 07:37:13,360 --> 07:37:14,760 first thing that I want to do is I want 10859 07:37:14,760 --> 07:37:17,840 to actually create the user so let me go 10860 07:37:17,840 --> 07:37:22,360 ahead and do this it should create the 10861 07:37:22,360 --> 07:37:24,240 user and keep in mind that we're not 10862 07:37:24,240 --> 07:37:26,040 going to be testing everything in one 10863 07:37:26,040 --> 07:37:28,318 single test block we're going to be 10864 07:37:28,318 --> 07:37:31,318 doing everything in sequential order so 10865 07:37:31,318 --> 07:37:33,398 first thing is we're going to test that 10866 07:37:33,398 --> 07:37:36,318 the user is created so it should create 10867 07:37:36,318 --> 07:37:37,360 the 10868 07:37:37,360 --> 07:37:41,840 user and we're going to go ahead 10869 07:37:41,840 --> 07:37:44,878 and make a request so let's call the 10870 07:37:44,878 --> 07:37:47,718 request function pass in our app 10871 07:37:47,718 --> 07:37:49,600 instance and then we're going to go 10872 07:37:49,600 --> 07:37:52,080 ahead and call a post request this time 10873 07:37:52,080 --> 07:37:55,478 and the endpoint is going to be/ API SLO 10874 07:37:55,478 --> 07:37:58,600 SL user and let me just verify that 10875 07:37:58,600 --> 07:38:01,878 route right over here yep router. poost 10876 07:38:01,878 --> 07:38:05,200 API users or I'm sorry it's I don't know 10877 07:38:05,200 --> 07:38:09,520 why off it's API users with an S and 10878 07:38:09,520 --> 07:38:11,638 then it's going to go ahead and call 10879 07:38:11,638 --> 07:38:14,040 this middle word function this is the 10880 07:38:14,040 --> 07:38:16,120 express validator so this is where we 10881 07:38:16,120 --> 07:38:18,000 can actually also make sure that the 10882 07:38:18,000 --> 07:38:19,920 validation is working working as 10883 07:38:19,920 --> 07:38:22,398 well uh we have create user Handler is 10884 07:38:22,398 --> 07:38:25,000 called which is our request Handler and 10885 07:38:25,000 --> 07:38:26,478 it's going to basically run through all 10886 07:38:26,478 --> 07:38:29,080 this logic so again in the previous 10887 07:38:29,080 --> 07:38:30,638 section of this tutorial where we went 10888 07:38:30,638 --> 07:38:32,080 over unit testing I showed you how to 10889 07:38:32,080 --> 07:38:34,318 unit test this whole function this time 10890 07:38:34,318 --> 07:38:35,600 we don't have to worry about mocking 10891 07:38:35,600 --> 07:38:36,718 anything it's going to call this 10892 07:38:36,718 --> 07:38:39,120 function and it's going to run the 10893 07:38:39,120 --> 07:38:40,958 actual logic it's going to call the 10894 07:38:40,958 --> 07:38:44,878 actual functions from Express validator 10895 07:38:44,878 --> 07:38:46,718 it's going to call our own hash password 10896 07:38:46,718 --> 07:38:48,440 function it's going to create the user 10897 07:38:48,440 --> 07:38:49,920 and going to actually save it to the 10898 07:38:49,920 --> 07:38:53,360 database using okay so let's go 10899 07:38:53,360 --> 07:38:55,000 ahead 10900 07:38:55,000 --> 07:38:59,558 and go back to our test 10901 07:39:00,120 --> 07:39:03,120 file oops sorry about that all right so 10902 07:39:03,120 --> 07:39:05,120 we're going to make a post request and 10903 07:39:05,120 --> 07:39:07,920 let's go ahead and send a request body 10904 07:39:07,920 --> 07:39:10,240 so uh to send the request body you can 10905 07:39:10,240 --> 07:39:12,798 use this send method like that whoops 10906 07:39:12,798 --> 07:39:17,040 not sure why that is send yep and then 10907 07:39:17,040 --> 07:39:18,878 you can pass in an object so so we need 10908 07:39:18,878 --> 07:39:21,398 to send the username so for the username 10909 07:39:21,398 --> 07:39:25,318 I'll do Adam 123 10910 07:39:25,318 --> 07:39:28,160 password let's do password and then 10911 07:39:28,160 --> 07:39:32,798 display name just do Adam the developer 10912 07:39:32,798 --> 07:39:34,958 okay so when we send this request it's 10913 07:39:34,958 --> 07:39:37,680 going to handle it on the server side we 10914 07:39:37,680 --> 07:39:39,318 don't obviously care about what's going 10915 07:39:39,318 --> 07:39:40,958 on on the server side with this because 10916 07:39:40,958 --> 07:39:42,798 we're trying to just care about what the 10917 07:39:42,798 --> 07:39:45,958 response is okay so what I'm going to 10918 07:39:45,958 --> 07:39:49,958 do is after this whole thing is done we 10919 07:39:49,958 --> 07:39:51,398 also want to await this call because 10920 07:39:51,398 --> 07:39:53,360 remember this do send method also 10921 07:39:53,360 --> 07:39:56,680 returns a promise as well so let's await 10922 07:39:56,680 --> 07:40:00,558 that and now let's write an assertion 10923 07:40:00,558 --> 07:40:03,200 let's do response. status 10924 07:40:03,200 --> 07:40:09,080 code to be 2011 okay so let's run our 10925 07:40:09,080 --> 07:40:12,318 test so let's do npm run test 10926 07:40:12,318 --> 07:40:14,878 e2e so this should run all of our tests 10927 07:40:14,878 --> 07:40:17,680 for us and you can see both tests pass 10928 07:40:17,680 --> 07:40:19,718 and you can see in the cont conso that 10929 07:40:19,718 --> 07:40:21,280 that's I think that's the salt from the 10930 07:40:21,280 --> 07:40:22,680 hash password function that's being 10931 07:40:22,680 --> 07:40:26,520 logged if I am correct yep it's the salt 10932 07:40:26,520 --> 07:40:28,840 let me remove this console log but you 10933 07:40:28,840 --> 07:40:31,520 can see that both tests are passing so 10934 07:40:31,520 --> 07:40:32,520 that's good and you can see that they 10935 07:40:32,520 --> 07:40:34,360 don't conflict with each other and one 10936 07:40:34,360 --> 07:40:36,040 more thing that I also want to show you 10937 07:40:36,040 --> 07:40:39,080 is if I open up my 10938 07:40:39,080 --> 07:40:41,318 mongodb compass 10939 07:40:41,318 --> 07:40:45,200 tool okay and if I were to 10940 07:40:45,200 --> 07:40:48,680 actually let me see not this one but it 10941 07:40:48,680 --> 07:40:49,680 shouldn't be connecting yeah it should 10942 07:40:49,680 --> 07:40:51,600 not connect to this Express tutorial 10943 07:40:51,600 --> 07:40:53,718 database it should connect to the test 10944 07:40:53,718 --> 07:40:56,680 database so you know what let me remove 10945 07:40:56,680 --> 07:40:58,160 this line over 10946 07:40:58,160 --> 07:41:01,558 here and show you what happens when I 10947 07:41:01,558 --> 07:41:03,360 run the 10948 07:41:03,360 --> 07:41:06,120 test okay so now watch this if I refresh 10949 07:41:06,120 --> 07:41:08,840 the page or refresh this 10950 07:41:08,840 --> 07:41:10,878 application uh what is this new version 10951 07:41:10,878 --> 07:41:13,360 available let me click X you can see 10952 07:41:13,360 --> 07:41:16,160 that now this Express tutorial test 10953 07:41:16,160 --> 07:41:17,680 comes up and you can see that our 10954 07:41:17,680 --> 07:41:18,958 collection 10955 07:41:18,958 --> 07:41:20,558 our users collection is inside the 10956 07:41:20,558 --> 07:41:23,440 express tutorial test database and so is 10957 07:41:23,440 --> 07:41:25,718 our sessions okay so hopefully this 10958 07:41:25,718 --> 07:41:27,478 makes 10959 07:41:27,478 --> 07:41:29,600 sense but the reason why we didn't see 10960 07:41:29,600 --> 07:41:30,798 it before is because we dropped the 10961 07:41:30,798 --> 07:41:32,478 database and the reason why we dropped 10962 07:41:32,478 --> 07:41:33,840 the database is because we don't want 10963 07:41:33,840 --> 07:41:38,440 the test data from previous test Suites 10964 07:41:38,440 --> 07:41:41,600 to conflict with other tests okay I'll 10965 07:41:41,600 --> 07:41:43,878 explain more about that later on because 10966 07:41:43,878 --> 07:41:46,200 right now you might wonder okay well we 10967 07:41:46,200 --> 07:41:48,440 do need the data with this user in the 10968 07:41:48,440 --> 07:41:50,360 next test but yes that's within the same 10969 07:41:50,360 --> 07:41:53,000 test Suite in other test sues like in 10970 07:41:53,000 --> 07:41:55,280 other scenarios you don't want to have 10971 07:41:55,280 --> 07:41:56,638 leftover data in the database 10972 07:41:56,638 --> 07:41:58,520 conflicting with that scenario because 10973 07:41:58,520 --> 07:42:00,718 you might have a scenario where you're 10974 07:42:00,718 --> 07:42:02,600 only expecting one user but there might 10975 07:42:02,600 --> 07:42:04,318 be three users because you didn't clear 10976 07:42:04,318 --> 07:42:06,398 the database okay all of those tests 10977 07:42:06,398 --> 07:42:08,398 that you ran kept creating a new user 10978 07:42:08,398 --> 07:42:10,360 and because you didn't drop the database 10979 07:42:10,360 --> 07:42:13,040 the users kept adding on into the 10980 07:42:13,040 --> 07:42:15,000 database collection and your assertions 10981 07:42:15,000 --> 07:42:16,040 are going to 10982 07:42:16,040 --> 07:42:18,318 fail so let's go ahead and continue 10983 07:42:18,318 --> 07:42:20,558 Contin so after we create the user let's 10984 07:42:20,558 --> 07:42:25,280 actually log in so what we'll do is this 10985 07:42:25,280 --> 07:42:29,120 I'm going to first manually drop my 10986 07:42:29,120 --> 07:42:31,680 database let me just do that because it 10987 07:42:31,680 --> 07:42:35,478 will error out if it creates a 10988 07:42:36,318 --> 07:42:38,000 duplicate okay so we dropped the 10989 07:42:38,000 --> 07:42:41,360 database good let's go ahead and go back 10990 07:42:41,360 --> 07:42:45,878 to our code so now we'll do this it 10991 07:42:45,878 --> 07:42:50,280 should log the user in 10992 07:42:51,398 --> 07:42:53,638 and we're going to make a request a post 10993 07:42:53,638 --> 07:42:57,718 request this time to SL API 10994 07:42:57,718 --> 07:43:01,558 SLO yep I think it's just slpi SLO and 10995 07:43:01,558 --> 07:43:03,680 then we're going to send our whoops I 10996 07:43:03,680 --> 07:43:05,240 don't know why it keeps doing set 10997 07:43:05,240 --> 07:43:08,558 encoding send username so the same 10998 07:43:08,558 --> 07:43:11,040 username that we configured up top over 10999 07:43:11,040 --> 07:43:15,478 here so Adam 123 and then the same 11000 07:43:15,760 --> 07:43:18,440 password and then let's go ahead and get 11001 07:43:18,440 --> 07:43:22,638 the response in a variable okay so let's 11002 07:43:22,638 --> 07:43:26,280 just go back to our 11003 07:43:26,280 --> 07:43:29,240 off I think I put that right over here 11004 07:43:29,240 --> 07:43:31,840 yep so it should send back a status of 11005 07:43:31,840 --> 07:43:35,558 200 on success okay uh I think with 11006 07:43:35,558 --> 07:43:37,360 passport let me just double check 11007 07:43:37,360 --> 07:43:39,398 everything yep I think everything here 11008 07:43:39,398 --> 07:43:44,200 is good so let's continue so expect 11009 07:43:44,200 --> 07:43:50,200 response status code to be 200 11010 07:43:50,360 --> 07:43:52,958 now let's go ahead and run our test make 11011 07:43:52,958 --> 07:43:55,200 sure that it 11012 07:43:55,200 --> 07:43:58,478 passes okay so now it says uh let's see 11013 07:43:58,478 --> 07:44:02,000 now it failed and let's see what's going 11014 07:44:02,000 --> 07:44:05,398 on over here it says received 500 so it 11015 07:44:05,398 --> 07:44:07,798 seems like the backend uh there might be 11016 07:44:07,798 --> 07:44:09,000 something wrong with the back end for it 11017 07:44:09,000 --> 07:44:10,798 to send back a 11018 07:44:10,798 --> 07:44:14,718 500 so I'll actually just console log 11019 07:44:14,718 --> 07:44:17,798 the response body and see what that 11020 07:44:17,798 --> 07:44:18,718 message 11021 07:44:18,718 --> 07:44:21,200 says because it sent back a status code 11022 07:44:21,200 --> 07:44:24,680 of 500 so that's obviously not 11023 07:44:24,680 --> 07:44:27,558 good let's see okay so it 11024 07:44:27,558 --> 07:44:31,558 says um maybe not response. body but 11025 07:44:31,558 --> 07:44:34,638 maybe let's just do 11026 07:44:39,318 --> 07:44:43,120 response okay so it says right over here 11027 07:44:43,120 --> 07:44:46,040 that unknown authentication strategy 11028 07:44:46,040 --> 07:44:47,840 okay so I know what the problem is the 11029 07:44:47,840 --> 07:44:50,120 problem is that it doesn't recognize our 11030 07:44:50,120 --> 07:44:53,878 local strategy and I think it's likely 11031 07:44:53,878 --> 07:44:56,398 because of the way that we moved 11032 07:44:56,398 --> 07:44:58,680 everything into 11033 07:44:58,680 --> 07:45:01,360 here so I'm trying to think what would 11034 07:45:01,360 --> 07:45:03,878 be the easiest way to do this because if 11035 07:45:03,878 --> 07:45:05,798 I were to actually remove this create 11036 07:45:05,798 --> 07:45:07,680 app and then move everything back into 11037 07:45:07,680 --> 07:45:10,240 the index file I would need to export 11038 07:45:10,240 --> 07:45:11,920 the app and that would require me to 11039 07:45:11,920 --> 07:45:14,958 import the index 11040 07:45:14,958 --> 07:45:18,558 file which would also call this mongus 11041 07:45:18,558 --> 07:45:20,798 doc connect code and I don't want that 11042 07:45:20,798 --> 07:45:22,600 to happen so the only other option is I 11043 07:45:22,600 --> 07:45:25,000 would have to essentially move this to a 11044 07:45:25,000 --> 07:45:26,520 separate file and then it would also 11045 07:45:26,520 --> 07:45:28,558 call this app I listen which I don't 11046 07:45:28,558 --> 07:45:29,760 want so we don't want to import this 11047 07:45:29,760 --> 07:45:33,440 index file at all so I think the other 11048 07:45:33,440 --> 07:45:35,000 thing that I might need to do is I might 11049 07:45:35,000 --> 07:45:38,160 need to import this strategy into the 11050 07:45:38,160 --> 07:45:39,718 create app 11051 07:45:39,718 --> 07:45:43,000 probably maybe like right over here and 11052 07:45:43,000 --> 07:45:46,280 I think maybe that might fix that so 11053 07:45:46,280 --> 07:45:48,878 let's try to run the test again and see 11054 07:45:48,878 --> 07:45:52,240 if that same issue occurs okay so you 11055 07:45:52,240 --> 07:45:54,160 can see that the error goes away now so 11056 07:45:54,160 --> 07:45:56,000 it it seemed like this import did fix 11057 07:45:56,000 --> 07:45:59,600 that I guess because we had it over here 11058 07:45:59,600 --> 07:46:01,200 uh was what was causing the problem let 11059 07:46:01,200 --> 07:46:05,000 me move this as well over over to here 11060 07:46:05,000 --> 07:46:09,520 okay um I guess it has to be inside the 11061 07:46:09,520 --> 07:46:11,798 scope since I guess this is where we are 11062 07:46:11,798 --> 07:46:13,120 creating the 11063 07:46:13,120 --> 07:46:15,920 app and we are initializing passport 11064 07:46:15,920 --> 07:46:17,240 here so maybe that's the reason why I'm 11065 07:46:17,240 --> 07:46:18,958 not too sure 11066 07:46:18,958 --> 07:46:21,240 but at least we fixed that part so now 11067 07:46:21,240 --> 07:46:23,440 we can go back over here and let me 11068 07:46:23,440 --> 07:46:25,478 remove this console log and let me just 11069 07:46:25,478 --> 07:46:28,398 rerun the test we know that it's passing 11070 07:46:28,398 --> 07:46:30,958 though okay and you can even see in the 11071 07:46:30,958 --> 07:46:32,920 console log from the actual source code 11072 07:46:32,920 --> 07:46:35,280 right over here it is logging the user 11073 07:46:35,280 --> 07:46:40,318 over here and if we go 11074 07:46:40,958 --> 07:46:43,638 to where is the local 11075 07:46:43,638 --> 07:46:46,600 strategy right here yep you can see that 11076 07:46:46,600 --> 07:46:48,240 it should be logged I think yeah it's 11077 07:46:48,240 --> 07:46:49,680 being logged right over here inside the 11078 07:46:49,680 --> 07:46:51,798 serialized user function and it says 11079 07:46:51,798 --> 07:46:53,558 inside serialized user and it logs the 11080 07:46:53,558 --> 07:46:58,520 user itself and then after successful 11081 07:46:58,798 --> 07:47:01,160 authentication the endpoint right over 11082 07:47:01,160 --> 07:47:03,760 here sends back a status code of 200 11083 07:47:03,760 --> 07:47:06,280 okay so we verified that we were able to 11084 07:47:06,280 --> 07:47:09,280 successfully log in now additionally 11085 07:47:09,280 --> 07:47:11,478 what you can do is you can write some 11086 07:47:11,478 --> 07:47:15,718 more assertions on the response object 11087 07:47:15,718 --> 07:47:18,160 they have this property called I think 11088 07:47:18,160 --> 07:47:20,958 it's headers yep headers and what you 11089 07:47:20,958 --> 07:47:22,958 can do is you can check to see if the 11090 07:47:22,958 --> 07:47:25,520 headers actually has the cookie that you 11091 07:47:25,520 --> 07:47:27,318 expect because this response that 11092 07:47:27,318 --> 07:47:29,798 headers object is actually just an 11093 07:47:29,798 --> 07:47:32,680 object that has the cookies property so 11094 07:47:32,680 --> 07:47:34,000 let me show you real quick what that 11095 07:47:34,000 --> 07:47:36,240 looks 11096 07:47:37,120 --> 07:47:40,798 like right over here yep you can see 11097 07:47:40,798 --> 07:47:42,878 that we have this set cookie property 11098 07:47:42,878 --> 07:47:45,318 which seems to be an array so I guess 11099 07:47:45,318 --> 07:47:47,280 what you could do is you can look to see 11100 07:47:47,280 --> 07:47:50,440 if the array contains this uh cookie 11101 07:47:50,440 --> 07:47:52,440 name right over here so that's one thing 11102 07:47:52,440 --> 07:47:55,200 that you could do but I'll let you all 11103 07:47:55,200 --> 07:47:57,318 take care of that so now inside this 11104 07:47:57,318 --> 07:48:00,318 second test we are trying to log the 11105 07:48:00,318 --> 07:48:02,798 user in so now once we've logged the 11106 07:48:02,798 --> 07:48:05,680 user in then we should have a session 11107 07:48:05,680 --> 07:48:08,600 and we should be able to visit the o/ 11108 07:48:08,600 --> 07:48:11,440 status endpoint and get a response back 11109 07:48:11,440 --> 07:48:13,318 with our user record cuz that's who we 11110 07:48:13,318 --> 07:48:15,398 are logged in as so let's go ahead and 11111 07:48:15,398 --> 07:48:17,558 try that out so I'll sa for the test 11112 07:48:17,558 --> 07:48:24,360 it's should visit API status and return 11113 07:48:24,360 --> 07:48:28,558 logged in user let me just call this 11114 07:48:28,558 --> 07:48:30,398 authenticate 11115 07:48:30,398 --> 07:48:33,240 authenticated 11116 07:48:33,240 --> 07:48:35,558 user and I want to show you what happens 11117 07:48:35,558 --> 07:48:38,798 when we try to do this so we're going to 11118 07:48:38,798 --> 07:48:42,798 make a get request this time to / 11119 07:48:42,798 --> 07:48:45,440 API o 11120 07:48:45,440 --> 07:48:48,958 status and we're going to assert that 11121 07:48:48,958 --> 07:48:51,200 the status code 11122 07:48:51,200 --> 07:48:57,200 returned is 200 and let's see if this 11123 07:48:58,478 --> 07:49:00,920 passes and you'll notice now that it 11124 07:49:00,920 --> 07:49:01,920 actually 11125 07:49:01,920 --> 07:49:04,840 fails okay it gives gives us back a 11126 07:49:04,840 --> 07:49:07,558 41 and so I think the reason why this 11127 07:49:07,558 --> 07:49:09,160 happens I don't know the exact reason 11128 07:49:09,160 --> 07:49:10,478 but I will tell you what I think because 11129 07:49:10,478 --> 07:49:13,600 I ran into this issue myself so when we 11130 07:49:13,600 --> 07:49:16,600 run our test they are running in order 11131 07:49:16,600 --> 07:49:18,638 okay they are running this one first 11132 07:49:18,638 --> 07:49:21,200 this one first and this one first but 11133 07:49:21,200 --> 07:49:24,040 when we run this test right over here it 11134 07:49:24,040 --> 07:49:27,000 doesn't actually uh persist the cookies 11135 07:49:27,000 --> 07:49:30,840 for the next request so for example we 11136 07:49:30,840 --> 07:49:34,600 do receive cookies inside this inside 11137 07:49:34,600 --> 07:49:37,958 this API call right over here okay we do 11138 07:49:37,958 --> 07:49:40,200 have cookies inside this second test and 11139 07:49:40,200 --> 07:49:41,718 those cookies are being sent from the 11140 07:49:41,718 --> 07:49:44,360 server however in this third test when I 11141 07:49:44,360 --> 07:49:46,240 when we try to make a get request to 11142 07:49:46,240 --> 07:49:48,600 this protected endpoint we need to 11143 07:49:48,600 --> 07:49:51,200 obviously send the cookies but in the 11144 07:49:51,200 --> 07:49:52,840 context right over here we don't have 11145 07:49:52,840 --> 07:49:55,478 the cookies sent so I think because of 11146 07:49:55,478 --> 07:49:58,240 that reason uh it is giving us back that 11147 07:49:58,240 --> 07:50:00,280 401 because it's treating us as if we 11148 07:50:00,280 --> 07:50:03,398 never logged in so the way around this 11149 07:50:03,398 --> 07:50:06,398 is to actually implement this logic 11150 07:50:06,398 --> 07:50:10,000 inside just one test so I'll go into 11151 07:50:10,000 --> 07:50:13,280 this second test over here and I'll say 11152 07:50:13,280 --> 07:50:14,920 for the for the 11153 07:50:14,920 --> 07:50:19,040 title should log the user in and 11154 07:50:19,040 --> 07:50:21,240 visit 11155 07:50:21,240 --> 07:50:24,240 API 11156 07:50:24,520 --> 07:50:30,760 status and return off user okay and so 11157 07:50:30,760 --> 07:50:35,558 what we'll do is after we call this send 11158 07:50:35,558 --> 07:50:38,558 method I can actually use the then 11159 07:50:38,558 --> 07:50:40,600 method and 11160 07:50:40,600 --> 07:50:43,360 then I'll get the response of the post 11161 07:50:43,360 --> 07:50:45,798 request because remember I'm using the 11162 07:50:45,798 --> 07:50:49,318 then method to resolve the promise for 11163 07:50:49,318 --> 07:50:51,718 this request. post method and then that 11164 07:50:51,718 --> 07:50:53,318 would resolve the 11165 07:50:53,318 --> 07:50:57,280 value of whatever that post was going to 11166 07:50:57,280 --> 07:50:59,280 return okay whatever the promise was 11167 07:50:59,280 --> 07:51:03,000 going to resolve so inside this then I'm 11168 07:51:03,000 --> 07:51:04,600 going to pass in a callback function 11169 07:51:04,600 --> 07:51:07,440 which gives me access to that response 11170 07:51:07,440 --> 07:51:10,600 for that post request to the API off 11171 07:51:10,600 --> 07:51:13,958 endpoint so inside here what I can do is 11172 07:51:13,958 --> 07:51:18,200 I can actually return request. apppp so 11173 07:51:18,200 --> 07:51:20,878 I can make another request right after 11174 07:51:20,878 --> 07:51:23,200 this post request is finished and I can 11175 07:51:23,200 --> 07:51:25,040 make a get request 11176 07:51:25,040 --> 07:51:28,440 and I'm going to paste that URL over 11177 07:51:28,440 --> 07:51:30,798 here and then what I can do is I can 11178 07:51:30,798 --> 07:51:34,040 also set the cookies so I can use this 11179 07:51:34,040 --> 07:51:36,638 set function and then I want to set the 11180 07:51:36,638 --> 07:51:38,760 cookie and then we need to just pass in 11181 07:51:38,760 --> 07:51:40,240 this array of cookies which we can 11182 07:51:40,240 --> 07:51:44,638 easily grab if we reference res. headers 11183 07:51:44,638 --> 07:51:47,080 and I think the property was called set 11184 07:51:47,080 --> 07:51:49,360 hyphen cookie I 11185 07:51:49,360 --> 07:51:52,318 think or set cookies let me actually 11186 07:51:52,318 --> 07:51:55,240 just double check real 11187 07:51:55,240 --> 07:51:59,520 quick let me do 11188 07:52:00,638 --> 07:52:04,638 this let me console lock 11189 07:52:04,638 --> 07:52:07,558 this okay but then what will happen is 11190 07:52:07,558 --> 07:52:09,520 we send the cookies along with this 11191 07:52:09,520 --> 07:52:13,280 request and so this response so after we 11192 07:52:13,280 --> 07:52:16,280 await this whole thing this response up 11193 07:52:16,280 --> 07:52:18,478 top over here is the response respon for 11194 07:52:18,478 --> 07:52:21,160 the actual get request okay so then 11195 07:52:21,160 --> 07:52:22,958 right over here we can write assertions 11196 07:52:22,958 --> 07:52:26,840 on the status code on the response body 11197 07:52:26,840 --> 07:52:28,240 so let me go ahead and remove this third 11198 07:52:28,240 --> 07:52:30,318 test because we won't need that anymore 11199 07:52:30,318 --> 07:52:32,398 and let me go ahead and 11200 07:52:32,398 --> 07:52:35,878 run the test 11201 07:52:38,080 --> 07:52:42,638 again and let's see invalid value and 11202 07:52:42,638 --> 07:52:45,398 defined um let's see 11203 07:52:45,398 --> 07:52:48,160 headers it may have been set cookie 11204 07:52:48,160 --> 07:52:50,600 instead of set cookies maybe that's why 11205 07:52:50,600 --> 07:52:53,718 it's giving us back 11206 07:52:53,718 --> 07:52:56,920 undefined yep it's set cookie okay yep 11207 07:52:56,920 --> 07:52:59,958 that's fine so let's go ahead and change 11208 07:52:59,958 --> 07:53:02,240 this to set cookie so this will pass all 11209 07:53:02,240 --> 07:53:05,000 the cookies send it to the 11210 07:53:05,000 --> 07:53:07,600 server and let's go ahead and run the 11211 07:53:07,600 --> 07:53:09,920 test 11212 07:53:11,240 --> 07:53:14,558 again and now you can see that our tests 11213 07:53:14,558 --> 07:53:17,040 pass and specifically this second test 11214 07:53:17,040 --> 07:53:19,840 pass with all of our API calls and you 11215 07:53:19,840 --> 07:53:22,280 can even see on the server side it goes 11216 07:53:22,280 --> 07:53:24,798 through the serialized user function and 11217 07:53:24,798 --> 07:53:26,600 then it goes through the Der serialize 11218 07:53:26,600 --> 07:53:28,398 user function so this is invoked 11219 07:53:28,398 --> 07:53:31,200 whenever we make a next request after we 11220 07:53:31,200 --> 07:53:33,878 first log in okay so everything is 11221 07:53:33,878 --> 07:53:36,798 working as expected and if I want I can 11222 07:53:36,798 --> 07:53:39,160 write some more assertions on the 11223 07:53:39,160 --> 07:53:41,798 response body so I can do response. 11224 07:53:41,798 --> 07:53:42,920 body. 11225 07:53:42,920 --> 07:53:49,080 username to be atom 123 for the display 11226 07:53:49,080 --> 07:53:52,280 name let's do that as 11227 07:53:52,280 --> 07:53:56,840 well to be adam. developer and let's run 11228 07:53:56,840 --> 07:53:59,478 our tests and see what 11229 07:53:59,478 --> 07:54:02,760 happens and you can see all of our tests 11230 07:54:02,760 --> 07:54:06,240 pass so I hope that this shows you how 11231 07:54:06,240 --> 07:54:09,318 to write integration tests as well as 11232 07:54:09,318 --> 07:54:11,600 endtoend tests I hope that you better 11233 07:54:11,600 --> 07:54:13,360 understand how to do it and how to set 11234 07:54:13,360 --> 07:54:16,080 it up in a way that makes it 11235 07:54:16,080 --> 07:54:18,718 effective and and this will be the last 11236 07:54:18,718 --> 07:54:21,478 tutorial for the entire expressjs series 11237 07:54:21,478 --> 07:54:23,240 I'm still going to make more expressjs 11238 07:54:23,240 --> 07:54:24,680 tutorials it's just not going to be part 11239 07:54:24,680 --> 07:54:28,680 of this specific uh long series that I 11240 07:54:28,680 --> 07:54:31,520 designed so I hope that you all enjoyed 11241 07:54:31,520 --> 07:54:33,160 watching this series if you watch the 11242 07:54:33,160 --> 07:54:35,600 whole entire thing like I said before 11243 07:54:35,600 --> 07:54:37,398 the code will be on GitHub I'm going to 11244 07:54:37,398 --> 07:54:39,558 leave a link in the description as well 11245 07:54:39,558 --> 07:54:41,680 so you all can access it you're more 11246 07:54:41,680 --> 07:54:43,958 than welcome to ask questions Down Below 11247 07:54:43,958 --> 07:54:45,798 in the comment section I check my 11248 07:54:45,798 --> 07:54:47,840 comments every single day and I spond 11249 07:54:47,840 --> 07:54:50,318 whenever I get a chance to if you need 11250 07:54:50,318 --> 07:54:52,638 additional help visit the Discord server 11251 07:54:52,638 --> 07:54:54,680 the link is in the description as well 11252 07:54:54,680 --> 07:54:56,638 you can go onto the Discord server and 11253 07:54:56,638 --> 07:54:59,240 get help with your programming issues uh 11254 07:54:59,240 --> 07:55:01,600 talk to other developers um just you 11255 07:55:01,600 --> 07:55:04,840 know just just hang out and whatever so 11256 07:55:04,840 --> 07:55:06,440 yeah like I said I hope you all enjoyed 11257 07:55:06,440 --> 07:55:08,760 this whole Express Chas tutorial and 11258 07:55:08,760 --> 07:55:11,040 that is going to be it for this one so I 11259 07:55:11,040 --> 07:55:13,558 will see you all in my next episode 11260 07:55:13,558 --> 07:55:15,760 peace 11261 07:55:16,200 --> 07:55:19,200 out 811213

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.