We are using CloudFlare in front of an API whose origin server is behind auth. Users login, and most receive the same data. In a few cases, some users receive different data based on their username and access level.
E.g. users Rahul accesses the endpoint /fruit. Rahul receives Apples, Bananas. All other users receive Apples, Bananas and Pears.
So we need to change the cacheKey when Rahul (and a few others) make a request, appending their username or auth string, and this is where I’m stuck. I hacked up a basic example, but I’m struggling with the concept of what the worker should actually do. Is it:
- Have we got auth in the response? If so, Base64 decode the Authorization header to get the username
- Is username in the list of restricted usernames?
- If so, append cachekey.
Should the worker then check the cache for this cachekey and apply some logic (if not there, insert), or can I just return the altered cacheKey as this guide suggests?
I’ve tried various methods, and an additional problem was the worker seemed to interfere with the authentication (on the origin server), so I’m not sure if my understanding of what a worker can/should do is right.
Really appreciate if someone can advise