Skip to content

Commit c6c703f

Browse files
BenjaminCharmesml-evs
authored andcommitted
Centralize user auth in Vuex to prevent duplicate API calls
1 parent 7cf0320 commit c6c703f

File tree

3 files changed

+57
-18
lines changed

3 files changed

+57
-18
lines changed

webapp/src/router/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ const router = createRouter({
104104

105105
router.beforeEach(async (to, from, next) => {
106106
if (to.path === "/" || (to.name === "samples" && from.path === "/")) {
107-
const { getUserInfo } = await import("@/server_fetch_utils.js");
108-
const user = await getUserInfo();
107+
const { isUserAuthenticated } = await import("@/server_fetch_utils.js");
108+
const authenticated = await isUserAuthenticated();
109109

110-
if (!user) {
110+
if (!authenticated) {
111111
next("/about");
112112
return;
113113
}

webapp/src/server_fetch_utils.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -378,20 +378,12 @@ export function searchCollections(query, nresults = 100) {
378378
}
379379

380380
export async function getUserInfo() {
381-
store.commit("setCurrentUserInfoLoading", true);
382-
return fetch_get(`${API_URL}/get-current-user/`)
383-
.then((response_json) => {
384-
store.commit("setDisplayName", response_json.display_name);
385-
store.commit("setCurrentUserID", response_json.immutable_id);
386-
store.commit("setIsUnverified", response_json.account_status == "unverified" ? true : false);
387-
store.commit("setCurrentUserInfoLoading", false);
388-
return response_json;
389-
})
390-
.catch(() => {
391-
// If the user is not logged in, we return null.
392-
store.commit("setCurrentUserInfoLoading", false);
393-
return null;
394-
});
381+
return store.dispatch("fetchCurrentUser", { fullInfo: true });
382+
}
383+
384+
export async function isUserAuthenticated() {
385+
const result = await store.dispatch("fetchCurrentUser");
386+
return result === true || (result && result.immutable_id);
395387
}
396388

397389
export async function requestMagicLink(email_address) {

webapp/src/store/index.js

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { API_URL } from "@/resources.js";
12
import { createStore } from "vuex";
23
// import { createLogger } from "vuex";
34
// import { set } from 'vue'
@@ -30,6 +31,8 @@ export default createStore({
3031
currentUserDisplayName: null,
3132
currentUserID: null,
3233
currentUserInfoLoading: false,
34+
currentUserInfoLoaded: false,
35+
currentUserInfoPromise: null,
3336
serverInfo: null,
3437
blocksInfos: {},
3538
currentUserIsUnverified: false,
@@ -345,6 +348,9 @@ export default createStore({
345348
},
346349
setCurrentUserInfoLoading(state, isLoading) {
347350
state.currentUserInfoLoading = isLoading;
351+
if (!isLoading && !state.currentUserID) {
352+
state.currentUserInfoLoaded = true;
353+
}
348354
},
349355
setItemGraphIsLoading(state, isLoading) {
350356
state.itemGraphIsLoading = isLoading;
@@ -423,7 +429,48 @@ export default createStore({
423429
return state.userActivityCache[cacheKey];
424430
},
425431
},
426-
actions: {},
432+
actions: {
433+
async fetchCurrentUser({ state, commit }, { fullInfo = false } = {}) {
434+
if (state.currentUserInfoPromise) {
435+
return state.currentUserInfoPromise;
436+
}
437+
438+
if (!fullInfo && state.currentUserInfoLoaded) {
439+
return state.currentUserID !== null;
440+
}
441+
442+
commit("setCurrentUserInfoLoading", true);
443+
444+
const promise = fetch(`${API_URL}/get-current-user/`, {
445+
method: "GET",
446+
credentials: "include",
447+
})
448+
.then((response) => {
449+
if (!response.ok) {
450+
throw new Error("UNAUTHORIZED");
451+
}
452+
return response.json();
453+
})
454+
.then((response_json) => {
455+
commit("setDisplayName", response_json.display_name);
456+
commit("setCurrentUserID", response_json.immutable_id);
457+
commit("setIsUnverified", response_json.account_status == "unverified" ? true : false);
458+
commit("setCurrentUserInfoLoading", false);
459+
state.currentUserInfoLoaded = true;
460+
state.currentUserInfoPromise = null;
461+
return response_json;
462+
})
463+
.catch(() => {
464+
commit("setCurrentUserInfoLoading", false);
465+
state.currentUserInfoLoaded = true;
466+
state.currentUserInfoPromise = null;
467+
return null;
468+
});
469+
470+
state.currentUserInfoPromise = promise;
471+
return promise;
472+
},
473+
},
427474
modules: {},
428475
// plugins: [createLogger()],
429476
});

0 commit comments

Comments
 (0)