Adding access tokens to secure our API
The APIs that we created in the last two recipes is public. That means everyone can access and get the information from our server, but what happens if you want to add a security layer on the API and get the information for registered users on your platform? We need to add access token validation to protect our API, and to do this; we have to use JSON Web Tokens (JWT).
Getting ready
For this recipe, you need to install JWT for Node.js:
npm install jsonwebtoken
How to do it...
We will mostly use the same code that we created for the MySQL recipe and add a security layer to validate our access tokens:
- The first thing we need to do is to modify our config file (
config/index.js
), add a security node with thesecretKey
we are going to use to create our tokens, and add the expiration time of the token:
export default { db: { dialect: 'mysql', // The database engine you want to use host: 'localhost', // Your host, by default is localhost database: 'blog', //...