Fix race condition on user loading
This commit is contained in:
parent
15b5223dc5
commit
527b806a35
|
|
@ -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();
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -71,20 +71,14 @@ export const useUserStore = defineStore({
|
|||
window.location.href = redirectUrl;
|
||||
});
|
||||
},
|
||||
fetchUser() {
|
||||
async fetchUser() {
|
||||
const appStore = useAppStore();
|
||||
itGetCached("/api/core/me/")
|
||||
.then((data) => {
|
||||
this.$state = data;
|
||||
this.loggedIn = true;
|
||||
appStore.userLoaded = true;
|
||||
const courseSessionsStore = useCourseSessionsStore();
|
||||
courseSessionsStore.loadCourseSessionsData();
|
||||
})
|
||||
.catch(() => {
|
||||
this.loggedIn = false;
|
||||
appStore.userLoaded = true;
|
||||
});
|
||||
const data = await itGetCached("/api/core/me/");
|
||||
this.$state = data;
|
||||
this.loggedIn = true;
|
||||
appStore.userLoaded = true;
|
||||
const courseSessionsStore = useCourseSessionsStore();
|
||||
await courseSessionsStore.loadCourseSessionsData();
|
||||
},
|
||||
},
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue