From eb0f47b194709adcac8f8c4622ab34acf2b0b31c Mon Sep 17 00:00:00 2001 From: Maximilian Oertel Date: Fri, 11 Oct 2024 16:45:06 +0100 Subject: [PATCH] Fix post-signout firekit initialisation --- src/App.vue | 1 - .../mutations/useSignOutMutation.js | 4 ++++ src/store/auth.js | 23 +++++++++++-------- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/App.vue b/src/App.vue index 229200161..2efb72ec7 100644 --- a/src/App.vue +++ b/src/App.vue @@ -86,7 +86,6 @@ const navbarBlacklist = ref([ onBeforeMount(async () => { await authStore.initFirekit(); - authStore.setUser(); await authStore.initStateFromRedirect().then(async () => { // @TODO: Refactor this callback as we should ideally use the useUserClaimsQuery and useUserDataQuery composables. diff --git a/src/composables/mutations/useSignOutMutation.js b/src/composables/mutations/useSignOutMutation.js index 22fa88417..43654d563 100644 --- a/src/composables/mutations/useSignOutMutation.js +++ b/src/composables/mutations/useSignOutMutation.js @@ -33,6 +33,10 @@ const useSignOutMutation = () => { // Clear the query client to remove all cached data. queryClient.clear(); + // Re-initialize Firekit. This is necessary to ensure that Firekit is properly reset after + // sign-out in order to allow a new user to sign in. + await authStore.initFirekit(); + // Redirect to sign-in page. router.push({ path: APP_ROUTES.SIGN_IN }); }, diff --git a/src/store/auth.js b/src/store/auth.js index 033fb3b4b..7e5bd5fc6 100644 --- a/src/store/auth.js +++ b/src/store/auth.js @@ -25,6 +25,8 @@ export const useAuthStore = () => { routeToProfile: false, ssoProvider: null, showOptionalAssessments: false, + adminAuthStateListener: null, + appAuthStateListener: null, }; }, getters: { @@ -61,8 +63,17 @@ export const useAuthStore = () => { //@TODO: Move to mutation since we cannot rotate query keys anymore. await this.roarfirekit.completeAssessment(adminId, taskId); }, - setUser() { - onAuthStateChanged(this.roarfirekit?.admin.auth, async (user) => { + async initFirekit() { + try { + this.roarfirekit = await initNewFirekit(); + this.setAuthStateListeners(); + } catch (error) { + // @TODO: Improve error handling as this is a critical error. + console.error('Error initializing Firekit:', error); + } + }, + setAuthStateListeners() { + this.adminAuthStateListener = onAuthStateChanged(this.roarfirekit?.admin.auth, async (user) => { if (user) { this.localFirekitInit = true; this.firebaseUser.adminFirebaseUser = user; @@ -70,7 +81,7 @@ export const useAuthStore = () => { this.firebaseUser.adminFirebaseUser = null; } }); - onAuthStateChanged(this.roarfirekit?.app.auth, async (user) => { + this.appAuthStateListener = onAuthStateChanged(this.roarfirekit?.app.auth, async (user) => { if (user) { this.firebaseUser.appFirebaseUser = user; } else { @@ -78,11 +89,6 @@ export const useAuthStore = () => { } }); }, - async initFirekit() { - this.roarfirekit = await initNewFirekit().then((firekit) => { - return firekit; - }); - }, async getLegalDoc(docName) { return await this.roarfirekit.getLegalDoc(docName); }, @@ -182,7 +188,6 @@ export const useAuthStore = () => { return this.roarfirekit.createLevanteUsersWithEmailPassword(userData); }, }, - // persist: true persist: { storage: sessionStorage, debug: false,