MCPcopy Create free account
hub / github.com/Acode-Foundation/Acode / getLoggedInUser

Method getLoggedInUser

src/lib/auth.js:129–158  ·  view source on GitHub ↗

* @param {boolean} forceFetch * @returns {Promise }

(forceFetch = false)

Source from the content-addressed store, hash-verified

127 * @returns {Promise<User>}
128 */
129 async getLoggedInUser(forceFetch = false) {
130 if (loggedInUser && !forceFetch) return loggedInUser;
131
132 try {
133 const res = await fetch(`${config.API_BASE}/login`);
134
135 if (res.ok) {
136 loggedInUser = await res.json();
137 localStorage.setItem(CACHE_USER_KEY, JSON.stringify(loggedInUser));
138 clearTimeout(cacheTimeout);
139 cacheTimeout = setTimeout(() => (loggedInUser = null), 600_000);
140 return loggedInUser;
141 }
142
143 if (res.status === 401) {
144 localStorage.removeItem(CACHE_USER_KEY);
145 return null;
146 }
147
148 throw new Error("Unable to fetch user Info");
149 } catch (error) {
150 if (CACHE_USER_KEY in localStorage) {
151 try {
152 return JSON.parse(localStorage.getItem(CACHE_USER_KEY));
153 } catch {}
154 }
155 console.error("Unable to fetch user info:", error);
156 throw error;
157 }
158 }
159
160 async login() {
161 return new Promise((resolve, reject) => {

Callers 7

onDeviceReadyFunction · 0.80
onLoginFunction · 0.80
index.jsFile · 0.80
plugin.view.jsFile · 0.80
getOwnedFunction · 0.80
callbackFunction · 0.80
HandleIntentFunction · 0.80

Calls 1

errorMethod · 0.45

Tested by

no test coverage detected