All right.
And once we can send back the cookie with the Json web token.
Now let's set up a property on the job schema, which is going to point back to the user.
So essentially in the process, whenever we create a job, it's actually going to be tied to the user.
And also I want to set up the auth middleware.
Now what do I mean by that?
Well, when the requests are going to be coming in, I'll be checking for the cookie in there.
There's going to be a JWT.
So Json web token and in there we'll get the ID So if the cookie is valid and if the token is valid,
then the request is going to go through.
If not, then we'll send back the response.
So essentially we will lock down roots for jobs.
So there will be authorized routes.
So essentially you cannot just randomly send the request and do something with the jobs.
No, you need to log in first.
And we're going to start by navigating to models.
We're looking for job model and we're going to create another property.
So the name is really up to you since there is Createdat and Updatedat I'm going to go with created
by and this is where I'm going to point back to the user and the syntax is following.
We're going to go with type and here we'll set it as mongoose types and then dot and object ID.
So essentially I'm saying that the value over here is going to be the Mongo ID, And then as far as
the reference, we're going to point to a user.
Let's save it.
This is why it was so important for us to start everything from scratch as far as wiping out everything
from database.
And now, once we are done adding this property, we want to go to middleware and we want to create
a new one.
In my case, I'm going to call this auth middleware.
DJs in here.
Let's just set up a function in there.
There's going to be a log.
And first we'll just place that function in front of all the job routes.
So let's go right away with export.
I'm going to call this authenticate user.
It's going to be async.
Rick RAZ next.
Remember, when it comes to middleware, if we invoke next, then essentially it's just going to pass
to the next middleware.
If not, then the request is going to be stuck over here.
Now, of course, eventually there's going to be a condition, basically, if everything is okay, yeah,
then we pass it on to the next middleware.
If not, well, then that's where we send back the error response.
And for now, again, I just want to log it.
I just want to showcase how the routes are going to be eventually protected.
And for now I'm just going to go with Auth.
Middleware.
Let's save it.
And now let's decide where we want to place this middleware.
So if I want to protect.
All of the job routes where I'm going to do that.
Well, one way is to go to a job router.
Correct.
And essentially, in order to protect any or all of the routes.
It's pretty straightforward.
You just want to place the auth middleware in front of the controller.
So basically, if you want to just lock down one route, get all jobs, you are going to place the auth
middleware in front of get all jobs.
But remember in the server.
We have Joe Browder here.
Correct.
So if I place auth middleware in front of the entire job router, then I'm automatically locking down
all of the job routes.
Now, please keep in mind that of course you can go back to a job router and add to all of them one
by one.
This is just a little bit faster.
So let's navigate to a server we want to import.
In this case I'm looking for the auth one and I think I'm going to copy and paste since that way I don't
need to type auth middleware.
So let me remove.
And this should be auth middleware.
JS.
Let me see.
I have some kind of bug over here.
Oh, yeah.
And the reason for that is because, of course, in the auth middleware I'm exporting authenticate user
and it's a named export.
So let me go back to the server.
We want to change this around and we're going to go with authenticate user.
Then let's scroll down.
And again, these ones are going to be public routes.
So, of course, in order to register and log in, we want to let all of the users do that.
Correct.
But.
When it comes to jobs, these routes will be protected.
So in here, like I said, we simply want to place authenticate user in front of the job router and
now take a look at the result.
If I'm going to navigate to my collections, we're looking for job routes in this case.
Now, I'm not going to test with each and every route, especially since we haven't set up the functionality
to pass the user.
Because remember now that we added that property, of course we want to provide it and I'm talking about
the created by property.
So I'm just going to showcase with get all jobs, check it out, I send it and notice.
So this is empty.
Okay, that's fine.
What I'm looking for is this log.
So this means that pretty much any request we're going to make as far as jobs are concerned, we will
need to go through this middleware and this is where we'll check for the cookie for the JWT and all
that cool stuff.
And with this in place, we can move on to the next step.