I am running a NodeJS express application with a PostgreSQL DB. I am using node-cache module as the store for session management in express.
The app has a user management scheme which consists of two types of users, a common user and an Admin. An admin can perform all the tasks that a common user can. In addition to that, admin can add/remove users from the app and also grant/revoke admin rights to/from users. So here comes my problem:
If an admin removes user X from the tool while user X is still logged in, then on the next request that user X sends to the server, he must be automatically logged out. Similarly, if an admin revokes admin rights of user Y while he is still logged in, then on next hit to server user Y should be automatically logged out.
After some research I came up with two ways of implementing this:
1) Authorize every request - i.e. write a middleware that will query the database and check if the user still has tool access rights. But this approach has the overhead of one additional DB hit for every request. This, I think, is a significant overhead because in my app users are not removed frequently and also the probability of an user being removed when he is already logged in is small.
2) Don't authorize every request. Instead, when user's tool access is removed, delete that user's session (if any) from the cache. So when that user sends a request, he will be logged out since his session no longer exists. However, doing this is not so straightforward since node-cache (or even Redis) does not have any 'delete by value' function. To do this I'll have to search through all the keys in the cache, find the ones that have value same as userID of user whose session needs to be removed and then delete those sessions.
I would like to know which approach is better and efficient or if there is a third and a better approach.