Okay, So now we want to get these two, these two functions and these two roots working to get the
user profile and to update the user profile.
So it's actually going to be pretty simple as far as getting the user profile, we just want to first
get the user.
So let's say const user and we're going to await on user dot find by ID.
And remember when we're logged in, we're authenticated, we have access to request dot user, so we're
simply going to pass in that logged in user ID.
Then we're going to check to see if the user.
Okay, so if we have a user, then we're going to respond with our Json, which is basically the same
stuff that you get when you authenticate or when you register, which is going to be the ID name and
email.
Now one thing I want to do is let's see, see how we have dot status.
When we authenticate.
I just did res.json, which is fine, but I just want to stay consistent.
So I'm going to do res dot status 200.
Just to stay consistent.
You can you can always just do res.json.
All right now.
Where we're getting the user profile.
If there's a user, we're responding with that else.
Else, then we're just going to set an error of 404 and just say user not found.
So pretty simple.
So we'll save that and we can try it out.
So we'll go over to Postman.
Now, if we're not logged in, obviously this isn't going to work, right?
But if I go and I authenticate.
Let's log in with the Brad user, which is the user I created in the last video when we registered.
So I'll go ahead and authenticate and then come over here.
Let's get the user profile and there we go.
So we get the the user data.
Now, we also want to be able to update the user profile.
So let's do that next.
So down here in the update user profile function.
Let's get rid of that.
And we're going to again, just get the user so we can actually just copy this.
Okay, so we're going to get the user, then we're going to check.
For the user.
And I only want to update fields that.
That, you know, if we send it in the body.
So I want to be able to send just the name and have just the name update.
I don't want to have to send every field.
So what we can do here is we'll say the user dot name.
So the user we got from the database that matches the request, basically the one we're logged in as
we're going to set that equal to the request dot body name.
Or if that's not there, we're just going to keep whatever is already in the database and then we'll
do the same with the email.
All right now.
I want to just check to see if there's anything in the body for the password.
So we're going to say if request body dot password, then we'll set user dot password.
And the reason I'm doing it, doing it like this and not just putting password up here is because the
password is hashed right.
The password that's in the database is hashed, so we only want to mess with it if it's being updated.
So then after that.
Let's say const updated user and we're just going to call user dot save and that will return the user
data which will then put in here.
And then I want to respond with that with that data.
So we'll say res dot status.
200 dot.
Jason.
And then we're going to pass in an object with the ID, which will be the updated user ID, the name,
the email and is admin.
Okay.
And then let's put an else for this if user so else.
Then we're just going to send an error, say user not found.
Okay, So pretty simple.
So let's come over here.
And right now we are logged in.
I'm logged in as the Brad user.
So I'm going to go to, let's see, update user profile.
And I don't have to add any user ID or anything because it's going to know by the cookie by the JWT
that's saved.
But we do have to put in the body what we want to change.
So let's say I want to change the name.
And I'll just do my just my first name.
So let's click send.
And now you can see we get the data back and the name is just Brad, if I want to put it back.
Send.
And now I updated the name if I want to change the email.
Say email.
I'll say Brad t@email.com.
And send that.
All right.
So now we can get the user profile and we can also update the user profile.
Now, our back end as far as authentication is pretty much complete.
Right.
We're able to log in, we're able to register, we're able to store our Json web token in a cookie safely.
We're not storing it.
We're not sending it to local storage or anything like that.
We can we have our admin middleware, so we haven't added any functionality to these admin functions
yet, but you do have to be an admin to even access them.
So now what we'll do is jump back into the front end and start to implement a login form, a register
form and so on.