All right.
And up next, let's set up, get current user root and effectively the functionality is going to be
following.
We're going to use user model.
We'll use find one.
And based on the ID, we're going to grab the user.
Now, of course, you can also search right away based on the ID, but I just want to showcase that
of course we have multiple options, so let's try it out.
There's going to be one gotcha and we'll work on it once we have this functionality in place.
So back in the controllers, pretty straightforward.
In the user controller, we just want to look for a user whose ID matches to the one we have in the
req dot user user ID.
So I'm going to call this user.
We're going to go with Await.
We already have the user model.
Then let's go with find one.
Like I said, we can also use find by ID, but I just want to showcase that we have multiple options.
And basically in here I'll say, get me the user.
Whose ID.
And remember, when Mongo creates the ID, the property is following its underscore ID and set it equal
to user user ID.
Now, how do I know that it's there?
Well, because remember, we placed authenticate middleware in front of it and now let's send back user.
And for now, let's set it equal to a user.
And this is where you'll see the gotcha.
So now let's navigate back to the user routes.
We're looking for current user.
Let's send it.
And if everything is correct, I should get info about the job.
Now, if I'm going to go to a login and if I'll change it around and if I'll say, Peter, what do you
know in the get user?
Of course I'll get the Peter value.
Now there's a tiny typo over here as far as the name, but we'll actually fix that once we start working
with update user.
So pretty much this will always.
Return, which is the current user.
Again, something very important because we're not storing that user value anywhere in the front end
as far as when we log in.
So that is just stored in a cookie and then JWT, hopefully that is clear.
And now let's also fix the gotcha.
Notice how we're sending back the password.
And even though, yes, it's hashed, we still don't want to send back the password.
And effectively we have multiple ways how we can fix this.
And I'll show you a instance.
Method approach.
Now what am I talking about?
Well, I'm going to go to models, more specifically user model.
And then right after my schema I can go here with user schema.
So the same one we created then methods and then I need to come up with a name.
So effectively when I get back, my user, for example, in here.
But just please keep in mind, of course it works in any controller where you're getting back that instance.
On that instance, I'm creating a method.
Now.
There are also static methods which are available on the entire model, but this is not it.
So again, we're talking about the instance method and we need to come up with a name.
So in my case I'm going to go to Json.
Now you do want to go with good old function keyword.
Because inside of the function we'll use this.
So if you don't use this, then technically it doesn't matter.
You can go with arrow function syntax as well.
But if you're using this, which is going to point back to the instance, then yes, I strongly suggest
using the good old function keyword.
Otherwise the functionality is not going to work.
So what do we want to do over here?
Well, first I'm just going to go with let an object and I'll set it equal to this.
So again, I'm talking about the instance, the user instance that we're getting back, and we're just
going to go with two objects.
So we will transform our user to the good old JavaScript object.
Then we'll use the delete keyword and I simply want to remove the password property.
And at the very end of the day I just want to go with return object.
And essentially once I run it, I'll get back the user without the password.
So let's navigate to user controller and essentially right after we grab the user based on data, let's
come up with a variable.
In my case, I'm going to go with user.
Without password and we'll set it equal to user.
And since we just created that instance method we want to go with to Json and we want to invoke it.
And now instead of sending back the user, we'll set it equal to user without password.
And if everything is correct when it comes to a current user, I'm going to get back the user without
the password property.