In this video we're going to take a few moments to talk about how we can better secure the user profile
data we're sending from these server now to illustrate what we're going to change.
All we need to do is log in as one of our users.
So over here I have my log in user request.
I'm just going to fire this off a single time and down below we have our response body.
Now we have our two root properties we have user and token and both of those are going to stay.
There's nothing wrong with sending them back.
The problem is what data we're sending back about the user for example we're sending back the password
which is not something we want to do.
Passwords should be secured to the utmost extent and exposing them is never gonna be something we want
to do even if it's to a logged in user.
There's no reason they would ever need this data as it's hashed and it's not useful to them.
So it's best to lock that down.
Also the tokens are right.
There's no need to expose all of the authentication tokens with the user profile.
So it's gonna be our job to figure out how we can hide both of these when we're sending data back to
the client to get this done we'll be making a few changes to the application and let's go ahead and
start by scrolling up in the user router to the log in request both.
Log in and sign up.
They send back user and token we can see that here on line twelve and down below on line twenty two.
Let's focus on changing them to just send back the profile data we actually want to expose.
We're going to look at two ways to get this done.
So let's start with the solution that uses skills we already have right here.
We're going to set any user property equal to something else instead of using that shorthand syntax.
What we're gonna do is set the user response equal to whatever comes back from the following function
call user dot get a public profile.
Now get public profile does not exist but we could go ahead and create that function and set it up to
just return the public data that we should be exposing about the user.
So over inside of the user models file we can go ahead and add a new method on now down below we have
user schema dot methods for methods on the instance and individual user.
And we have user schema dot statics for methods on the actual uppercase U user model.
We want one on the individual instance right here we can set that up.
It would be user schema dot methods dot right here get public profile exactly as we had set it up before
and we can set this up as a standard regular function.
Once again we're gonna use the this keyword inside of here.
So we don't want to use an arrow function.
The first thing we can do inside of here is create a constant user and get its value from this like
we've done down below.
And this just makes our function a little easier to work with.
And the next thing we're gonna do is get back a raw object with our user data attached.
So this is going to remove all of these stuff that Mongoose has on there to perform things like these
save operation we want back and object with just our user data.
Right here we'll create a another variable I'll call this something like user object and we'll get its
value by using the two object method which is provided by Mongoose.
So that's user dot to object.
This is gonna give us just that raw profile data and then we could go ahead and return it.
So I'm going to return user object.
Now this isn't going to do anything special.
We've essentially recreate the same behavior we've had before.
But with more code in place and a second though we'll be able to alter it to get what we want to get
done right here.
If we go back to postmen and we log in again we can still see everything we were seeing earlier in the
lesson.
But now we can actually manipulate User object to change what we expose.
So right here what I'm going to do is use the delete operator to delete stuff off of that object such
as user object dot password and then down below I will delete user object dot tokens removing both of
those from the object that we send back as the response.
Now with this in place we can head back over to postmen we can fire off the exact same log in request
and this time we no longer see that data.
We're still getting back token and user and users still has the data we actually want to share with
the individual user who's logged in and it does not contain things like token and password.
Excuse me tokens plural and password which the user does not need to access.
So now that we have this in place we have a pretty manual way to get the job done.
And it's manual because we have to call our function get public profile every single time we're sending
the user back.
There is a way to automate this.
That's not going to require us to make any changes to our route handlers.
That's the second solution and we're going to touch on that now.
And it really requires just a small tweak to what we've already done.
So here's what we're gonna do.
And I'll explain why it works with an example in isolation in just a minute we're going to remove the
method call that we just set up.
Now since we're removing it we can go back to using the shorthand which means that so far in this lecture
we haven't made any changes.
This is exactly what we started with.
We're going to save that file as we're not going to change it at all.
Now over inside of the user model all we're going to do is rename this method.
We're going to change the name from get public profile over to to capital Jason.
And it's important that this matches up exactly lowercase t o capital J.
S o n.
Now what I'm gonna do is save the program and we're going to head back over to post man and test things
out right here.
I'm going to lie again and we can see that things are still working.
We no longer have the password and we no longer have that tokens array.
All we've done is we've created this to Jason method on the user.
We haven't changed the user router at all.
Now this means we have the exact same behavior any other time we are sending the user back.
For example when signing up or when getting their profile.
So if I go over to read profile without having made any changes to how data is sent back you can see
right here that the password and the tokens array are not there anymore which is fantastic.
Now the question is how exactly is this working.
What's so special about to Jason that allows it to run even though we're never explicitly calling this
to figure this out.
Oh we're going to do is head to index dot J S in here down near the bottom of the file.
We're just going to add a little code to mess around with an example.
Now we already have an example from earlier in the class when we were messing around with Jason Webb
tokens we can remove that.
So the last thing in the file is our call to app.
Listen now we're going to figure out what exactly that to Jason method is doing start by creating a
very simple object.
I'm going to set up a new constant I will have the variable name and be pet.
It's gonna be an object and we're just going to setup the name of our pet.
My cat's name is how so from here.
What we're gonna do is string a ify this object.
So we're going to convert it from an object over to Jason and we'll log of that Jason to the terminal
down below.
So console log we'll use the method we've used before to get this done.
That is Jason dot string Fi and what are we going to do.
We're just going to pass in the object.
We're trying to string ify.
Now if I save the program and reruns down below as expected we get our Jason string.
We have our Jason with the name property where the value is how and we can see that we have Jason and
we have those double quotes wrapping all properties.
So this is exactly what Express is doing when we pass an object to response.
Dot send it's calling Jason dot string a.
Behind the scenes.
So right here when we pass our objects off to response dot send they're getting string RFID and the
same is true any other time we get that done such as when we send the profile back down below.
Now let's go ahead and bring two Jason into the mix to see how that impacts the behavior when we set
up to Jason.
It's gonna get called whenever that object gets string a five.
So right here we're going to set up Pat dot to Jason and we're gonna set this up as a standard arrow
function.
Now right here we have access to a this value which is the object itself.
So right here console dialogue printing this.
Then down below we'll go ahead and return this to see what behavior we get.
Right here I'll be saving index dot J S down below.
I have my object with the name property and to Jason function we just defined.
And then when it comes to the output right here I can see that I just have name the Jason is exactly
like what I had before.
The nice thing though is now we can manipulate what exactly comes back when we string a fi the object
my returning an object from here.
So what I could do for example is return this to keep things the same like we're doing now or I could
return an empty object to have none of the data get exposed.
So right here we can see that down below the Jason is completely empty.
There are no properties because to Jason sends an object back with no properties attached.
This is exactly the sort of thing we're doing in our Express application when we use response dots and
Jason string if I gets called on the user we've set up a to Jason method on the user where we manipulate
the object sending back just the properties we want to expose and the end result is that we no longer
see password or tokens any time the user gets sent back to the client now that we have this in place
we're no longer in any danger of exposing user data that we didn't want to expose we can go ahead and
remove the little example we had down below save index dot J S and that's where we're going to stop
for this one we'll be continuing on with the other routes inside of the user file in the next video
let's jump right into that.