'Is there an alternative to making a database query to get user info for authentication for every request?

I am learning ExpressJS and I got to authentication/authorization middleware using JWT. From what I understand the general flow goes something like this:

  1. User sends username and password
  2. Server verifies info, creates a JWT where payload is user_id, and sends the token back
  3. The token is sent with every following request
  4. Before each request first the authentication middleware verifies the JWT, then makes a database query to get the user info and attaches it to the req object and moves on the next middleware
  5. The following middleware can access the user info with req.user

So my question is this: Is there an alternative to this method where we do not make a database query to get the user info before every request? Or is that query for user info just an inevitable cost?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source