Implementing authentication
A common scenario for web sites is an privileges area that requires a user to identify themselves via authentication.
The typical way to achieve this is with sessions, so in this recipe we're going to implement an authentication layer with our Express server and in the There's more... section we'll do the same with Koa and Hapi.
Getting ready
Let's copy the express-logging
folder from the previous section and name the new folder express-authentication
, we'll also need to install express-session
and body-parser
:
$ cp -fr ../adding-logging/express-logging express-authentication$ cd express-authentication
How to do it...
We're going to need the body-parser
module (so we can accept and parse POST requests for a login form), and the express-session
module. Let's begin by installing those:
$ npm install --save express-session body-parser
Along with modifying a few files, we're also going to create a routes/auth.js
file and views/login.ejs
file:
$ touch routes/auth.js views...