Authentication with Fastify
Many web applications require a login system. Often, users of a website have different privileges, and to determine which resources they can access, they must first be identified through authentication.
This is typically achieved by setting up a session, which is a temporary information exchange between a user and a device. Sessions enable the server to store user-specific information, which can be used to manage access and maintain the user’s state across multiple requests.
In this recipe, we’ll implement an authentication layer for a Fastify server. Please refer to Chapter 6 for more information on Fastify.
Getting ready
Let’s start by creating a Fastify server:
- Create a project directory named
fastify-auth
to work in and initialize the project withnpm
. We’ll also create some files and subdirectories that we’ll use later in this recipe:$ mkdir fastify-auth $ cd fastify-auth $ npm init --yes $ mkdir...