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 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 userStore = useUserStore();
userStore.$patch({ loggedIn });
if (loggedIn && !userStore.email) {
userStore.fetchUser();
if (loggedIn && !userStore.id) {
await userStore.fetchUser();
}
};

View File

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