1 00:00:00,210 --> 00:00:05,370 In this video we're going to wrap up the section by taking a few moments to make sure we remove the 2 00:00:05,400 --> 00:00:09,320 tasks for a user who's choosing to delete themselves. 3 00:00:09,330 --> 00:00:13,340 So if I want to delete my profile my user document should go away. 4 00:00:13,380 --> 00:00:16,740 Yes but my tasks should also go away. 5 00:00:16,740 --> 00:00:19,910 Otherwise they'll just sit around in the database forever. 6 00:00:19,980 --> 00:00:23,090 So to get that done we could take one of two approaches. 7 00:00:23,100 --> 00:00:29,070 The first approach would be to head to the place where users get deleted over in the user router near 8 00:00:29,070 --> 00:00:29,950 the bottom. 9 00:00:29,970 --> 00:00:31,650 We have our delete route. 10 00:00:31,770 --> 00:00:37,870 What we could do is add some code just below this line to also remove the tasks. 11 00:00:37,890 --> 00:00:41,510 Now that's one approach but it's not the approach we're going to take. 12 00:00:41,580 --> 00:00:47,700 Instead we're going to go back to what we learned about middleware and use that now for the moment. 13 00:00:47,700 --> 00:00:50,500 This is the only way you can remove your account. 14 00:00:50,550 --> 00:00:57,200 So we could add the code here just one time but it's a good idea to get in the habit of using middleware. 15 00:00:57,210 --> 00:01:02,940 So if I were to allow another way to delete users from somewhere else in the application that would 16 00:01:02,940 --> 00:01:06,150 also have their tasks deleted as well. 17 00:01:06,150 --> 00:01:12,360 So to do this we'll head over to the user model and if I scroll down near the bottom of the file we 18 00:01:12,360 --> 00:01:15,420 already have one piece of middleware in place. 19 00:01:15,420 --> 00:01:19,530 This hashes the password before the user is saved. 20 00:01:19,560 --> 00:01:28,140 What we're going to do is create some middleware that deletes the tasks delete user tasks when user 21 00:01:28,620 --> 00:01:30,340 is removed. 22 00:01:30,390 --> 00:01:32,250 That is the goal of the middleware. 23 00:01:32,250 --> 00:01:34,020 We'll be setting up right here. 24 00:01:34,020 --> 00:01:41,220 Right here we'll be using user schema dot pre like we had done before and we'll be using a pre remove. 25 00:01:41,220 --> 00:01:47,700 In this case which we had explored earlier in the class over in the Mongoose middleware documentation. 26 00:01:47,700 --> 00:01:53,550 Now from here we will be setting this up as an async function and we will go ahead and define it as 27 00:01:53,550 --> 00:01:57,140 a standard function like we did with our other middleware. 28 00:01:57,150 --> 00:01:59,940 So right here we have the basic setup. 29 00:01:59,940 --> 00:02:03,930 And once again we're going to get access to user via this. 30 00:02:03,930 --> 00:02:10,980 So const user equals this and we also get access to that next argument to let Mongoose know when we're 31 00:02:10,980 --> 00:02:15,930 done running our code and down below we'll make sure to call that at the end. 32 00:02:15,930 --> 00:02:18,550 Now the question is what do we do in the middle. 33 00:02:18,600 --> 00:02:25,440 This is where we are going to delete multiple tasks using just the owner field. 34 00:02:25,470 --> 00:02:28,200 So right here let's go ahead and get that done. 35 00:02:28,260 --> 00:02:35,390 First up we have to load in the task model so up near the top of the file I'm going to load that in 36 00:02:36,320 --> 00:02:38,020 const upper a T. 37 00:02:38,020 --> 00:02:46,420 Task I'll use require that is dot forward slash a task and then down below we can actually use. 38 00:02:46,420 --> 00:02:48,070 Task to remove them. 39 00:02:48,070 --> 00:02:49,630 Right down here. 40 00:02:49,630 --> 00:02:53,530 That would be the following task dot. 41 00:02:53,530 --> 00:02:56,310 And we can go ahead and use delete many. 42 00:02:56,350 --> 00:02:59,440 This is listed in the queries documentation. 43 00:02:59,440 --> 00:03:02,020 We could use delete one but that's not what we want. 44 00:03:02,050 --> 00:03:07,050 We want to delete multiple and right here we can have our criteria for deleting. 45 00:03:07,120 --> 00:03:13,730 We're going to delete every single task where the owner value equals the user I.D.. 46 00:03:13,990 --> 00:03:15,190 Perfect. 47 00:03:15,190 --> 00:03:20,800 Now the only other thing we need to do is use a wait right here since that is an asynchronous process 48 00:03:20,830 --> 00:03:22,030 and there we go. 49 00:03:22,060 --> 00:03:26,010 We are done now whenever a user is removed. 50 00:03:26,020 --> 00:03:27,200 This code will run. 51 00:03:27,220 --> 00:03:29,860 Removing their tasks as well. 52 00:03:29,860 --> 00:03:33,940 Let's go ahead and wrap up by making sure it works as expected to do this. 53 00:03:33,940 --> 00:03:39,860 We're going to delete the logged in user from postmen using delete user down below. 54 00:03:40,000 --> 00:03:41,080 Before we do though. 55 00:03:41,110 --> 00:03:45,820 Let's go ahead and check out how many tasks we have over in robo 3D. 56 00:03:46,210 --> 00:03:51,600 If I refresh that tasks collection we can see that we have three tasks showing up. 57 00:03:51,730 --> 00:03:55,760 Let's go ahead and make sure some of those go away when the user is deleted. 58 00:03:55,960 --> 00:04:02,650 Over here back in post man I will delete that user by firing this off down below we can see we got a 59 00:04:02,650 --> 00:04:08,770 two hundred with the removed user and if I were to fire it off again I would expect it to fail since 60 00:04:08,770 --> 00:04:12,300 we just deleted that user and the off token wouldn't work anymore. 61 00:04:12,310 --> 00:04:13,870 That's exactly what's happening. 62 00:04:13,870 --> 00:04:16,300 When I fire it off for a second time. 63 00:04:16,540 --> 00:04:23,830 Now if I had over two robo three TI and give things a refresh I can see that user's task the first user 64 00:04:24,040 --> 00:04:25,600 was indeed deleted. 65 00:04:25,690 --> 00:04:26,740 They had two. 66 00:04:26,800 --> 00:04:30,940 One was to finish the node course and one was to go to the office. 67 00:04:31,000 --> 00:04:35,140 We removed one manually and then one just got deleted. 68 00:04:35,140 --> 00:04:37,480 When the user itself was removed. 69 00:04:37,480 --> 00:04:39,980 So now we have the two tasks for Mike. 70 00:04:40,000 --> 00:04:46,680 If I refresh that's exactly what we have and over here under users collection we have just the one user. 71 00:04:46,690 --> 00:04:49,960 This is Mike now that we have that in place. 72 00:04:50,020 --> 00:04:55,600 We are all done with this video I just wanted to cover this really quickly and we're also all done with 73 00:04:55,600 --> 00:04:57,550 this section in this section. 74 00:04:57,550 --> 00:04:59,310 We setup authentication. 75 00:04:59,380 --> 00:05:05,800 This is gonna allow us to make sure that our data isn't just being changed or accessed by anyone. 76 00:05:05,800 --> 00:05:12,070 We have a system for signing up logging in logging out and managing your user data. 77 00:05:12,070 --> 00:05:16,870 Now that we're done with this we're going to continue on to the next section where we'll continue to 78 00:05:16,870 --> 00:05:20,900 expand on our app adding some really interesting features. 79 00:05:20,950 --> 00:05:22,200 I'm excited to get to it. 80 00:05:22,240 --> 00:05:25,660 So let's jump right in to the section intro for the next one.