Fix race condition on user loading

This commit is contained in:
Daniel Egger 2022-12-14 23:15:07 +01:00
parent 15b5223dc5
commit 527b806a35
2 changed files with 10 additions and 16 deletions

View File

@ -1,13 +1,13 @@
import { useUserStore } from "@/stores/user"; import { useUserStore } from "@/stores/user";
import type { NavigationGuardWithThis, RouteLocationNormalized } from "vue-router"; import type { NavigationGuardWithThis, RouteLocationNormalized } from "vue-router";
export const updateLoggedIn: NavigationGuardWithThis<undefined> = (_to) => { export const updateLoggedIn: NavigationGuardWithThis<undefined> = async (_to) => {
const loggedIn = getCookieValue("loginStatus") === "true"; const loggedIn = getCookieValue("loginStatus") === "true";
const userStore = useUserStore(); const userStore = useUserStore();
userStore.$patch({ loggedIn }); userStore.$patch({ loggedIn });
if (loggedIn && !userStore.email) { if (loggedIn && !userStore.id) {
userStore.fetchUser(); await userStore.fetchUser();
} }
}; };

View File

@ -71,20 +71,14 @@ export const useUserStore = defineStore({
window.location.href = redirectUrl; window.location.href = redirectUrl;
}); });
}, },
fetchUser() { async fetchUser() {
const appStore = useAppStore(); const appStore = useAppStore();
itGetCached("/api/core/me/") const data = await itGetCached("/api/core/me/");
.then((data) => { this.$state = data;
this.$state = data; this.loggedIn = true;
this.loggedIn = true; appStore.userLoaded = true;
appStore.userLoaded = true; const courseSessionsStore = useCourseSessionsStore();
const courseSessionsStore = useCourseSessionsStore(); await courseSessionsStore.loadCourseSessionsData();
courseSessionsStore.loadCourseSessionsData();
})
.catch(() => {
this.loggedIn = false;
appStore.userLoaded = true;
});
}, },
}, },
}); });