So all is nice.
We send the cookie from the server.
The browser is happy to send it back, but.
How we can access on the server.
Because again, this is the place where we actually check that docket and only if the token is valid,
then the user can access the resources if it's invalid.
Same like with our JSON response right away.
Return a 401.
Error.
Now, this one is not a straightforward because we do need to install a extra package.
So now let's take a look at the renaming.
Notice we do need to install a package by the name of cookie parser.
Again, this is happening on a server.
And then this is going to be the command.
We want to import that in the server.
JS And somewhere where we have the middleware, we're going to go with cookie parser, we just invoke
it and then we'll right away set up a log in the auth Jess, where I'll show you that just like with
headers, we'll actually have access to all the cookies that we have set up in the log in register or
any of the routes on the server.
So let's try this one out.
So first let me go to the terminal.
I'll stop it here.
I'll clear.
Nope.
I don't want to do that.
And then let's go with NPM install.
And.
The library we're looking for is the cookie parser like so.
So we installed this.
Then we want to spin up again the project.
And we want to import cookie parser in the server js.
So let's navigate to server.
Scroll up.
And similar we have the imports for libraries.
In my case, I'm going to do it right after.
The security ones.
We're going to go with import.
Cookie and parser.
And that is equal to from.
And of course we're looking for the library cookie parser.
Let's keep on scrolling.
And then in my case, I'm going to do it right after Express.
Jason, I'm going to go with that.
That's huge.
And we want to go with cookie parser.
And we want to.
Invoke it.
Let's navigate to the middleware and let's test it out.
So I'm going to go to Auth JS.
And then remember, we're basically getting the request.
And at the moment we're checking, Hey, is the authorization header present?
And only if it's present, then of course we verify it and all that cool stuff.
Now we want to check for the cookies.
And again, in this video, simply log, we'll say.
Correct.
And we're looking for the cookies like so.
So let's say that.
Doesn't really matter which route you use again, because if the token is present, basically if the
cookie is present, then browser will automatically send it back and case I have that.
Susan User So let me refresh on the stats and if I go back to my server, I can see that I have the
token.
So this is what we're logging in the off.
So we have the token here with the value.
Now just to showcase that if, let's say the cookie expires, which I'm going to simulate by basically
removing it.
You'll notice that once I refresh.
I actually have nothing here.
So notice I have here the object.
And that is equal to No.
Again, this is just a showcase that yes, if the cookie is present, the browser will send it back.
But once it expires, it will actually return empty object.
So we'll have no cookies.
And in the next video we'll set up the logic where we won't check for the authorization header anymore.
We'll check for that token cookie.