SHARE
Facebook X Pinterest WhatsApp

Integrate JSON Web Token Authentication into Single-page Apps

Written By
thumbnail
Rob Gravelle
Rob Gravelle
Sep 21, 2018

There are basically two types of authentication you can use in any web app: Session-based and Token-based. For today’s single-page apps (SPAs), Session-based authentication tends to be overkill. Therefore, Token-based authentication is the de facto standard for SPAs. JSON Web Tokens (JWT) are one of the most widely used in modern SPAs. JWT is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object. This information can be verified and trusted because it is digitally signed. JWTs can be signed using a secret (with the HMAC algorithm) or a public/private key pair using a cryptographic algorithm such as RSA or ECDSA.

In today’s article, we’ll set up a Node.js project in MS Visual Source Code. Within that development environment we’ll build and test an SPA that authenticates a user’s credentials against one that is stored securely in a MongoDB database.

Prerequisites

In order to follow along with this tutorial, you’ll want to download MS Visual Studio Code, as well as the Node.js JavaScript runtime and npm Node.js package manager. Node.js comes with npm, so installing it covers both tools.

To confirm that you have Node.js and npm correctly installed on your machine, you can type the following statements at the command prompt:

C:\Users\blackjacques>node -- version
v8.10.0

C:\Users\blackjacques>npm -- version
5.6.0

Creating the Project

The first thing we’ll need is a folder in which to put all of our project files.

  1. Fire up Visual Studio and select File > Add Folder to Workspace…
  2. That will bring up a folder browser that you can use to select the project folder. You don’t have to create one before-hand. In Windows, you can just use the New Folder button to create one.

  3. With the project folder added, go to the Terminal and make sure that the working directory is your project root. If the Terminal is not currently visible, go to View > Integrated Terminal to bring it up.
  4. In the Terminal, type the following command:

    npm init
    

    That will initiate a command line questionnaire that will conclude with the creation of a package.json in the directory in which you initiated the command.

    Alternatively, you can issue:

    npm init -- yes
    

    That will generate a default package.json using information extracted from the current directory.

  5. Now it’s time to install some dependencies:

    npm install express jsonwebtoken mongoose body-parser -- save
    

    That will install the express web framework, jsonwebtoken package to authenticate the user, mongoose schema model, and body-parser middleware.

  6. We also need to install the nodemon development server to prevent the stopping and restarting of the server process.

    npm install nodemon -- save-dev
    

That’s all we need for now. We’ll be adding a few more dependencies as we go.

Configuring the Node Server

Going Forward

We’ll leave it at that for now. You can peruse the files that we worked on today on GitHub.

Next time, we’ll:

  • Set up the MongoDb database.
  • Create a User model.
  • Create the Signup Route.
  • And, last but not least, add the authorization code!

Recommended for you...

Website Security 101
James Payne
May 21, 2021
An Introduction to JSON Web Tokens (JWT)
Diogo Souza
Dec 2, 2019
How to Use Enzyme for React JS Testing
Diogo Souza
Jul 15, 2019
Avoiding Dark Patterns
Octavia Anghel
Apr 8, 2019
HTML Goodies Logo

The original home of HTML tutorials. HTMLGoodies is a website dedicated to publishing tutorials that cover every aspect of being a web developer. We cover programming and web development tutorials on languages and technologies such as HTML, JavaScript, and CSS. In addition, our articles cover web frameworks like Angular and React.JS, as well as popular Content Management Systems (CMS) that include WordPress, Drupal, and Joomla. Website development platforms like Shopify, Squarespace, and Wix are also featured. Topics related to solid web design and Internet Marketing also find a home on HTMLGoodies, as we discuss UX/UI Design, Search Engine Optimization (SEO), and web dev best practices.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.