···40404141/**
4242 * Save data to the cache
4343+ * Stores the user DID alongside the data for validation
4344 */
4445function setCache(data) {
4546 try {
4647 const cache = {
4748 version: CACHE_VERSION,
4849 timestamp: Date.now(),
5050+ did: data.did || null, // Store user DID for cache validation
4951 data: data,
5052 };
5153 localStorage.setItem(CACHE_KEY, JSON.stringify(cache));
5254 } catch (e) {
5355 console.warn("Failed to write cache:", e);
5456 }
5757+}
5858+5959+/**
6060+ * Get the DID stored in the cache
6161+ */
6262+function getCachedDID() {
6363+ const cache = getCache();
6464+ return cache?.did || null;
5565}
56665767/**
···6676}
67776878/**
6969- * Get cached data if available and valid
7979+ * Get the current user's DID from the page
8080+ */
8181+function getCurrentUserDID() {
8282+ return document.body?.dataset?.userDid || null;
8383+}
8484+8585+/**
8686+ * Get cached data if available and valid for the current user
7087 */
7188function getCachedData() {
7289 const cache = getCache();
7390 if (!cache) return null;
74919292+ // Validate that cached data belongs to the current user
9393+ const currentDID = getCurrentUserDID();
9494+ const cachedDID = cache.did;
9595+9696+ // If we have both DIDs and they don't match, cache is invalid
9797+ if (currentDID && cachedDID && currentDID !== cachedDID) {
9898+ console.log("Cache belongs to different user, invalidating");
9999+ invalidateCache();
100100+ return null;
101101+ }
102102+75103 // Return data even if expired - caller can decide to refresh
76104 return cache.data;
77105}
···118146 isRefreshing = true;
119147 try {
120148 const data = await fetchFreshData();
149149+150150+ // Check if user changed (different DID)
151151+ const cachedDID = getCachedDID();
152152+ if (cachedDID && data.did && cachedDID !== data.did) {
153153+ console.log("User changed, clearing stale cache");
154154+ invalidateCache();
155155+ }
156156+121157 setCache(data);
122158 notifyListeners(data);
123159 return data;