convert auth helpers to modules

This commit is contained in:
Daniel 2026-05-08 07:56:41 +02:00
parent bc0b43151d
commit 9ca5365daf
4 changed files with 66 additions and 67 deletions

View file

@ -481,8 +481,8 @@
<script type="module" src="/js/voicePreferences.js"></script> <script type="module" src="/js/voicePreferences.js"></script>
<script defer src="/js/app.js?v=7.1.3"></script> <script defer src="/js/app.js?v=7.1.3"></script>
<script type="module" src="/js/ui-state.js"></script> <script type="module" src="/js/ui-state.js"></script>
<script defer src="/js/secureStorage.js"></script> <script type="module" src="/js/secureStorage.js"></script>
<script defer src="/js/authFetch.js"></script> <script type="module" src="/js/authFetch.js"></script>
<script defer src="/js/auth.js"></script> <script defer src="/js/auth.js"></script>
<script defer src="/js/liveEncounter.js"></script> <script defer src="/js/liveEncounter.js"></script>
<script defer src="/js/voiceDictation.js"></script> <script defer src="/js/voiceDictation.js"></script>

View file

@ -8,8 +8,7 @@
// the login screen. // the login screen.
// ============================================================ // ============================================================
(function() { if (!window.__fetchAuthIntercepted) {
if (window.__fetchAuthIntercepted) return;
window.__fetchAuthIntercepted = true; window.__fetchAuthIntercepted = true;
var _fetch = window.fetch.bind(window); var _fetch = window.fetch.bind(window);
@ -96,4 +95,4 @@
return resp; return resp;
}); });
}; };
})(); }

View file

@ -4,70 +4,68 @@
// Capacitor mobile: iOS Keychain / Android EncryptedSharedPreferences // Capacitor mobile: iOS Keychain / Android EncryptedSharedPreferences
// via capacitor-secure-storage-plugin // via capacitor-secure-storage-plugin
// ============================================================ // ============================================================
(function() { var memCache = {};
var memCache = {};
function isNative() { function isNative() {
try { try {
return !!(window.Capacitor && typeof window.Capacitor.isNativePlatform === 'function' && window.Capacitor.isNativePlatform()); return !!(window.Capacitor && typeof window.Capacitor.isNativePlatform === 'function' && window.Capacitor.isNativePlatform());
} catch(e) { return false; } } catch(e) { return false; }
} }
function plugin() { function plugin() {
try { try {
var p = window.Capacitor && window.Capacitor.Plugins && window.Capacitor.Plugins.SecureStoragePlugin; var p = window.Capacitor && window.Capacitor.Plugins && window.Capacitor.Plugins.SecureStoragePlugin;
return p || null; return p || null;
} catch(e) { return null; } } catch(e) { return null; }
} }
window.SecureStorage = { window.SecureStorage = {
isNative: isNative, isNative: isNative,
get: function(key) { get: function(key) {
if (isNative() && plugin()) { if (isNative() && plugin()) {
return plugin().get({ key: key }) return plugin().get({ key: key })
.then(function(r) { memCache[key] = r.value; return r.value; }) .then(function(r) { memCache[key] = r.value; return r.value; })
.catch(function() { return null; }); .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; });
}));
} }
}; 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; });
}));
}
};

View file

@ -9,6 +9,7 @@ const moduleEntrypoints = [
'public/js/clinicalAssistant.js', 'public/js/clinicalAssistant.js',
'public/js/admin.js', 'public/js/admin.js',
'public/js/admin-docs.js', 'public/js/admin-docs.js',
'public/js/authFetch.js',
'public/js/diagrams.js', 'public/js/diagrams.js',
'public/js/drugs-loader.js', 'public/js/drugs-loader.js',
'public/js/ed-encounters.js', 'public/js/ed-encounters.js',
@ -20,6 +21,7 @@ const moduleEntrypoints = [
'public/js/notes.js', 'public/js/notes.js',
'public/js/sickVisit.js', 'public/js/sickVisit.js',
'public/js/soap.js', 'public/js/soap.js',
'public/js/secureStorage.js',
'public/js/transcriptionSettings.js', 'public/js/transcriptionSettings.js',
'public/js/ui-state.js', 'public/js/ui-state.js',
'public/js/wellVisit.js', 'public/js/wellVisit.js',