Introduction to passport.js
passport.js
is a middleware provided by Node.js for authentication. The functionality of passport.js
is to authenticate the requests that are made to the server. It provides several strategies for authentication. passport.js
provides strategies to such as local strategy, Facebook strategy, Google strategy, Twitter strategy, and JWT strategy. In this chapter, we will focus on using the JWT strategy.
JWT
JWT is a way of authenticating the requests using a token-based approach. There are two methods of authenticating requests: cookie-based authentication, and token-based authentication. The cookie-based authentication mechanism saves the user's session ID in the browser's cookie, whereas the token-based mechanism uses a signed token that will look like this:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjVhNjhhNDMzMDJkMWNlZDU5YjExNDg3MCIsImlhdCI6MTUxNzI0MjM1M30.5xY59iTIjpt9ukDmxseNAGbOdz6weWL1drJkeQzoO3M
This token is then validated on every request that we make to the...