From 9ca5365daf5d6eb98bd83296089341ae619dda92 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 8 May 2026 07:56:41 +0200 Subject: [PATCH] convert auth helpers to modules --- public/index.html | 4 +- public/js/authFetch.js | 5 +- public/js/secureStorage.js | 122 ++++++++++++++++---------------- test/module-entrypoints.test.js | 2 + 4 files changed, 66 insertions(+), 67 deletions(-) diff --git a/public/index.html b/public/index.html index a9a2292..e1217ea 100644 --- a/public/index.html +++ b/public/index.html @@ -481,8 +481,8 @@ - - + + diff --git a/public/js/authFetch.js b/public/js/authFetch.js index eed38c4..d11759a 100644 --- a/public/js/authFetch.js +++ b/public/js/authFetch.js @@ -8,8 +8,7 @@ // the login screen. // ============================================================ -(function() { - if (window.__fetchAuthIntercepted) return; +if (!window.__fetchAuthIntercepted) { window.__fetchAuthIntercepted = true; var _fetch = window.fetch.bind(window); @@ -96,4 +95,4 @@ return resp; }); }; -})(); +} diff --git a/public/js/secureStorage.js b/public/js/secureStorage.js index d4eb503..78cf84d 100644 --- a/public/js/secureStorage.js +++ b/public/js/secureStorage.js @@ -4,70 +4,68 @@ // Capacitor mobile: iOS Keychain / Android EncryptedSharedPreferences // via capacitor-secure-storage-plugin // ============================================================ -(function() { - var memCache = {}; +var memCache = {}; - function isNative() { - try { - return !!(window.Capacitor && typeof window.Capacitor.isNativePlatform === 'function' && window.Capacitor.isNativePlatform()); - } catch(e) { return false; } - } +function isNative() { + try { + return !!(window.Capacitor && typeof window.Capacitor.isNativePlatform === 'function' && window.Capacitor.isNativePlatform()); + } catch(e) { return false; } +} - function plugin() { - try { - var p = window.Capacitor && window.Capacitor.Plugins && window.Capacitor.Plugins.SecureStoragePlugin; - return p || null; - } catch(e) { return null; } - } +function plugin() { + try { + var p = window.Capacitor && window.Capacitor.Plugins && window.Capacitor.Plugins.SecureStoragePlugin; + return p || null; + } catch(e) { return null; } +} - window.SecureStorage = { - isNative: isNative, +window.SecureStorage = { + isNative: isNative, - get: function(key) { - if (isNative() && plugin()) { - return plugin().get({ key: key }) - .then(function(r) { memCache[key] = r.value; return r.value; }) - .catch(function() { return null; }); - } - try { return Promise.resolve(localStorage.getItem(key)); } catch(e) { return Promise.resolve(null); } - }, - - set: function(key, value) { - memCache[key] = value; - if (isNative() && plugin()) { - return plugin().set({ key: key, value: value }).catch(function() {}); - } - try { localStorage.setItem(key, value); } catch(e) {} - return Promise.resolve(); - }, - - remove: function(key) { - delete memCache[key]; - if (isNative() && plugin()) { - var p = plugin().remove({ key: key }).catch(function() {}); - // Also clear legacy localStorage copy if any - try { localStorage.removeItem(key); } catch(e) {} - return p; - } - try { localStorage.removeItem(key); } catch(e) {} - return Promise.resolve(); - }, - - // Synchronous read for hot paths (fetch headers). On native returns cached - // value populated by prior get/set. On web reads localStorage directly. - getSync: function(key) { - if (isNative()) return memCache[key] || null; - try { return localStorage.getItem(key); } catch(e) { return null; } - }, - - // Hydrate the memory cache from native storage at boot. Resolves when ready. - hydrate: function(keys) { - if (!isNative() || !plugin()) return Promise.resolve(); - return Promise.all(keys.map(function(k) { - return plugin().get({ key: k }) - .then(function(r) { memCache[k] = r.value; }) - .catch(function() { memCache[k] = null; }); - })); + get: function(key) { + if (isNative() && plugin()) { + return plugin().get({ key: key }) + .then(function(r) { memCache[key] = r.value; return r.value; }) + .catch(function() { return null; }); } - }; -})(); + try { return Promise.resolve(localStorage.getItem(key)); } catch(e) { return Promise.resolve(null); } + }, + + set: function(key, value) { + memCache[key] = value; + if (isNative() && plugin()) { + return plugin().set({ key: key, value: value }).catch(function() {}); + } + try { localStorage.setItem(key, value); } catch(e) {} + return Promise.resolve(); + }, + + remove: function(key) { + delete memCache[key]; + if (isNative() && plugin()) { + var p = plugin().remove({ key: key }).catch(function() {}); + // Also clear legacy localStorage copy if any + try { localStorage.removeItem(key); } catch(e) {} + return p; + } + try { localStorage.removeItem(key); } catch(e) {} + return Promise.resolve(); + }, + + // Synchronous read for hot paths (fetch headers). On native returns cached + // value populated by prior get/set. On web reads localStorage directly. + getSync: function(key) { + if (isNative()) return memCache[key] || null; + try { return localStorage.getItem(key); } catch(e) { return null; } + }, + + // Hydrate the memory cache from native storage at boot. Resolves when ready. + hydrate: function(keys) { + if (!isNative() || !plugin()) return Promise.resolve(); + return Promise.all(keys.map(function(k) { + return plugin().get({ key: k }) + .then(function(r) { memCache[k] = r.value; }) + .catch(function() { memCache[k] = null; }); + })); + } +}; diff --git a/test/module-entrypoints.test.js b/test/module-entrypoints.test.js index b0e4eed..71b4a88 100644 --- a/test/module-entrypoints.test.js +++ b/test/module-entrypoints.test.js @@ -9,6 +9,7 @@ const moduleEntrypoints = [ 'public/js/clinicalAssistant.js', 'public/js/admin.js', 'public/js/admin-docs.js', + 'public/js/authFetch.js', 'public/js/diagrams.js', 'public/js/drugs-loader.js', 'public/js/ed-encounters.js', @@ -20,6 +21,7 @@ const moduleEntrypoints = [ 'public/js/notes.js', 'public/js/sickVisit.js', 'public/js/soap.js', + 'public/js/secureStorage.js', 'public/js/transcriptionSettings.js', 'public/js/ui-state.js', 'public/js/wellVisit.js',