In this video we're going to wrap up the section by taking a few moments to make sure we remove the
tasks for a user who's choosing to delete themselves.
So if I want to delete my profile my user document should go away.
Yes but my tasks should also go away.
Otherwise they'll just sit around in the database forever.
So to get that done we could take one of two approaches.
The first approach would be to head to the place where users get deleted over in the user router near
the bottom.
We have our delete route.
What we could do is add some code just below this line to also remove the tasks.
Now that's one approach but it's not the approach we're going to take.
Instead we're going to go back to what we learned about middleware and use that now for the moment.
This is the only way you can remove your account.
So we could add the code here just one time but it's a good idea to get in the habit of using middleware.
So if I were to allow another way to delete users from somewhere else in the application that would
also have their tasks deleted as well.
So to do this we'll head over to the user model and if I scroll down near the bottom of the file we
already have one piece of middleware in place.
This hashes the password before the user is saved.
What we're going to do is create some middleware that deletes the tasks delete user tasks when user
is removed.
That is the goal of the middleware.
We'll be setting up right here.
Right here we'll be using user schema dot pre like we had done before and we'll be using a pre remove.
In this case which we had explored earlier in the class over in the Mongoose middleware documentation.
Now from here we will be setting this up as an async function and we will go ahead and define it as
a standard function like we did with our other middleware.
So right here we have the basic setup.
And once again we're going to get access to user via this.
So const user equals this and we also get access to that next argument to let Mongoose know when we're
done running our code and down below we'll make sure to call that at the end.
Now the question is what do we do in the middle.
This is where we are going to delete multiple tasks using just the owner field.
So right here let's go ahead and get that done.
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
const upper a T.
Task I'll use require that is dot forward slash a task and then down below we can actually use.
Task to remove them.
Right down here.
That would be the following task dot.
And we can go ahead and use delete many.
This is listed in the queries documentation.
We could use delete one but that's not what we want.
We want to delete multiple and right here we can have our criteria for deleting.
We're going to delete every single task where the owner value equals the user I.D..
Perfect.
Now the only other thing we need to do is use a wait right here since that is an asynchronous process
and there we go.
We are done now whenever a user is removed.
This code will run.
Removing their tasks as well.
Let's go ahead and wrap up by making sure it works as expected to do this.
We're going to delete the logged in user from postmen using delete user down below.
Before we do though.
Let's go ahead and check out how many tasks we have over in robo 3D.
If I refresh that tasks collection we can see that we have three tasks showing up.
Let's go ahead and make sure some of those go away when the user is deleted.
Over here back in post man I will delete that user by firing this off down below we can see we got a
two hundred with the removed user and if I were to fire it off again I would expect it to fail since
we just deleted that user and the off token wouldn't work anymore.
That's exactly what's happening.
When I fire it off for a second time.
Now if I had over two robo three TI and give things a refresh I can see that user's task the first user
was indeed deleted.
They had two.
One was to finish the node course and one was to go to the office.
We removed one manually and then one just got deleted.
When the user itself was removed.
So now we have the two tasks for Mike.
If I refresh that's exactly what we have and over here under users collection we have just the one user.
This is Mike now that we have that in place.
We are all done with this video I just wanted to cover this really quickly and we're also all done with
this section in this section.
We setup authentication.
This is gonna allow us to make sure that our data isn't just being changed or accessed by anyone.
We have a system for signing up logging in logging out and managing your user data.
Now that we're done with this we're going to continue on to the next section where we'll continue to
expand on our app adding some really interesting features.
I'm excited to get to it.
So let's jump right in to the section intro for the next one.