Building the authentication layer
Since we need to add new non-trivial functionality to our application, we need to implement mainly two pieces:
- An authentication plugin to generate tokens, check the incoming requests, and revoke old or not used tokens
- A bunch of new routes to handle the registration, authentication, and life cycle of the tokens
Before jumping directly into the code, we need to add one last note. In this chapter’s code snippets, we will use a new data source called userDataSource
([1]). Since it exposes only createUser
([3]) and readUser
([2]) methods and the implementation is trivial, we will not show it in this book. However, the complete code is in the ./routes/auth/autohooks.js
file inside the GitHub repository.
Since we must implement both, we can add the authentication plugin first.
Authentication plugin
First, create the ./plugins/auth.js
file inside the project’s root folder. The auth.js
code snippet shows the implementation...