1 00:00:00,180 --> 00:00:01,940 Welcome to your challenge video. 2 00:00:01,950 --> 00:00:02,820 As promised. 3 00:00:02,820 --> 00:00:09,090 In this one it's going to be up to you to use what you've learned about Mongoose so far to improve both 4 00:00:09,120 --> 00:00:11,630 the user and task model. 5 00:00:11,640 --> 00:00:13,890 There are actually two challenges here. 6 00:00:13,890 --> 00:00:17,830 The first one is going to require you to add a new field to user. 7 00:00:17,880 --> 00:00:22,740 And the second is going to require you to customize the task model. 8 00:00:22,800 --> 00:00:28,220 Let's go ahead and start with the user challenge since the user code is already on commented. 9 00:00:28,240 --> 00:00:32,640 Any task code is currently commented out so up above. 10 00:00:32,640 --> 00:00:36,030 I do indeed have some challenge comments for this one. 11 00:00:36,030 --> 00:00:40,700 The big picture goal is to add a password field to user. 12 00:00:40,770 --> 00:00:45,760 Now for the moment you're just going to store that password in whatever form it comes in. 13 00:00:45,780 --> 00:00:51,320 So if someone types out my pass one two three as their password that's what you're going to save. 14 00:00:51,330 --> 00:00:52,640 To the database. 15 00:00:52,740 --> 00:00:59,520 Now this is known as storing a plain text password and it's very bad to do in terms of security. 16 00:00:59,640 --> 00:01:05,700 We are going to explore how we can correctly store our passwords and hash them later on in the class. 17 00:01:05,730 --> 00:01:07,830 But for now we're not going to worry about that. 18 00:01:07,830 --> 00:01:13,300 For now it's OK to store those plain text passwords in the database. 19 00:01:13,350 --> 00:01:19,670 So step number one we want to set up this new password field as a required string. 20 00:01:19,680 --> 00:01:26,400 We also want to ensure that the length is greater than six so seven or more characters the more characters 21 00:01:26,400 --> 00:01:28,710 the more secure the password. 22 00:01:28,710 --> 00:01:32,590 Next up we're going to trim the password removing any spaces. 23 00:01:32,640 --> 00:01:39,480 And finally we're gonna use a custom validator to ensure that the password doesn't contain the string 24 00:01:39,720 --> 00:01:40,780 password. 25 00:01:40,800 --> 00:01:47,630 Now to get this done you can use the includes method which is available on all strings in JavaScript. 26 00:01:47,760 --> 00:01:54,780 If you haven't used that before you can google javascript string includes to pull up the documentation 27 00:01:54,810 --> 00:01:56,670 and learn how it works. 28 00:01:56,670 --> 00:02:02,740 So for step number four you want to use a custom validator now a four step number two. 29 00:02:02,790 --> 00:02:04,230 You could use a custom one. 30 00:02:04,230 --> 00:02:06,610 You could also use the built in one. 31 00:02:06,660 --> 00:02:09,450 Either way gets the job done from there. 32 00:02:09,450 --> 00:02:13,210 You want to test your work create a user with valid data. 33 00:02:13,230 --> 00:02:19,490 Create another one with invalid password data and make sure you're getting the expected results. 34 00:02:19,500 --> 00:02:21,950 All right take some time to knock that one out. 35 00:02:22,020 --> 00:02:28,900 Test your work and when you're done come back and click play. 36 00:02:28,910 --> 00:02:30,050 How did that go. 37 00:02:30,050 --> 00:02:32,570 Let's go ahead and kick things off down below. 38 00:02:32,600 --> 00:02:35,800 By setting up the password to be a required string. 39 00:02:36,050 --> 00:02:38,240 So I'll put that right here after e-mail. 40 00:02:38,240 --> 00:02:43,520 Though the order isn't particularly important and I'm going to go ahead and set it up as an object. 41 00:02:43,520 --> 00:02:48,610 Putting a comma afterwards so the age property gets set up correctly as well. 42 00:02:48,740 --> 00:02:51,680 From here we'll start off with the type definition. 43 00:02:51,680 --> 00:02:57,080 Now once again the order we put things even inside of the object in here is not important. 44 00:02:57,080 --> 00:03:00,430 I just typically follow a pattern where type comes first. 45 00:03:00,440 --> 00:03:06,770 So type is gonna be string and I mentioned that I wanted to make this a required field so it required 46 00:03:06,860 --> 00:03:09,190 is going to get set equal to true. 47 00:03:09,320 --> 00:03:10,370 Perfect. 48 00:03:10,370 --> 00:03:13,170 Now from there we can move on to the other steps. 49 00:03:13,220 --> 00:03:16,590 I want to make sure the length is greater than six. 50 00:03:16,700 --> 00:03:22,550 And I also want to ensure that I'm trimming that password now for the length you could have either used 51 00:03:22,580 --> 00:03:29,150 a custom validator or you could have used the built in mean length validator which we explored in the 52 00:03:29,150 --> 00:03:30,500 documentation. 53 00:03:30,500 --> 00:03:35,960 And I can set this to 7 to ensure that the minimum length acceptable is seven. 54 00:03:35,960 --> 00:03:41,560 From there we'll go ahead and set up trim by setting trim equal to true. 55 00:03:41,600 --> 00:03:47,630 Now that brings us to the last change we need to make for a password which is to ensure that no password 56 00:03:47,840 --> 00:03:50,700 doesn't contain the string password. 57 00:03:50,870 --> 00:03:56,320 We can do that with a custom validator like we did right here for email validation. 58 00:03:56,450 --> 00:04:03,220 So I'll set that up by first configuring the function validate setting it up as a method using that 59 00:04:03,260 --> 00:04:06,180 yes sixth method definition syntax. 60 00:04:06,350 --> 00:04:12,320 We know we get the value right here and then we can throw an error if something has gone wrong if the 61 00:04:12,320 --> 00:04:14,700 password is not valid. 62 00:04:14,750 --> 00:04:20,390 Now in our case what we're gonna do is once again set up an if statement that triggers when the password 63 00:04:20,390 --> 00:04:28,070 is invalid and we're going to throw a new error with our error message and we can say something like 64 00:04:28,070 --> 00:04:34,470 password cannot contain inside of quotes. 65 00:04:34,760 --> 00:04:35,520 Password. 66 00:04:35,720 --> 00:04:40,250 So giving them very clear instructions as to why things have failed. 67 00:04:40,250 --> 00:04:43,650 Now for that if statement what do we want to put inside of here. 68 00:04:43,730 --> 00:04:52,430 I want the code below to run when the value includes using the includes method the following the string 69 00:04:53,000 --> 00:04:53,990 password. 70 00:04:53,990 --> 00:04:59,510 Now if you looked at the documentation you would have found out that includes returns true or false 71 00:04:59,570 --> 00:05:03,800 it returns true if this is included and false otherwise. 72 00:05:03,890 --> 00:05:07,940 Now this is completely acceptable for the purposes of the challenge. 73 00:05:07,940 --> 00:05:10,990 One thing you'll note though is that this is not going to work. 74 00:05:11,000 --> 00:05:17,990 If they type in password with a capital P we can always temporarily convert the value to lower case 75 00:05:18,230 --> 00:05:26,990 to do a case insensitive comparison right here value dot to lower case. 76 00:05:26,990 --> 00:05:29,840 So we're going to take the lower case version. 77 00:05:29,840 --> 00:05:35,420 We're going to compare that with password and that's going to allow us to have a more reliable solution 78 00:05:35,450 --> 00:05:39,430 once again using this was not part of the challenge. 79 00:05:39,440 --> 00:05:45,320 So from here we're actually all done in terms of configuring the field and we can go ahead and test 80 00:05:45,320 --> 00:05:48,640 things out down below with our user setup. 81 00:05:48,650 --> 00:05:51,230 So we're creating the new user and saving them. 82 00:05:51,230 --> 00:05:53,330 Let's bring password into the mix. 83 00:05:53,330 --> 00:06:01,540 I'm going to get things started by adding that on password and let's go ahead and start with a password 84 00:06:01,570 --> 00:06:03,030 that is too short. 85 00:06:03,040 --> 00:06:08,590 So right here what I'm gonna do is have a bunch of spaces followed by a couple of letters. 86 00:06:08,590 --> 00:06:13,340 Let's use R E 3 2 followed by a bunch more spaces. 87 00:06:13,390 --> 00:06:17,500 So after this is getting trimmed the length is indeed going to be too short. 88 00:06:17,500 --> 00:06:23,020 Right now the length would be fine but those spaces are gonna go away because of that trim setting from 89 00:06:23,020 --> 00:06:28,380 the terminal down below we can run the file node source D.B. in there. 90 00:06:28,410 --> 00:06:34,840 We have Mongoose dot J S I'm going to run it and we're going to get one of those really long error messages 91 00:06:34,960 --> 00:06:38,010 just up above we have the actual error message. 92 00:06:38,020 --> 00:06:41,370 Now this is something we'll learn how to customize a bit later. 93 00:06:41,410 --> 00:06:47,590 For now it is path password and then it shows the value is shorter than the minimum allowed a length 94 00:06:47,710 --> 00:06:48,850 which is seven. 95 00:06:48,850 --> 00:06:54,490 So everything is working even though the error message isn't the most useful piece of information. 96 00:06:54,490 --> 00:06:59,590 Now that we have this test in place let's go ahead and test some of the other stuff we set up so we 97 00:06:59,590 --> 00:07:00,850 know trim is working. 98 00:07:00,880 --> 00:07:02,570 We know man length is working. 99 00:07:02,650 --> 00:07:08,320 Let's go ahead and make sure that our validation is working by using password in the password right 100 00:07:08,320 --> 00:07:08,880 here. 101 00:07:09,040 --> 00:07:17,380 I'm gonna use capital P password 1 2 3 and make sure that still fails down below I can use control C 102 00:07:17,380 --> 00:07:21,610 to shut things down up and enter to start them up once again. 103 00:07:21,640 --> 00:07:22,600 And what do I get. 104 00:07:22,600 --> 00:07:27,120 I get a validation error which is expected scrolling through that error. 105 00:07:27,160 --> 00:07:28,520 I'll be brought to the message. 106 00:07:28,600 --> 00:07:31,020 Password can not contain password. 107 00:07:31,150 --> 00:07:32,830 Perfect from here. 108 00:07:32,980 --> 00:07:35,760 Let's go ahead and try something completely valid. 109 00:07:35,800 --> 00:07:43,480 I'll use something like phone 0 9 8 exclamation mark that meets all the criteria we've set up from the 110 00:07:43,480 --> 00:07:44,980 terminal down below. 111 00:07:44,980 --> 00:07:50,830 I'll shut things down I'll restart them once again and this time around I can see that things were indeed 112 00:07:50,890 --> 00:07:53,380 saved to the database correctly. 113 00:07:53,380 --> 00:07:58,960 Now once again I am storing the plain text password exactly as the user provided it. 114 00:07:58,960 --> 00:08:00,460 That is just temporary. 115 00:08:00,460 --> 00:08:03,240 We'll talk about password hashing shortly. 116 00:08:03,310 --> 00:08:08,230 Now that we have this in place the first challenge is all done from the terminal. 117 00:08:08,230 --> 00:08:13,480 I'm going to clear the terminal output and now we're going to dive in to the second challenge for the 118 00:08:13,480 --> 00:08:14,350 video. 119 00:08:14,350 --> 00:08:19,800 The second challenge is going to require you to customize that task model right here. 120 00:08:19,840 --> 00:08:25,800 What I'm going to do is comment out the new user as well as the user save call. 121 00:08:25,810 --> 00:08:32,320 And down below we're gonna go ahead and uncommon the calls to create the new task and to save it as 122 00:08:32,320 --> 00:08:35,830 we'll use those to test our work for this challenge. 123 00:08:35,830 --> 00:08:39,870 Right here I have some challenge comments outlining what I'd like you to do. 124 00:08:39,880 --> 00:08:44,230 Your goal is to add some validation and sanitization to the task. 125 00:08:44,380 --> 00:08:45,850 So what do we want to do. 126 00:08:45,850 --> 00:08:49,550 Well we want to trim the description and make it required. 127 00:08:49,600 --> 00:08:54,400 So we want to make sure there's not a bunch of spaces inside of there and we also want to make sure 128 00:08:54,430 --> 00:08:55,920 it's always provided. 129 00:08:55,930 --> 00:08:58,080 That makes no sense to create a task. 130 00:08:58,120 --> 00:08:59,990 If there is no description. 131 00:09:00,220 --> 00:09:02,030 Next up what do we want to do. 132 00:09:02,080 --> 00:09:09,080 We want to make completed optional and if it's not provided we'll set a default value for it to false. 133 00:09:09,100 --> 00:09:12,970 So you don't have to tell us the completed value in creating a task. 134 00:09:13,030 --> 00:09:15,080 And if you don't we'll assume it's not done. 135 00:09:15,100 --> 00:09:17,030 That's why you're creating it. 136 00:09:17,050 --> 00:09:23,080 Next up you want to test your work and make sure that everything up above is working as expected. 137 00:09:23,080 --> 00:09:28,860 So take some time to knock this one out test your work and when you're done come back and click play 138 00:09:32,740 --> 00:09:33,590 how would you do. 139 00:09:33,590 --> 00:09:36,230 Let's go ahead and kick things off with step number one. 140 00:09:36,230 --> 00:09:39,590 We're going to customize that description right here. 141 00:09:39,620 --> 00:09:45,170 I'll be setting required equal to true making sure the description is always provided. 142 00:09:45,170 --> 00:09:51,560 And I'm also going to set trim equal to true to make sure we're trimming those leftover spaces down 143 00:09:51,560 --> 00:09:55,400 below for completed what we want to do is leave it as optional. 144 00:09:55,400 --> 00:10:00,590 You do not need to provide it but if you don't provide it we do want to store a boolean value in the 145 00:10:00,590 --> 00:10:01,500 database. 146 00:10:01,520 --> 00:10:05,650 Now out of our options true and false I think false makes the most sense. 147 00:10:05,660 --> 00:10:10,190 So right here the default value will indeed be false. 148 00:10:10,190 --> 00:10:12,970 Now let's go ahead and make sure all of this is working. 149 00:10:13,070 --> 00:10:17,810 What I'm going to do is remove the challenge comments and customize things down below. 150 00:10:17,840 --> 00:10:21,870 I forgot if I remove the challenge comments from up above and I didn't. 151 00:10:21,950 --> 00:10:25,250 So I'll take a quick moment to remove those as well. 152 00:10:25,250 --> 00:10:31,760 Now let's verify that the changes to task are actually taking effect to start what I'm going to do is 153 00:10:31,760 --> 00:10:33,110 provide nothing. 154 00:10:33,140 --> 00:10:37,890 I'm going to leave off description and I'm going to leave off completed to see what happens. 155 00:10:37,970 --> 00:10:44,090 I'll save the file I'll run things down below from the terminal by using the up arrow key twice to cycle 156 00:10:44,090 --> 00:10:50,060 past clear to our node command and then I'll hit enter to run the script and right here I am indeed 157 00:10:50,090 --> 00:10:54,170 seeing that the path description is required which is good. 158 00:10:54,170 --> 00:10:55,910 So let's add it right here. 159 00:10:55,910 --> 00:11:00,130 Description I'll set it equal to a string. 160 00:11:00,280 --> 00:11:05,740 I'll toss some spaces up front and then I'll have my description be something like eat lunch which is 161 00:11:05,740 --> 00:11:09,050 currently something I'm putting off to finish this video. 162 00:11:09,060 --> 00:11:09,850 So right here. 163 00:11:09,880 --> 00:11:14,980 This should be valid because we do provide the description and we'll see it get trimmed and there's 164 00:11:14,980 --> 00:11:17,150 no need to provide completed. 165 00:11:17,200 --> 00:11:23,830 And we'll see the default value of false hopefully down below we can shut things down for the final 166 00:11:23,830 --> 00:11:25,980 time and start them up once again. 167 00:11:25,990 --> 00:11:31,870 And what do I get I get my task showing up trim and did work because I'm no longer seeing those extra 168 00:11:31,870 --> 00:11:37,030 spaces and the completed value was automatically set to false. 169 00:11:37,030 --> 00:11:43,030 So with Mongoose we can customize our models to fit our needs making it really easy to add validation 170 00:11:43,060 --> 00:11:49,420 and sanitization into the mix and those are two essential topics when it comes to accepting input from 171 00:11:49,420 --> 00:11:50,410 the user. 172 00:11:50,440 --> 00:11:54,430 Now that we have the basics in place we're going to continue on in the next video. 173 00:11:54,550 --> 00:11:56,380 So stay tuned and I'll see you then.