All right.
And once the authenticate user middleware is in place.
Now let's use the user ID to get only the jobs that belong to a specific user.
And also, when we create the job, we provide the created by property which we set up at the very beginning
of authenticate user videos.
So first of all, let's navigate to job controller.
Like I said, any of these controllers will have access to that user ID.
So in here for get all jobs, I'm not looking for all of the jobs.
In fact, this is something that only admin is going to be able to see later on.
So in this case, I want to change this around.
Remember, this is a filter object and effectively I want to go with created by.
So that's the property and I want to set it equal to rec dot user user ID.
So as a result, when the user makes the request with a cookie with the token, we only provide jobs
that belong to that specific user.
And not only that, but also when we create the job, of course we need to provide that property.
Now there's million ways how we can set up, but a one liner is following where essentially I have req.body
in there I have the user with the user ID correct, So I can just attach one more property the created
by.
Correct, because that's what the job model is looking for.
And then we're going to go with req dot user and then user ID.
So essentially, since we're passing here, the erect body, I'm just attaching this created by to the
object and I'm setting it equal to the user ID.
So as a result, if we are going to go right now to our Thunder client, and let's just start by creating
a job.
So this will be the company position and all that.
Let me send it over here.
And if everything is correct, I'm also going to see the created by.
So, of course, this is going to be the ID for the user, John, Basically for my first user.
And if I'm going to change this around and if I'm going to go with Apple and.
Let's go DevOps over here.
Then of course, we'll create this job.
And now if I go and check get all jobs, I should see two of them.
Now, of course I have over here cookies.
So let me go to response and check it out.
I see these two jobs now if I'm going to register another user.
And you know what?
I'm not going to be particularly original.
I'm just going to go with Peter and Peter.
Let me send remember, first we need to register.
Okay.
User created.
Then we want to log in.
So this stays the same.
The password and all that.
If Peter makes the request to get all jobs, notice nothing.
Empty array.
So, yes, it's a valid request, but Peter hasn't created those jobs.
So of course, in order for Peter to see any jobs, well, he needs to create few.
Correct.
So let's go over here with Facebook and we're going to go with designer.
So for now, I'm just going to create one job and check it out.
Now, of course, I have this one job, and if I go back to all of the jobs notice now, of course.
I can see only this one job.
So again, if John logs in.
This might be a little bit redundant, but I just want to make sure everyone is on the same page.
If John logs in, if I get the cookie where I have John's credentials, then of course, again we go
back and now we see two jobs.
And again, all of this is happening because we're sending back the cookie with JWT and by default browser
is going to be sending back and essentially on the server.
We'll take a look at the cookie, take a look at the token.
If everything is correct, then we'll proceed to a specific route.
And for example, if I want to delete the job, of course I just need to grab the correct one.
Over here.
And pass it.
In the URL, so I sent the up job.
Delete it.
So now John also has only one job and with this in place we can move on to the next step.