All right.
And while we're still on a roll, let's also set up a structure for user routes.
So effectively, we'll create three more routes and controllers, one to get the current user.
And that is very important because we're sending back the JWT in a cookie.
So it's not like we're sending back with a request and then we can store it on the front end.
No, we're sending back in a cookie and therefore we need the actual route, which gives us info about,
well, what is the actual user?
Then also we want to set up application stats and this is going to be the admin route.
So we'll take a look how we can restrict access even more.
And also we want to update the user.
So let's say I want to change the name, maybe email and all that cool stuff.
And let's start by navigating to controllers.
We want to create a new one user.
Controller.
Jess.
Let's start by importing few things and then we'll set up our controllers.
So for starters, I'm going to look for status codes.
That's going to be the first one in here.
We'll have two models, user and job since we'll get the application stats.
So therefore let's go with user.
Import then user.
And for some reason it just keeps suggesting this user model.
That's not what I want to do.
I want to go with JS in here and then also we want to grab the job.
So the same deal over here.
Let's go with job.
So those are the three imports.
Now, eventually there's going to be a little bit more, but for now, let's not worry about it.
We want to right away export, as always.
First one is going to be get current user.
And again, this is going to be very typical setup because we're not storing that user info technically
on the front end.
We're just using that cookie to send the token to the front end and then front end right away sends
back.
But we do need to get the info about the actual user, for example, the username, and therefore we'll
set up this get current user, which as a side note, we're going to run every time user navigates to
the dashboard.
Something of course will set up a little bit later.
As far as the response logic, let's just go with the rest dot status.
Let's pass in the status codes and we want to go with Okay.
And yes, I'll set up some messages, but eventually, of course we'll send something else.
But for now I just want to see whether everything works.
So I'm just going to say get current user.
Then we want to select this copy and paste two times and we want to change some values over here.
So for starters, it's going to be get application stats.
So this is an admin route where we can check how many users we have and how many jobs.
Now of course sky's the limit, but that's what I picked for this application as far as the admin.
So we're going to go here with application stats.
Yep, it's still going to be 200.
And then with update user, well this is where we'll change the properties.
So I'm going to go with update user.
And then when it comes to message, we're going to go here with update user.
A quick, quick, quick, quick side note in the auth middleware, you can actually remove this async.
So I just placed it here by mistake.
Then let's navigate to the routes.
We want to create a new file and we'll call this user router.
JS And let's start by pretty much repeating the same steps.
And you know what?
I think it's going to be faster if I just take a look at the auth one and delete some stuff.
Basically let me navigate to user router copy and paste.
So these two lines stay the same.
Let's remove this one and then default router.
Yep, that stays the same.
But as far as the routes, the first one is going to be get.
So this is going to be for current user for application stats.
It's also going to be get.
And then for update user, it's going to be patch.
Then the URL is going to be API version one users and then in order to get the current user.
We're just going to go here with current user as far as the application stats.
I'll add the admin.
Just to signal that this is actually just for admin and I'll call it app stats.
And the last one will be update hyphen user.
So let's set up over here.
Forward slash update.
All right, friend, end user now when it comes to.
The controllers would pretty much want to import, of course, all of them.
And eventually for the update user will also set up.
A validation layer, but not for now.
So for starters, let's start with get current user.
Let's import.
Let's make sure we have a JS over here.
Then we also want to grab the get application stats.
And then lastly, let's go with update users.
So we import all of these things.
Now we just want to navigate to the server.
Let me copy and paste.
And here's the kicker.
We want to place this authenticate user in front of it, just like we do with jobs.
So in order to check.
What is the current user?
Yes, the user will have to log in first.
Same deal with update user.
And when it comes to admin, not only will check for current user but will also check whether the user
is actually an admin.
So I'll place it here right after.
I think it's easier to read and we'll just go with authenticate user first and then we'll go with user
router, which we haven't imported yet.
So user router.
And as far as the URL, you know what, let's go with users.
And now of course we just want to navigate up.
Copy and paste and change this value.
So instead of auth router, we're going to go with user.
And router.
Okay, good.
Let me check.
Whether everything is working in a console.
And now, of course, I just want to navigate to the Thunder client.
I want to make sure that I'm logged in.
Okay, good.
And then let's set up more routes over here.
So these are job routes then?
Auth routes.
So let's create a new one.
Let's create a new folder.
Let's call this user.
Route.
And then.
In here.
Let's start by setting up a new request.
And I'm going to call this get.
Rent user.
Okay, good.
It's a getter out.
Then we want to go with URL.
Forward slash users and then I believe it was current user, so current and then user.
And if everything is correct, we should see.
Yep.
Message get current user.
So now let's copy and paste.
So basically duplicate once and twice or here let's rename first.
Let's call this get application stats.
Or simply application stats.
That's irrelevant over here.
We're looking for users, then admin, then forward slash, then app.
Stats.
Let's send that one.
Okay.
Application stats.
We have that one over here.
And the last one is going to be the patch one.
We just want to rename it.
We're going to go with update user root.
Okay.
Good batch.
And it's going to be users and update user.
Good.
Let's send it on.
If you see 200 for all these three requests, we are in good shape and we can move on to the next step.