NodeJS

API Security

NodeJS Course

Introduction

Securing your API is an important step. When we were using Express to serve view templates we used PassportJS along with a username and password to authenticate users, but that is not the only way to secure an Express app, and in the context of an API it often makes sense to use a different strategy. The username and password session pattern that we learned previously will still work of course, though it is made a little more complicated by the fact that we’ve separated our front-end code from the back-end.

Another strategy is to generate and pass a secure token between our back-end and front-end code. Doing so will make sure that our user’s username and password are not compromised and will also give us the ability to expire our user’s session for added security. The basic idea is that when a user signs in to our app, a secure token is created, and then for all subsequent requests that token is passed in the header of our request object. In the end, the process is straightforward since you should already be comfortable with using passport to authenticate users.

This strategy, while particularly useful with APIs can be used with a traditional view-template project as well. The main difference here is that instead of setting and checking a cookie we’re passing a special token in the header of our request. In our previous Authentication Tutorial, the Passport middleware checked the cookie that was sent and then either authenticated or denied our user. In this case, we’re going to do something very similar, but instead of using cookies, we’re going to pass the token.

Lesson overview

This section contains a general overview of topics that you will learn in this lesson.

  • Explain how token authentication differs from session based authentication.
  • What are JSON Web Tokens?
  • What is an authorization header? How do we use it?
  • Identify and explain the methods used to sign and verify tokens.
  • Write custom middleware to verify tokens on a given route.
  • Have familiarity with token expiration with JWT.
  • Expand PassportJS implementations to use JSON Web Tokens.

Assignment

  1. This video is a great resource that explains everything you need to know about creating and verifying JSON Web Tokens.
  2. This video presents different ways in which JWTs can be useful.

Knowledge check

The following questions are an opportunity to reflect on key topics in this lesson. If you can’t answer a question, click on it to review the material, but keep in mind you are not expected to memorize or master this knowledge.

Additional resources

This section contains helpful links to related content. It isn’t required, so consider it supplemental.

Support us!

The Odin Project is funded by the community. Join us in empowering learners around the globe by supporting The Odin Project!