- [Instructor] We now have all
the authentication logic set up.
Next, we need to create the routes to let users log in.
If we look at this site,
we see that there is already a login link under Account,
but if we click on it, we see that there is an error
because the route for it does not exist yet.
So back in Visual Studio Code,
I open server, routes, users, index.js.
And there, right on top, I add router.get,
and the route should be on the slash login,
request,
response,
and we will render form,
res render.
And this form exists already if we look into
views, users.
There is already a login form.
So back in index.js,
I now simply have to render users
slash
login.
Let's reload the application real quick,
and let's reload the form,
and we see now we have this login form.
This login form will now post to slash users slash login,
and we will implement the post route for it now.
And for the authentication, we will now use passport.
And for that, I will bring in passport
by adding const passport equals
require
passport.
Very much like Mongoose, passport maintains
a module frugal state.
So everything we configured on it, like the strategy,
will be available on the imported module as well.
Now for the post route I will add
router.post,
slash
login,
and now we use this passport
middle var
passport.authenticate,
and its first argument, this middle var
picks which strategy to use,
and we will use the local strategy here.
In a second argument, we can pass in
the configuration object,
and there we can set what to do
in case of a successful or failed login.
And in case of a successful login,
I want to redirect the user,
so I add the configuration property
successRedirect,
and this should lead the user
to the index page.
Similarly, there is a failure redirect
configuration option,
and there I want to send the user
back to the form users slash login,
and I want to add a error variable
to the very string,
so error should be true.
Now for the form that we render here on line 12,
we want to also pass in this error state,
so I will now
add a form variable error,
and it comes from request.query.error.
If we looked at the login form
template real quick, we see that
if the error variable is set,
the user will get an alert.
Now it's time to try this out.
First we will simply run a test.
So I run NPX mocha
test/server/routes/users/
and there's an index.login.test.js,
and let's run this real quick.
And we see that we can successfully
log in the user.
Now let's also try to fill out the form.
So I start the application again.
On the form, let's first try to use
wrong credentials,
and login.
And as expected, we get an error message back.
Now let's use the correct credentials
that we created before when registering.
And we got redirected to the index page,
and we also see that the user
is now set as template variable,
because we now have different navigation
that shows the username on the top right,
and also new navigation options,
My Account, and Logout.
In the next video, we will implement
this logout route.