In this video.
Let's see how we can update a user profile.
So inside the user controller, we have a dummy root.
And here is the update line number 89 or nine code.
So let's expand it and now let's test the endpoints if it exists.
And this is the endpoint when I hit ST, indeed the root has been called, so let's see how we are going
to implement the update.
So here we go.
Remember, if we update a user field with a password, it means that we are changing the details about
the user, including the password.
So with this one, unless we rehash the new password.
So first we are going to make some checking.
One, if a user provide a password field, then you go ahead and then re hash the new password.
Or if a user is updating, add our field apart from the password.
We need to also handle that.
So let's have the steps.
Like I said, since we have the user being log in and we are going to protect this endpoint that we
need to have the user and that red dot user.
So with that one, we don't need ID as we see here.
So let's get back to the root and the users and now let's make changes.
So here we go, let's locate the update, which is this one.
So now let's remove the ID because we want to have the ID inside the red dot user.
So now let's go to the controller and let's look at the step.
Step number one is that we are going to check the email the user is trying to update to.
Let's say that originally the email is a at gmail.com and then for updating the user is trying to change
from a dot com to let's say B at gmail.com.
It can happen that B at gmail.com has been used by different person in that way.
We need to avoid that.
So here are going to be check if email exists.
So here we go const.
User found.
Is equal to a wait and for the user model they are going to find by ID and the ID can be found on that
rec dot user.
So here we go.
So let's go ahead and throw some error.
In case this user name exists.
So here, if the user found, then we are going to return and then you pass the error to next.
Because of that, let's add next to the arguments as that.
So here we call next and now we pass in our instance of our app error handler.
Then we pass in the error name as user name Xs or email is ticking or exists and then let's provide
the status code as 400.
So now the next step.
The next step is that check if user is updating the password.
So here we go.
If rec dot body dot password.
Meaning if the user is providing password, now you go ahead and hash the user password.
So as we did for registration, we need a source equal to a weight.
And then the big package dot gen sort, they pass in ten.
After that we want to have the hashed password as hashed password is equal to a weight.
And now we bring in the big quick package dot hash.
Then you pass in the new password that is rec dot, body dot password.
And lastly, we go ahead and then update the user.
So here we go, updates the user.
So here const user is equal to a weight.
Now we are going to find the user by ID and then update the pass in the rec dot user.
So here we pass in the field, the user is trying to update.
So for this one is a password field is equal to the hashed password.
The next argument that this function takes is going to be the new attribute because you want to see
the updated data on the fly.
So here we provide new to true.
And next is we want to run the validation in case a user refuses to provide the password field and going
to be true.
So the last step is that we need to send a response to the user.
So let's go last this one and now here is the response.
So here send the response and here we go, raise dirt status, then provide 200, meaning everything
is okay.
And here is our JSON data.
You provide the status as success and then the data as the user, and that is it.
So this is for the password handling, right?
So next is if a user refused to provide a password, but the person is trying to update the other properties.
So let's see, const user is equal to our weight.
And then for the user model, I'm going to find by ID and update and the ID is under rec the user.
And here we are going to use the entire request body for the updating you pass in the rec dot body.
And lastly, you pass in the configuration as new to true and run validation also to true and that is
it.
Lastly, let's send the response.
We can copy this one and down here we can remove this one and paste this one here the say response.
So now we are done with the concept, a thing where we need to pass the source and the second element
to the hash.
So guys, so let's go ahead and then protect this controller.
So inside the user's root, here we go.
And now let's locate the update.
So you're going to pass in a log in mature as is logged in.
And come on.
So now it's time to test it, remember?
The path has changed.
So is no more an ID here, but instead something like this.
So with this one, let's try to make a request without passing in any payload.
And let's see the error where cannot get.
It means that we are not doing something right.
So inside the user's route.
Where is this one?
So let's remove the ID.
Yes.
Good.
So let's again try and get rid of split because in the past, in the token.
So let's go ahead and then look in from here.
And here we go.
We have the user code John and our grab John token and then here for the update, let's go ahead and
provide that under headers.
And here you bring in authorization and bearer and I'll paste the token and let's get sent where we
can see that email is taking meaning and providing an empty email.
So here you go.
Let's say I want to update the foo name.
So here as foo name and I want to change this one to change.
And now let's send and you can see we have message email is taken meaning that we are doing something
right.
So let's get to the controller and let's see where the error is coming from here.
So I think we have something wrong about this one.
Well, we are not going to find the user by the ID, but instead we want to check if a user is trying
to provide the email as a way of updating.
So here we are going to find the user by email and as rec dot body dot email.
Great.
So let's save it now.
Let's check it out.
When I hit send, let's comment this one and I'll show you as we move on.
So now in this one, I can go ahead and make a request.
And now you can see that I have updated the full name to change.
BLOCK So again, if I try to update the email field to somebody's email, which has been taken already,
so I think I have this email or at home at gmail.com or better.
So let's check inside our collection of a database and let's see, let me connect my DB.
I want to look for email which has been registered.
So here users and documents.
Let me check this one I have John.
No, I want outbound.
Yes, you can see that this email has been taken by different person.
So if I try to update to this email, let's see now back to the update.
So I want to change from John to add on.
But for this item here, it has been taken by a different person, but I so I can go ahead and then
change it.
That is what I want to avoid it.
So inside the user's controller for this, we can make some conditions here before returning an error.
So here let's check that.
We want to check if a user is trying to provide the email field as rec, dot, body, dot email, then
we are going to call this functionality.
So inside here I'll paste that and now let's go ahead and add it again.
So let's hit send where I think the error is coming from here we are finding the user by ID, but our
criteria says email.
So let's change this one to find one.
So for find one, we can find a data based on any property.
So with this one, I think we are good to go.
So let's hit send and you can see that this email is taken.
So we can modify the message by saying that e-mail is taking all.
You already have this email.
All right.
So let's try and send again and let's look at the error and there you go.
We have it as email mail is taking or you already have this email.
So let's try to change the password now.
The password still remains the same as one, two, three, four, five.
And now the email is this.
So let's go ahead and then log in and now let me remove this one and use the ATM.
Now let's say you can see that everything remains the same, but if I change the email and let's see
this email doesn't exist.
If email is correct and the password is incorrect, we get the same result.
So let's go ahead and then update the password also.
So inside the update this time around, I won't remove the email and then update the password as that.
And I would change this 1 to 1 two and let's hit St now in Sussex.
Let me log in with a format password and now I think let me check my server here.
Oh, I got some error here.
Meaning that cannot set headers after they are sent to the client.
Meaning that inside here we need to return something.
That is an update which is.
Let me see where we are sending.
Let me check for the password yet.
This one, we need to ten return here.
Yes, and that is it.
It will fix that.
So let me try again.
Okay.
Now is correct.
Let me log in with a format password and a C invalid.
If I change this 1 to 1, two and let's see now it works.