In this video, we are going to create a controller for updating a user password.
So let's get back to the controllers.
And for users, let's locate the damage controller.
So let's collapse this one.
And this is what we have as an update password controller.
So let's look at the step.
Step number one is less the structure, the password the user is trying to update from red dot body
and here I'll have access to the password.
So the first step is that let's check if the user is updating the password.
So the comment goes like this.
And here, if indeed the user is updating the password, then we are going to rehash the password again.
So going to be as const and then sorts is equal to await the package copy scripts and we have what is
called gen sort pass in ten.
The process is the same when we are hashing the user password upon registration.
So here is going to be as password hashed is equal to a weight.
Then the package called be crypt dot hash, then you pass in the plane password and then the source.
So next step is that we need to update the user.
So here we go.
UPDATE and we are going to await the user dot find by ID and update and the ID can be found under red
dot params, dot ID and then the fields I want to update for this one.
I want to update the password only and I want to assign to the password hashed.
Then the additional configuration as new to true meaning.
I want to have the updated record.
All right.
So the last step is that let's send this message by saying that password has been changed successfully
and here we go.
So for the error here, we are going to send our custom error by calling next.
And here we go.
So I need to pass in next as a third argument to this.
And down here called next and I'll pass in the app error and then the message.
So here I will say please provide password field.
All right.
So let's go ahead and make the request.
So inside the update password, which is this let's provide the write ID so I can use.
Let me locate the log in user here and let's grab the ID.
So I'm going to grab this one and the update endpoint, which is password update.
Let me provide the ID and then the body.
I want to change the password from one, two, three, four, 5 to 1, two, three and hit send where
we go back.
Please provide password field meaning that we are doing something wrong.
So inside the update password controller, we need to bring this logic inside the if statement here.
So let's go ahead and then cut the logic from here.
And let's place it inside the if statement.
So let me bring the comment above here as update user.
So now let me go ahead and make the request again.
And you can see password has been changed successfully.
So let me add y to it as successfully.
So let me go ahead and log in with the updated password.
So let's look at the log in.
So first of all, let me log in with the format password as one, two, three, four, five and hit
send.
You can see we have looking for meaning that this password has been changed.
If I change it back to one, two, three.
Let's have a look.
Now it works fine.
All right.
So in the next video, let's go ahead and allow a user to update the profile picture or the cover image.