1 00:00:00,900 --> 00:00:03,330 We've now got a fully functional user model. 2 00:00:03,330 --> 00:00:07,620 I know writing out these interfaces and understanding that generic syntax is really nasty stuff. 3 00:00:07,680 --> 00:00:10,860 But the good news is we really only had to do this the one time. 4 00:00:10,920 --> 00:00:14,640 Now any time we start to create a model in the future it's really just gonna be kind of a copy paste 5 00:00:14,670 --> 00:00:17,170 of what we already did inside of your. 6 00:00:17,240 --> 00:00:22,430 So now that we've got our user model we can get back on track with our sign up process as a reminder. 7 00:00:22,480 --> 00:00:27,470 Anytime someone makes a request to our sign up root handler we're going to first check and see if someone's 8 00:00:27,500 --> 00:00:32,800 already using that email address if they are we will respond with an air after that. 9 00:00:32,830 --> 00:00:38,350 If the email is not in use we will make sure that we store the password with a hash password or the 10 00:00:38,350 --> 00:00:40,220 user with the hash password. 11 00:00:40,390 --> 00:00:44,040 Eventually we will save that user to the database. 12 00:00:44,070 --> 00:00:49,610 Let's go over to our sign up ro handler and start to implement some of the stuff inside my roots directory. 13 00:00:49,620 --> 00:00:52,590 I'll find the sign up dot test file at the very top. 14 00:00:52,590 --> 00:01:01,360 I'm going to immediately import the User model from up one directory models user or me not user model 15 00:01:01,390 --> 00:01:05,350 but just user by itself. 16 00:01:05,390 --> 00:01:07,030 Then go down to my root handler. 17 00:01:07,100 --> 00:01:08,620 Here it is right here. 18 00:01:08,700 --> 00:01:12,560 Now we've got some logic inside of your already around handling request validation. 19 00:01:12,710 --> 00:01:17,810 That stuff is all good and we want to keep it but everything after it was really just some testing code. 20 00:01:17,810 --> 00:01:23,580 So I got delete the console log the throw and the resort sent that entire database connection. 21 00:01:23,600 --> 00:01:25,520 Everything was really just kind of a test. 22 00:01:25,520 --> 00:01:30,230 Anyways we're not going to use that inside the threat handler anymore anyways so I got to delete the 23 00:01:30,230 --> 00:01:31,120 important statement for it. 24 00:01:31,120 --> 00:01:39,640 At the very top of the file and then back down here if we get past that initial request validation step 25 00:01:40,490 --> 00:01:42,180 will then go through this workflow. 26 00:01:43,440 --> 00:01:48,660 So first we need to do is see if a user inside of our database already exists with that same exact email 27 00:01:49,390 --> 00:01:53,580 so to check for that we're really just going to do a query of our user collection and see if someone 28 00:01:54,060 --> 00:01:56,430 has that matching email address. 29 00:01:56,530 --> 00:02:04,650 I'm going to first hold the email off of the wrecked body property I'll then define a new variable called 30 00:02:04,710 --> 00:02:10,410 existing user and I'm going to run a query over my user collection by using the user model. 31 00:02:10,410 --> 00:02:17,150 So I'll say a weight user dot find one with that same exact email address. 32 00:02:17,160 --> 00:02:21,540 If we find another user with this e-mail then that user that we found inside of our database will be 33 00:02:21,540 --> 00:02:23,100 assigned to this variable. 34 00:02:23,100 --> 00:02:29,100 Otherwise if we do not find somewhat that email existing user it will instead be no it's all we really 35 00:02:29,100 --> 00:02:33,350 have to do is check and see if this validly assuming this variable is null if it is. 36 00:02:33,420 --> 00:02:34,750 That means we can create the user. 37 00:02:34,770 --> 00:02:41,570 Otherwise if it is not null then we need to return early and send back in error to the user inside of 38 00:02:41,620 --> 00:02:45,380 you're going to say if existing user is defined 39 00:02:48,190 --> 00:02:54,980 I'll do a console log that says email in use I going to return early. 40 00:02:55,260 --> 00:02:59,020 And right now it's going to send back an empty response. 41 00:02:59,110 --> 00:03:02,660 We don't really have any way to test this just yet because we are not creating any users. 42 00:03:02,730 --> 00:03:07,390 Secondly this as is for just a moment we're going to add in the ability to actually create a user and 43 00:03:07,390 --> 00:03:12,670 then we'll come back and add in some better error handling code inside their OK. 44 00:03:12,700 --> 00:03:18,010 So if we get past that step in theory we need to start to hash the password hash and the password is 45 00:03:18,010 --> 00:03:22,010 a complicated little process and we've been doing a lot of really in-depth stuff. 46 00:03:22,060 --> 00:03:26,920 So right now we're going to very temporarily skip the step and go directly on to creating a new user 47 00:03:26,950 --> 00:03:28,810 and saving them to our database. 48 00:03:28,900 --> 00:03:32,200 So we are going to implement password hashing because you absolutely have to have that. 49 00:03:32,440 --> 00:03:38,720 But very briefly we're just going to skip down to actually create the user so if we get past that step 50 00:03:38,720 --> 00:03:43,270 right there we then need to create our user and save them to the database. 51 00:03:43,320 --> 00:03:48,530 So for that we're going to do a honest user. 52 00:03:48,710 --> 00:03:50,970 And remember how we create a new user document. 53 00:03:51,110 --> 00:03:58,440 We call user dot built and we're gonna pass on the attributes or this user that we're trying to create. 54 00:03:58,520 --> 00:04:02,860 So we'll pass in the email and the password. 55 00:04:02,940 --> 00:04:04,880 We have not defined password inside of here. 56 00:04:04,890 --> 00:04:08,370 Remember password is on the body of the request. 57 00:04:08,430 --> 00:04:10,470 We already did validation on that password. 58 00:04:10,470 --> 00:04:16,540 During this earlier validation step up here so in addition to pulling off email I will also pull off 59 00:04:16,810 --> 00:04:23,740 Astrid so we are building the user just building them does not actually save them to the database to 60 00:04:23,740 --> 00:04:24,870 see them to the database. 61 00:04:24,940 --> 00:04:30,290 We have to do and await user dot save that will persist them too. 62 00:04:30,350 --> 00:04:38,240 Margot D.B. after saving them to the database we should send back to whoever made this request a cookie 63 00:04:38,240 --> 00:04:40,250 or a Jason web token or something. 64 00:04:40,250 --> 00:04:42,450 That's another step that will figure out in just a moment. 65 00:04:42,500 --> 00:04:44,860 Right now let's just respond to this request. 66 00:04:44,870 --> 00:04:51,410 Say that the user was created and sent back the user model or the user documents we'll do a resident 67 00:04:51,410 --> 00:04:58,040 status of two a one which indicates that a record was created as opposed to a two hundred and I'll send 68 00:04:58,040 --> 00:05:01,370 back the entire user case. 69 00:05:01,420 --> 00:05:03,770 This looks at least somewhat reasonable right now. 70 00:05:04,220 --> 00:05:11,120 Let's go ahead and try to do a little test I'm going to open up postman I still have postman open from 71 00:05:11,120 --> 00:05:12,680 when we are working with it earlier. 72 00:05:12,680 --> 00:05:19,060 So I'm making a post request to ticketing that Dev flash API users sign up. 73 00:05:19,280 --> 00:05:25,400 I've got a header of content type application Jason my body is set to raw. 74 00:05:25,530 --> 00:05:27,140 That sounds really awkward. 75 00:05:27,210 --> 00:05:33,150 I've got a valid e-mail a valid password and I've selected Jason over here as well. 76 00:05:33,200 --> 00:05:36,470 I'm gonna go ahead and send in this request all right. 77 00:05:36,470 --> 00:05:36,940 There we go. 78 00:05:37,730 --> 00:05:42,740 So it looks like I have successfully created a user and sent them back to whoever made this request 79 00:05:42,740 --> 00:05:45,020 in this case our postman client. 80 00:05:45,050 --> 00:05:51,170 Now in theory this user has been saved to our Mongo database we can now start to test out the workflow 81 00:05:51,680 --> 00:05:54,910 where we tried to sign up with a duplicated email address. 82 00:05:55,040 --> 00:06:00,080 Let's try to send this exact request once again hopefully we will get back some kind of empty response 83 00:06:00,260 --> 00:06:05,390 that's going to tell us that sorry the user was not created if I tried to send the exact same request 84 00:06:05,900 --> 00:06:13,750 I get back an empty object and if I go to my terminal I'll see a console log right there of email in 85 00:06:13,750 --> 00:06:16,000 use. 86 00:06:16,020 --> 00:06:20,100 All right let's say that this initial flow that we put together looks pretty good but there's definitely 87 00:06:20,100 --> 00:06:24,810 a couple of things we need to fix up we need to make sure that we hash that password. 88 00:06:24,810 --> 00:06:29,670 You'll notice that when we sent a response back when a user was created I'm going to put in a unique 89 00:06:29,670 --> 00:06:35,070 e-mail right here and then send it when we sent the user back the password was included which we definitely 90 00:06:35,070 --> 00:06:41,640 do not want to do and if the email was already in use we are sending back just an empty object which 91 00:06:41,640 --> 00:06:43,360 is not appropriate as well. 92 00:06:43,530 --> 00:06:45,210 There's still a little bit of stuff to work on. 93 00:06:45,300 --> 00:06:47,030 Let's start working on this in the next video.