expressjwtmicroservices

Use JWT to authenticate separate API Microservice


I am developing an app using microservices in NodeJS. I have built an auth api which handles the usual registration login etc and it issues JWT's

How do I use these to protect routes in a separate API microservice written with Express?

Do I need to use JWT with the secret to decrypt the token in the API app?


Solution

  • One common pattern here would be to use an API gateway as the entry point to your entire microservice architecture. Incoming requests for authentication would be routed to the appropriate microservice. If the credentials provided be correct, a new JWT would be returned to the gateway, which would then forward to the caller. For the actual microservice APIs which comprise your application, the gateway would check that the incoming JWT be valid before allowing the request to hit the microservice.

    This answer leaves out a few things, for simplicity. For instance, often you would want to have an authorization microservice, which decides what a user is allowed to do. Also, implementing JWT can be involved. You might need a cache layer to keep track of whitelisted and/or blacklisted JWT.