Skip to content

Commit fb67b4c

Browse files
Centralize user auth in Vuex to prevent duplicate API calls
1 parent f2827d4 commit fb67b4c

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,
@@ -344,6 +347,9 @@ export default createStore({
344347
},
345348
setCurrentUserInfoLoading(state, isLoading) {
346349
state.currentUserInfoLoading = isLoading;
350+
if (!isLoading && !state.currentUserID) {
351+
state.currentUserInfoLoaded = true;
352+
}
347353
},
348354
setItemGraphIsLoading(state, isLoading) {
349355
state.itemGraphIsLoading = isLoading;
@@ -409,7 +415,48 @@ export default createStore({
409415
return Object.values(state.blocksInfos);
410416
},
411417
},
412-
actions: {},
418+
actions: {
419+
async fetchCurrentUser({ state, commit }, { fullInfo = false } = {}) {
420+
if (state.currentUserInfoPromise) {
421+
return state.currentUserInfoPromise;
422+
}
423+
424+
if (!fullInfo && state.currentUserInfoLoaded) {
425+
return state.currentUserID !== null;
426+
}
427+
428+
commit("setCurrentUserInfoLoading", true);
429+
430+
const promise = fetch(`${API_URL}/get-current-user/`, {
431+
method: "GET",
432+
credentials: "include",
433+
})
434+
.then((response) => {
435+
if (!response.ok) {
436+
throw new Error("UNAUTHORIZED");
437+
}
438+
return response.json();
439+
})
440+
.then((response_json) => {
441+
commit("setDisplayName", response_json.display_name);
442+
commit("setCurrentUserID", response_json.immutable_id);
443+
commit("setIsUnverified", response_json.account_status == "unverified" ? true : false);
444+
commit("setCurrentUserInfoLoading", false);
445+
state.currentUserInfoLoaded = true;
446+
state.currentUserInfoPromise = null;
447+
return response_json;
448+
})
449+
.catch(() => {
450+
commit("setCurrentUserInfoLoading", false);
451+
state.currentUserInfoLoaded = true;
452+
state.currentUserInfoPromise = null;
453+
return null;
454+
});
455+
456+
state.currentUserInfoPromise = promise;
457+
return promise;
458+
},
459+
},
413460
modules: {},
414461
// plugins: [createLogger()],
415462
});

0 commit comments

Comments
 (0)