Authentication with Express.js
Many web applications require a login system. Often, users of a website have different privileges, and to ascertain which resources they're able to access, they must first be identified via authentication.
This is typically achieved by setting up a session, which is a temporary information exchange between a user and a device. In this recipe, we're going to implement an authentication layer for an Express.js server.
Getting ready
We must first start by scaffolding an Express.js web server for the recipe:
Important Note
For a more detailed introduction to Express.js, refer to the Building web applications with Express.js recipe in Chapter 6, Exploring Node.js Web Frameworks.
- First, let's create a project directory to work in and initialize the project with
npm
. We'll also create some files and subdirectories that we'll use later in the recipe:$ mkdir express-auth $ cd express-auth $ npm init --yes $ mkdir...