Skip to Main Content
Beginning API Development with Node.js
book

Beginning API Development with Node.js

by Anthony Nandaa
July 2018
Beginner content levelBeginner
100 pages
2h 7m
English
Packt Publishing
Content preview from Beginning API Development with Node.js

Exercise 20: Adding User Authentication

Now that we have secured all our todo routes, we need a way to issue tokens to valid users to access the API. We will have the users send their email and password to a route (/auth), and our API will issue back an authentication token which will be used for each request:

  1. In the /routes folder, create a file auth.js.
  2. We will now need two more packages for this, jsonwebtoken for signing the authentication token, and md5 for comparing the password since if you recall, we were using MySQL's md5 function to store the user's password:
npm install jsonwebtoken md5 --save
  1. In the auth.js file, have the following code:
const jwt = require('jsonwebtoken');const Joi = require('joi');const md5 = require('md5'); ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Pro REST API Development with Node.js

Pro REST API Development with Node.js

Fernando Doglio
Building APIs with Node.js

Building APIs with Node.js

Caio Ribeiro Pereira
Learning Node.js

Learning Node.js

Marc Wandschneider

Publisher Resources

ISBN: 9781789539660Supplemental Content