All right.
And once we have placed the authenticate middleware in front of all of our job routes, now let's set
up a package which is going to allow us to access the cookie.
And also we'll right away set up our first check in the authenticate user.
Now, like I showcased before, yes, we can see the cookie in the request, but we cannot access it,
unfortunately, right out of the gate.
So we do need to install an extra package.
And that package name is surprise, surprise, a cookie parser package.
If you want to use it in your own projects, go with NPM install or I, then cookie parser and then
we want to navigate to a server import and we'll set it up as a middleware.
So let's do that one first and then we'll take a look what we can do in the authenticate user.
So here in the server, let's scroll up.
We're looking for import and I'm going to name this cookie parser.
Then let's keep on moving.
And right over here where we have express Json, I'm also going to use the cookie parser.
So essentially we just want to invoke it.
Now with this in place we want to navigate to auth middleware.
Let's just start by logging the rec dot cookies.
So effectively once we set up the library as a middleware, we'll have access to the cookies property
and more specifically, our cookie.
Now remember when we were creating the cookie and of course we can see that in the auth controller,
What's the name that I used?
Well, in my case I went with Token, so there should be that token cookie.
If it's not present, then we want to throw our own custom error.
So for starters.
Let's change things around and let's go with rec and we're looking for cookies.
So that's the property.
The library provides.
And again, same deal.
We're going to navigate to our get all jobs.
Over here.
And once I make the request.
Check it out.
So this is my token property and this is actually a cookie.
So the first check is going to be following where essentially I want to see whether token cookie exists.
If it doesn't exist, then of course we'll send back the error response.
And in this case, it's going to be unauthenticated error.
So one of the custom errors we created before.
So let's navigate back to auth middleware.
In this case, I think I'm going to the structure.
I'll say that I'm looking for token cookie.
It's located in rec dot cookies.
And if it's not present so if there is no token cookie, then we want to throw our own custom error.
So we're going to go throw new and then we're looking for unauthenticated error.
And this is going to be 401.
And as far as the message, let's go with authentication.
Invalid over here.
Now, if everything is okay, of course we'll pass it on to the next one.
Now, as you can see with the auto import, the JS is missing, so now everything is correct.
So again, let's navigate back to get all jobs.
Let's take a look at the cookies.
And, you know, I'm just going to clear all of them.
And now let's make a request one more time and let's see what is going to be the result.
So notice since I removed all of the cookies, now I'm getting back 401.
However, if I'm going to go to auth routes.
And if I'm going to log in one more time.
Notice now I'm getting back.
And for some reason, it's complaining.
Oh, yeah, because I didn't create the user.
My bad.
So basically I removed everything from database.
So that's why I have this error.
So first let me go, then back to a register.
Let's start over here.
So I'll keep these values over here.
And again, it doesn't really matter since it's going to be the first one, it will automatically be
admin.
Let's send it.
Okay, so we created the user.
Now we're going to log in.
We're going to get the cookies.
Notice I have my cookie over here.
So now of course I can go to a get all jobs and again, I'm still going to get back an empty array.
That's not the point.
The point is that now I successfully can access the jobs because I have the cookie.
Again.
If I'm going to remove that cookie, it's going to be different.
Then of course we won't have access to the specific route.
In this case, the get all jobs.