User Login
We'll need to...
Install dependencies,
passport
andpassport-local
Configure Passport to use our user model
Initialize Passport to use our session module
Add login and logout functionality to the
auth
controller
Passport and passport-local
From the passport website
Passport is authentication middleware for Node.js. Extremely flexible and modular, Passport can be unobtrusively dropped in to any Express-based web application.
Passport has a lot of different strategies for authenticating users—github, linkedin, and more! (Passport website lists 502 different strategies!) We're using the Local strategy.
This module lets you authenticate using a username and password in your Node.js applications. By plugging into Passport, local authentication can be easily and unobtrusively integrated into any application or framework that supports Connect-style middleware, including Express.
Install passport and passport-local
We'll use Passport in order to provide login functionality, and passport-local
in order to provide local user authentication.
Configure Passport to use our user model
Create the Passport configuration inside of the config folder. This will be a javascript file
config/ppConfig.js
Initialize Passport to use our session module
Now that we've created the configuration, we need to make our app aware of its existence. This can be done by requiring the configuration and including it as middleware.
index.js
IMPORTANT NOTE: You must include the passport configuration below your session configuration. This ensures that Passport is aware that the session module exists.
Add login and logout functionality
Before continuing, verify that this test is passing
Auth Controller - GET /auth/login - should return a 200 response
Login
Luckily, all of that configuration and middleware means a straightforward login route. Let's go ahead and add the POST route for login.
controllers/auth.js
This should pass the following tests
Auth Controller - POST /auth/login - should redirect to / on success
Auth Controller - POST /auth/login - should redirect to /auth/login on failure
Login after Signup
Ideally, we want to already be logged in after signup. We can modify the signup route to call the passport.authenticate
function again. Note that we'll need to call it as an IIFE, passing the request and response.
controllers/auth.js
Logout
Including the Passport configuration in our app means that logging out is really really easy. You can now call a function attached to req
to log out. Let's implement the final route.
controllers/auth.js
This should pass the following test
Auth Controller - GET /auth/logout - should redirect to /
Login/Logout Finished
Congrats, your login/logout functionality should be finished! Verify by running the tests. You should have 17 passing and only one failing.
Now for one more section...
Last updated