I am not sure if I used the correct title but I was unable to think of a better way to describe it. It could be more of a design question.
I have a multi-tenant DB where one user can belong to one or more entities. I authenticate the user with his/her credentials by calling the /token endpoint.
After I receive a token I call my own end point (using the token) to get the list of available entities for this user and then allow this user to set his current entity in a memory cache. I then use this in memory cache to look up the entity/tenant ID for all following requests to know which entity/tenant the user is "logged into" when calling the DB.
Ideally I would like to eliminate the need for the memory cache in order to make my application more stateless by including the entity/tenant ID as a claim in the token but I only know this ID after the user has authenticated and selected his/her entity. I obviously can't change or add to the claims after the token is issued but is there an alternative design to implement this kind of behavior?
I considered possibly using a sub domain per tenant but technically this is more difficult to setup and maintain. I also considered prompting the user to enter the entity he/she wishes to log in to as free text with their credentials but this isn't ideal.
Has anyone faced this challenge before?