1 00:00:00,660 --> 00:00:02,640 -: In the last section we added our logic to see 2 00:00:02,640 --> 00:00:06,480 if a user is already making use of the given email address. 3 00:00:06,480 --> 00:00:08,189 So, remember this is our signup route. 4 00:00:08,189 --> 00:00:10,680 So, we don't want to allow people to keep signing up 5 00:00:10,680 --> 00:00:12,390 with the same email address. 6 00:00:12,390 --> 00:00:14,010 We wanna make sure that each user has 7 00:00:14,010 --> 00:00:16,353 a very unique email address. 8 00:00:17,520 --> 00:00:20,520 So, we implemented step one and now we're onto step two. 9 00:00:20,520 --> 00:00:24,720 We're gonna say if we did find a user in our database 10 00:00:24,720 --> 00:00:26,940 if this existingUser is populated right here 11 00:00:26,940 --> 00:00:28,260 if this has a value. 12 00:00:28,260 --> 00:00:30,123 We want to return an error. 13 00:00:32,340 --> 00:00:34,410 We're also going to handle the case in which 14 00:00:34,410 --> 00:00:37,200 the search itself through an error. 15 00:00:37,200 --> 00:00:39,660 So, this err object right here would be populated 16 00:00:39,660 --> 00:00:41,850 with an error if say 17 00:00:41,850 --> 00:00:44,610 a database to the connection did not exist. 18 00:00:44,610 --> 00:00:46,020 So, let's first handle that case. 19 00:00:46,020 --> 00:00:50,370 We'll say if there was an err return next err. 20 00:00:50,370 --> 00:00:53,160 Again, this would handle the case in which 21 00:00:53,160 --> 00:00:55,800 connection to the database just failed. 22 00:00:55,800 --> 00:00:57,750 And now we'll handle the case in which 23 00:00:57,750 --> 00:01:00,377 there is already an existingUser. 24 00:01:01,500 --> 00:01:02,970 So, I'm gonna move this comment up 25 00:01:02,970 --> 00:01:04,650 just to make sure or just to make clear 26 00:01:04,650 --> 00:01:06,800 that it corresponds with this If statement. 27 00:01:07,800 --> 00:01:10,860 So, if a user with this email does exist 28 00:01:10,860 --> 00:01:12,630 that would be indicated by the presence of 29 00:01:12,630 --> 00:01:14,160 this existingUser. 30 00:01:14,160 --> 00:01:17,070 We're going to return an error on the request. 31 00:01:17,070 --> 00:01:19,710 So, return and error on our request 32 00:01:19,710 --> 00:01:24,360 we can just write return res dot status. 33 00:01:24,360 --> 00:01:28,530 Dot status is going to set the http code on our request 34 00:01:28,530 --> 00:01:30,000 or on the response. 35 00:01:30,000 --> 00:01:31,860 We're gonna set it equal to 422 36 00:01:31,860 --> 00:01:33,060 which means 37 00:01:33,060 --> 00:01:35,340 Unprocessable Entity. 38 00:01:35,340 --> 00:01:37,260 In other words we couldn't process this. 39 00:01:37,260 --> 00:01:39,660 You, the data you supplied was bad. 40 00:01:39,660 --> 00:01:41,160 You gave us an email 41 00:01:41,160 --> 00:01:43,683 that corresponds to a user that already exists. 42 00:01:44,610 --> 00:01:46,600 So, we'll set the status to 422 43 00:01:47,490 --> 00:01:51,667 and then we will send the response off with an error of 44 00:01:51,667 --> 00:01:52,600 "Email 45 00:01:53,610 --> 00:01:54,807 is in use". 46 00:01:57,390 --> 00:01:58,223 Okay. 47 00:01:58,223 --> 00:01:59,056 So, that's gonna handle the case 48 00:01:59,056 --> 00:02:01,380 in which an email does exist. 49 00:02:01,380 --> 00:02:04,800 Let's now handle the case in which a email 50 00:02:04,800 --> 00:02:06,390 or a user with this existing 51 00:02:06,390 --> 00:02:08,190 with this email does not exist. 52 00:02:08,190 --> 00:02:12,063 So, we want to now create and save this user record. 53 00:02:13,020 --> 00:02:16,233 I'm gonna first move this comment up into a callback. 54 00:02:18,930 --> 00:02:20,703 Let's also move this comment up. 55 00:02:23,670 --> 00:02:24,503 Cool. 56 00:02:26,100 --> 00:02:29,550 So again, if a user with the email does not exist 57 00:02:29,550 --> 00:02:33,330 we want to create and save a new user record. 58 00:02:33,330 --> 00:02:35,010 So, to make a new user record 59 00:02:35,010 --> 00:02:38,580 we're going to use the new keyword on our User class. 60 00:02:38,580 --> 00:02:41,523 That's gonna make a new instance of our user model. 61 00:02:42,540 --> 00:02:45,240 So, we'll say our new user 62 00:02:45,240 --> 00:02:48,060 is gonna be a new User. 63 00:02:48,060 --> 00:02:50,430 And then we pass into this an object 64 00:02:50,430 --> 00:02:52,770 with the attributes that we want this user to have 65 00:02:52,770 --> 00:02:56,610 specifically the email and the password we want it to have. 66 00:02:56,610 --> 00:02:58,320 Now, if you recall we already pulled off 67 00:02:58,320 --> 00:03:01,020 the email and and password off the request up here. 68 00:03:01,020 --> 00:03:02,820 So, we can just make direct use of 69 00:03:02,820 --> 00:03:06,123 the email and password inside of this new User constructor. 70 00:03:07,140 --> 00:03:12,090 So, I'll say email is email and password is password. 71 00:03:12,090 --> 00:03:14,520 So, this creates my new user. 72 00:03:14,520 --> 00:03:16,710 But very importantly it only creates it 73 00:03:16,710 --> 00:03:18,480 it does not save the user. 74 00:03:18,480 --> 00:03:19,980 It just creates this in memory right now. 75 00:03:19,980 --> 00:03:23,580 So, this is just an in memory representation of this user. 76 00:03:23,580 --> 00:03:26,610 To actually save this user to our database 77 00:03:26,610 --> 00:03:27,840 we need to call 78 00:03:27,840 --> 00:03:29,943 user dot save. 79 00:03:31,650 --> 00:03:34,083 So, this will save the record to the database. 80 00:03:35,250 --> 00:03:38,730 Now, as you might imagine this is another operation. 81 00:03:38,730 --> 00:03:41,310 Any time that we start talking to the database 82 00:03:41,310 --> 00:03:42,180 we're talking about something 83 00:03:42,180 --> 00:03:43,740 that's gonna take some amount of time. 84 00:03:43,740 --> 00:03:45,870 It's gonna take some number of milliseconds 85 00:03:45,870 --> 00:03:48,060 to go off and save this user. 86 00:03:48,060 --> 00:03:51,240 And so, if we want to know when this user is saved 87 00:03:51,240 --> 00:03:53,160 we need to pass a callback. 88 00:03:53,160 --> 00:03:54,813 So, we'll pass a callback in. 89 00:03:55,860 --> 00:03:58,590 Callback as usual is being called with an err object 90 00:03:58,590 --> 00:04:00,720 if it failed to save. 91 00:04:00,720 --> 00:04:02,790 So, if the user failed to save 92 00:04:02,790 --> 00:04:04,120 we'll say if err 93 00:04:05,130 --> 00:04:07,083 return next err. 94 00:04:08,400 --> 00:04:11,430 Otherwise, at this point in time 95 00:04:11,430 --> 00:04:15,150 we have successfully saved our user. 96 00:04:15,150 --> 00:04:17,459 We can now respond to the request indicating 97 00:04:17,459 --> 00:04:19,019 that the user was created. 98 00:04:19,019 --> 00:04:21,783 And we'll do that by saying res dot json. 99 00:04:23,340 --> 00:04:25,860 Let's just send back the entire user for right now. 100 00:04:25,860 --> 00:04:27,930 Let's just verify that the user is actually getting saved. 101 00:04:27,930 --> 00:04:31,230 Let's see what the actual saved user model looks like. 102 00:04:31,230 --> 00:04:32,820 Okay, so I'm gonna save this file. 103 00:04:32,820 --> 00:04:34,410 I'm gonna flip over my terminal 104 00:04:34,410 --> 00:04:35,940 make sure I don't have any typos in here. 105 00:04:35,940 --> 00:04:37,260 Everything looks good. 106 00:04:37,260 --> 00:04:39,393 So, let's go back over to Postman. 107 00:04:40,380 --> 00:04:41,610 I've got a post request 108 00:04:41,610 --> 00:04:45,810 going to localhost3090 with route signup. 109 00:04:45,810 --> 00:04:50,460 I've got a body set with an email and a password. 110 00:04:50,460 --> 00:04:52,020 Let's give this a shot. 111 00:04:52,020 --> 00:04:56,340 I'm gonna send and my response is looking pretty good here. 112 00:04:56,340 --> 00:05:01,230 I got back from my database an email, a password, and an ID. 113 00:05:01,230 --> 00:05:02,100 That was 114 00:05:02,100 --> 00:05:03,780 this record was saved with. 115 00:05:03,780 --> 00:05:05,880 So, whenever we save a record in our database 116 00:05:05,880 --> 00:05:08,583 it automatically gets this ID property assigned. 117 00:05:09,450 --> 00:05:10,650 So, this is looking good. 118 00:05:10,650 --> 00:05:12,630 Let's make another request now 119 00:05:12,630 --> 00:05:15,960 and see what happens when we try to create another user 120 00:05:15,960 --> 00:05:18,270 with the same exact email. 121 00:05:18,270 --> 00:05:19,710 So I'm gonna send 122 00:05:19,710 --> 00:05:22,110 and I get the error "Email is in use". 123 00:05:22,110 --> 00:05:22,943 Perfect. 124 00:05:22,943 --> 00:05:23,793 Just what we wanted. 125 00:05:25,050 --> 00:05:27,060 Email is in use. 126 00:05:27,060 --> 00:05:28,203 Email is in use. 127 00:05:29,220 --> 00:05:30,053 Cool. 128 00:05:30,053 --> 00:05:31,170 So, this looks great right here. 129 00:05:31,170 --> 00:05:32,163 I like it a lot. 130 00:05:33,330 --> 00:05:34,260 As a quick wrap up 131 00:05:34,260 --> 00:05:36,720 let's run through the comments one more time. 132 00:05:36,720 --> 00:05:38,160 In our request handler 133 00:05:38,160 --> 00:05:40,953 we pulled off an email and password off the body. 134 00:05:42,210 --> 00:05:43,740 We saw if a user with the 135 00:05:43,740 --> 00:05:45,963 given email address already exists. 136 00:05:47,430 --> 00:05:50,730 If a user did exist already we sent an error back. 137 00:05:50,730 --> 00:05:54,990 Otherwise, we created a new user model and saved it. 138 00:05:54,990 --> 00:05:56,940 We then responded with some json 139 00:05:56,940 --> 00:05:58,860 that contained the user model. 140 00:05:58,860 --> 00:06:00,570 Now, we're definitely not gonna want to send back 141 00:06:00,570 --> 00:06:02,130 the whole user model. 142 00:06:02,130 --> 00:06:04,530 You know we don't wanna send back 143 00:06:04,530 --> 00:06:06,540 to the user whatever their password was. 144 00:06:06,540 --> 00:06:08,580 You know, I can now change this email to say 145 00:06:08,580 --> 00:06:10,710 test one create a new user 146 00:06:10,710 --> 00:06:12,870 and we're sending the password back in plain text. 147 00:06:12,870 --> 00:06:14,130 We definitely don't wanna do that. 148 00:06:14,130 --> 00:06:18,420 So, let's change the response right here to just be success. 149 00:06:18,420 --> 00:06:19,253 True. 150 00:06:22,320 --> 00:06:25,050 Let's try making a new email now 151 00:06:25,050 --> 00:06:26,370 and we get success, true. 152 00:06:26,370 --> 00:06:27,810 Perfect. 153 00:06:27,810 --> 00:06:29,070 So, this is looking pretty good. 154 00:06:29,070 --> 00:06:31,203 Let's continue on in the next section.