Okay, so now we're ready to work on our back end authentication.
And just like we have a product route and a product controller, we're going to have a user route and
a user controller.
And I think what I would like to do instead of just creating them as we go in this video, I just want
to create all of the routes and controller methods and for now we'll just have them respond with just
a string or something.
But as we go, we'll add our functionality to them, if that makes sense.
So I just want to basically have a skeleton, just a structure of our routes and controllers first.
So let's go ahead and go into routes in our back end and create a new folder called User Routes Dot
JS.
And then we're also going to create a new controller called User.
Controller.
Dot JS.
And I think the best thing to do here would be to just start out with.
Let's let's start off with the controller functions.
Now, just like with the product controller, we're going to use the async handler because we're using
mongoose methods inside of here.
And then we're also going to need the user model, which we already created.
So if we look in models, we should already have your user schema and be exporting your model from here.
So let's I'm just going to copy from the product controller.
These two imports, I'll grab one of these routes as well.
And then in the user controller, we'll paste that in and we're just going to change this to user and
we're going to bring in the user model.
And then let's see for the first route here, I want it to be the authentication or the login.
So for the description, let's change that to we'll say auth user and ultimately it's going to get the
token.
And the route is going to be slash API, slash users slash login.
And that's going to be public, obviously, because you're in the process of authenticating.
And then let's call the function name, we'll say auth user.
And we're using the async handler.
And for now, all I want to do get rid of these lines here.
All I want to do is respond.
We'll say rez dot send.
And let's respond with just a string that says auth user.
So just whatever this whatever the function does and then we're going to do the do this for the rest
of the functions in this file.
So let's copy this.
And the next one I want is the registration.
So for the description we'll say register user and it's going to be a I'm sorry, this one right here.
This needs to be a post request because we're submitting.
Same thing here.
So this will be a post request and it'll be to just slash API users because we're creating a new user,
right?
We're creating a new resource and then the name of the function, let's call it register.
Register user and we'll just have a string of register user for now.
Okay.
Next one is going to be log out.
So let's see.
This is we're going to say this is going to log out the user and.
It's also going to clear the cookie because we're going to be storing the Json web token in an Http
only cookie on the server.
So we need to destroy that.
It's not just a matter of clearing local storage in the front end.
So this as far as the route, this will be a post request to API users and then log out.
And this is going to be private because the user is going to obviously be logged in if they want to
log out.
And then let's call this log out.
User.
And just change that to log out.
Okay.
Next one is going to be to get the users profile.
So.
Let's say, get user profile and it's going to be a get request this time.
So let's change this to get and it'll be API slash user slash profile.
And as far as the function name, we're going to call it get user profile.
And for this we'll just say.
User profile or get.
Get user profile.
Now, we're also going to have a route to update the user profile.
So actually I'll just copy this one.
And let's just say update user profile.
And of course this will be a pull request because we're updating data.
So it will be a pull request to API user's profile and it will be private and so will this one.
So that'll be private.
And let's change the name of the function here to update users user profile and change this.
Okay.
And then we're going to have one to get all users.
And this is going to actually be an admin route because we don't want regular users to be able to get
other users information.
That's an admin and admin action.
So what we'll do is here, we'll say private slash admin and it's going to be a get request to API users.
And this is going to we'll just say get.
Users.
And the name of the function is going to be get users.
We'll change this.
Okay, so that's that.
Now we also want to be able to delete users.
That's also going to be an admin action.
So let's say delete users and it will be a delete request.
And when we delete a user, of course we're going to need the ID, so we're going to say colon ID.
And by the way, the update profile.
Notice that here we're not passing in an ID because we're going to use the token because obviously this
this is for the user to update their own profile.
So they only have access to their own user data and that's going to be encoded in the Json web token.
So no need to pass an ID into here.
So let's see.
Delete users.
Let's call this.
I delete do delete user.
Okay.
And then let's just here, we'll just say delete user.
All right.
Now, we also want to be able to get the user by the ID.
Uh, we should probably put that right under this one.
Because that one's to get all users.
But this one will say get user by ID and the request.
Let's see, it's going to be a get request and then API users and then colon ID, it's going to be an
admin function and then get user by ID for the name and we'll just say get user by ID.
All right.
And then the last one is going to be update user.
And this is where the the admin updates the end user.
So that's going to be a put request.
And that's going to be to API users and then colon ID.
And let's change this to update user.
Change this.
Okay.
And I think that should do it now.
We do have to export all these.
So let's say export.
And we're going to pass in here let's say auth user we want.
Register user.
Log out user.
Get pro or get user profile.
Update User profile.
We want get users and delete user.
What else?
Get user by ID.
And update user.
I think that should do it.
Okay, Now what we want to do since we have all these functions is we want to go into our routes.
So we want to go into user routes and basically just connect those controller functions to, excuse
me, to our user routes, just like we did here in the product router.
In fact, I'm just going to copy this.
And because we do want to bring in Express, we want to bring in the router now, we want to bring in
our functions from the user controller and get rid of that.
And we want to just bring in all the ones we exported.
In fact, what I'll do is go to the user controller and just copy all of these.
And then we can just paste those in here like that.
Okay.
And let's see.
Actually, I'm going to put the router.
Now, that's fine.
Now down here where we create our roots.
So we'll keep this so router dot route and slash, which is going to be just slash API slash users.
If it's a get request, then we're going to do get users and ultimately that's going to be an admin
function, an admin route.
So we're going to have to add middleware to make it so only admins can get users if it's a post request.
So let's do dot post.
If it's a post request, then that's going to register a user.
Now for let's see, let's do the log out.
Um, yeah, we'll do the log out next.
So let's say router dot.
And here, since we only have one method to log like we only have a post request to log out, we're
not doing a get or put or whatever.
So instead of doing router dot route, let's say router dot post to slash log out.
And we don't need let's see, we don't need to have this so we can get rid of that.
Since we're doing router dot post, we just put in the root and then a comma and then whatever function
we want to fire off, which is going to be log out user.
And then for log in, we're going to have a post request to log in and that's going to call log in user.
Wait, is it locked?
No, it's auth user.
If you want to call it Loginuser, that's fine.
Just make sure you change it in the controller.
And then let's see for the profile route we do have we do have a get and a put, so I'm going to do
router dot route for that.
And let's say router dot route on.
So we're doing the profile.
So yeah, we'll do slash profile and if we do a get request to that, then it's going to call the get
user profile, right?
And then if it's a put request, then it will be the update user profile.
Now to delete the user, to get the user by the ID or to update the user, We have to we're going to
have this in the root.
The colon ID So for this, let's say router dot root.
And let's say if it's slash colon ID.
Right.
And remember this, all of these are connected to slash API slash users, right?
So this is actually going to be slash API slash users.
This will be slash API slash user slash logout and so on.
So for the ID, let's do if it's a delete request.
Then we're going to call delete user.
If it's a get request, then we're going to call get user by D.
And if it's a put request, then we're going to call update User.
Okay, so those are all of our user routes and then just make sure you export the router.
So now what we can do is go into our server.js in the back end and let's bring in the user routes just
like we did product routes.
So change that to user and then change this.
Two user routes and then we'll just add that down here.
Copy this down.
So this will be user and this is going to be API slash users.
All right.
So now, since we did all that, we should be able to go to Postman and test some of these out.
In fact, we can add some.
Let's add a new folder here.
So this is in collections and I'm going to just add a new collection and let's call this users.
Okay, so any requests that have to do with users will go here.
So let's say add request.
And let's start with the authentication.
So we'll say auth user or login user, however you want to say it.
And what this is going to be is a post request to excuse me to base URL.
So we want to do our base.
Base URL slash API.
Well, actually API is included in the in that base URL.
So we just have to do slash users.
And if it's a post request to that and we send, we get okay we get registered user if it's a post login
is actually slash login.
Or auth.
All right.
And we can save this.
So I'll click save.
And now under our users collection we have our auth user row.
Now, when we're ready to actually use this, we're obviously we're going to send some data in the body
because we need a user, an email and password to log in.
But I just right now we're just testing the routes.
So let's create another request.
So we'll say add request and this one will be for register.
User which will be a post request to.
Base URL slash users.
Just slash users, nothing else.
And that should be our register.
So we'll go ahead and save that.
Okay.
If we do, let's see.
Ad request.
Let's do, um.
Let's do the log out.
So that will be, let's say log out user.
That will be a post request to.
Base URL slash user slash logout.
Send that log out user will save.
And let's add another request.
This one is going to be.
What else do we have here?
We can do the get profile.
So we'll say get user profile.
That's going to be a get request to base URL slash user slash and then profile.
Okay.
And as long as you're seeing a 200 response with the text here, then it's working because we didn't
add any other type of functionality.
So that's get user profile.
Now let's do the update user profile.
So add request.
And again, you don't have to do this like save all of these.
I'm just doing it because it's I like it to be organized so that I can just go to a tab and I can just
make the request.
I don't have to keep opening new tabs and closing them and typing in new URLs and stuff like that.
It's just it's just being organized.
So we'll say update user profile.
That's going to be a put request to base URL slash user slash profile.
Send that and we get update.
Save it.
And let's see.
I think that's all of them except the admin stuff.
Yeah.
So now let's do the admin.
And you could.
Could create a new folder.
Called admin just so we can.
You know, distinguish which ones are admin and which ones aren't.
So in admin, let's add a request to get all the users.
So that'll be the base URL.
Slash users.
And it's a get request.
Send it.
Okay.
Now, again, when when we actually have our functionality and we have our authentication set up, we'll
have to send a token in the cookie to actually be able to access this.
But right now it's all open because we haven't added that, that middleware, we haven't added that
authentication yet.
So let's save that.
And then let's see.
We'll add another one.
To let's do the get get the user by D, So I'm just going to name this.
Get user by ID.
And that'll be a get request to base URL.
Slash users.
And then we need the ID, so we're going to put colon ID here.
And then you'd put the value of whatever ID you want to get, whatever user.
So for now.
What I'm going to do is open up my products.
And let's see, we'll get our products.
And there's a user right here that I'm going to grab.
Okay.
And I'm just going to put that in there.
It doesn't matter right now what we put in here, because again, we're just returning a string.
But for later, at least we'll have a user's ID in there.
All right, save that.
Okay.
And then we're almost there.
So the next one is going to be the delete.
Oops.
We're going to do new request.
Say delete user.
It's going to be a delete request to, let's say, base URL.
Slash users slash and then the ID.
So once again, colon ID, and then I'll just paste in an ID here.
Send.
Test it out.
That works.
We'll save it.
And then finally, we want the update user.
So in admin I'm going to add a request.
Update user.
Port request.
Base URL slash user slash and then ID.
Paste that in send update user.
All right.
So we know that all of our routes are working up to this point, even though we're just returning a
string.
At least we have them set up.
We have our controller function set up.
So now we're ready in the next video to start working on each route.
And I want to start off with the, the auth user.
So if we go to the controller, this first one right here, I want to start on the login.