All right.
Up next, let's work on update user functionality.
And this one is going to be extremely similar to update job.
However, we're not looking for the ID in req.params.
It's actually going to be available in rec, dot user user ID and also we won't send back the.
Updated user essentially in order to check the changes.
We'll call get current user, which gets the current user because that's how our front end is going
to work.
So let's try it out.
I'm going to go to user controller.
We're looking for this one over here.
And essentially let's go with const updated.
User.
That is going to be equal to a.
Then user find by ID and update and let's pass in req dot user, user id, comma and then rec dot body.
So essentially this is where we'll provide all of the values.
And again, we're not going to be sending back this updated user.
So in order to check the changes, we will use get current user and also in the Readme, we added Json.
So that way you can speed this up a little bit as far as providing the values in the body.
So now let's go to update user request and like I said, I'm just going to copy and paste.
I believe I'm changing here the location from my city to Florida.
So let me send if everything is correct.
We can move on to the next step where we want to get the current user.
So this is basically, John, before we updated the user and once I send it, notice now the location
is Florida and everything is awesome, but there is a major piece missing and that is validation because
keep in mind, just like when we registered the user, also when we update, we want to make sure that
the values that are coming in are exactly what we're expecting.
So now let's quickly navigate to validation middleware and let's create a new one.
Now the good news is that we already have validation for register and most of the stuff is going to
be exactly the same.
So first we just want to come up with the value and let's go here with Validate and I'm going to call
this update user input.
Remember, we want to invoke our function.
And as far as the values, let's just scroll up.
We're looking for a registered one.
Let's grab all of these.
Lines of code, copy and paste.
So the only thing we're going to change here is the logic for email.
We'll use a different approach.
And also, when it comes to a password, we're actually not going to check it.
So for now, we have location, last name, also name and email.
So all of this stays the same.
The only thing we'll change is the custom one.
And as far as the logic, this line will stay exactly the same.
But here in the condition, we'll add some logic.
Basically, I'm going to go with and then I'll look for user underscore ID, Then I'll transfer this
one to string and then I'll say if it's not equal to req dot user user ID.
Now, why do we need to add this line of code?
Well, because remember, this deals with that unique email.
Correct.
And of course, if I'm sending in John at gmail.com, if I'll just check whether a user exists based
on this email, of course it's going to be a user.
It's me.
So with this line of code, we're just making sure that user exists.
But the user is not us.
So essentially, if I'm sending in some kind of email address, I'm checking for other users and whether
they are not already using this email address.
Hopefully that makes sense.
So this stays the same.
Again, we'll send back the error.
We just want to quickly jump to a router, I believe.
So let's go to user router and we want to add here this validate.
So right before the update user, we're going to go with validate, update user input.
And now let's try it out.
So I'm going to navigate to update user.
And you know what?
Let's go with.
No last name.
So let me remove it here.
Let me send a notice.
Last name is required.
Now, please keep in mind that again, on the front end, everything is going to be set up where we
are sending these values.
We just have validations in place so we get the correct ones.
And for example, if I'm going to go here with Peter at gmail.com, I'm going to get back the bad request.
Why?
Well, because peter@gmail.com is already used by another user.
That's why we added that additional logic over here.
However, if I go with John, of course the user already exists, but that user is me.
So of course that is allowed.
And the last thing that I want to do.
As far as the user controller, I just want to remove from the body the password since I don't have
any validation and I don't want the user to update password with this particular controller.
How we can do that?
Well, first we need to come up with some kind of value.
In my case, again, I'm going to go with Object over here.
Then we will spread out all of the properties we have in Req.body.
So we're going to go with dot, dot, dot then req dot body.
And then let's go with object.
And remember the keyword was delete.
So pretty much just like we did over here.
When we were setting up that instance.
One.
I just want to remove this property just in case it's being passed.
So let's go with password just in case it exists.
And then instead of req.body.
We're just going to pass in the object over here.
So that's the last thing that I want to actually check it out.
Let me log what I have as far as the object.
And let me add here this password.
So we're going to go here with password and let's set it equal to secret.
We also need to add a comma.
Let me send it over here.
And then if everything is correct, notice we don't have this password.
So essentially, we just make sure that the password value doesn't get added to our.
Update user functionality.
And with this in place, we can move on to the next step.