- [Instructor] So we need to add our routes
for registration and log in,
but also make sure we add the functions
that will check for a logged in user before proceeding.
So let's go ahead and work on this.
For this particular code
we're going to need to work in the routes.
So go into the routes folder and then open the crmRoutes.
And the first thing we're going to do is import the functions
that we've created inside of the userController.
So let's go ahead and do import
and then what I'm going to do is do the from first,
from dot dot controller,
or controllers, userController,
like so, and then what I can do is use that
to import the functions in question.
So for example we're going to have access to login.
So if I type L I'm going to see login, and then register,
and then loginRequired,
and these are the functions that we need.
So once we have these functions
we can pass them to the routes.
So what we're going to do is pass the loginRequired
to all the routes.
So before they actually execute the controllers
for getContacts and getContactWithID
it's going to execute the function loginRequired
that we just created.
So let's go ahead and add them.
So I'm going to basically click
and option click on every element
where I want to add that function, like so.
And then all I need to do is type loginRequired
and then comma, and you're good to go.
So we have now this function that will run first,
check that somebody's logged in,
and then run the function of getting the contacts
and the same with every other function
that is in this particular route.
Now, we also need to create the new route.
So remember, our users need to register first
and then they also need to be able to log in.
So we need to create the routes for those.
So what we're going to do
is inside of the routes function here,
we're going to add an endpoint
which is the first one will be the registration route.
So we're going to create an app.route like before.
And we'll name this route
forward slash auth forward slash register.
So this is the route that we're going to need to use
from Postman to register.
And then we're going to use the function register
that we created in the controllers
to register that new user.
So if you can remember this is this function
that we created here, the register function.
All right, so we also need to create a route for the login.
So let's copy line 33 and line 34,
paste it below, like so.
And as opposed to auth/register we're going to do auth/login
or you could just do login if you want to,
so let's go ahead and do that instead.
And then pass the right function
which is the login function, like so.
Okay, so now we've created our route for logging in,
so login route.
We have a new registration route,
and we pass the loginRequired function
on all the other routes.
So once we're logged in we're going to get a token
from the function,
and then we can use that token
to actually do the other functions here.
So I'll add new items to our database, update them,
get all the contacts from our database
and so on and so forth.
Okay, so let's quickly take a look,
just to make sure we haven't made any typos,
or any big code errors.
And I can see right away
that there's a dot missing on the post,
and this one as well,
and then we can save this.
And quickly check, I think we're all good.
Perfect.
All right, so now that we have all the new routes set up
and the loginRequired added to our post
and all the other endpoints, we're good to go and move on.