From 98511a5b24eea780c21d038db79058e51900ed09 Mon Sep 17 00:00:00 2001 From: k2maan Date: Tue, 10 Oct 2023 18:03:32 +0530 Subject: [PATCH] Improved: synced app state on logout --- src/store/auth.ts | 2 +- src/views/Home.vue | 22 ++++++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/store/auth.ts b/src/store/auth.ts index 33564a9..c15cf61 100644 --- a/src/store/auth.ts +++ b/src/store/auth.ts @@ -22,7 +22,7 @@ export const useAuthStore = defineStore('authStore', { const currTime = DateTime.now().toMillis(); isTokenExpired = state.token.expiration < currTime; } - return state.token.value && !isTokenExpired; + return !!(state.token.value && !isTokenExpired); }, getOMS: (state) => state.oms, getBaseUrl: (state) => { diff --git a/src/views/Home.vue b/src/views/Home.vue index 1c2c30e..8c46cfe 100644 --- a/src/views/Home.vue +++ b/src/views/Home.vue @@ -14,7 +14,7 @@

{{ authStore.getOMS }}

{{ authStore.current?.partyName ? authStore.current?.partyName : authStore.current.userLoginId }}

- {{ $t('Logout') }} + {{ $t('Logout') }} @@ -65,9 +65,9 @@ import { defineComponent, ref } from 'vue'; import { codeWorkingOutline, lockClosedOutline, + personCircleOutline, rocketOutline, - shieldHalfOutline, - personCircleOutline + shieldHalfOutline } from 'ionicons/icons'; import { useAuthStore } from '@/store/auth'; import { useRouter } from "vue-router"; @@ -93,8 +93,17 @@ export default defineComponent({ // and the user logs in through one and tries to login again from the next tab // $hydate will resync the state and hence, update the app UI this.authStore.$hydrate({ runHooks: false }) - if (this.authStore.isAuthenticated) return - this.router.push('/login') + // push to login only if user is not logged in (after state hydration) + if (!this.authStore.isAuthenticated) { + this.router.push('/login') + } + }, + async logout() { + this.authStore.$hydrate({ runHooks: false }) + // hydrate and logout only if user is logged in (authenticated) + if (this.authStore.isAuthenticated) { + await this.authStore.logout() + } } }, setup() { @@ -230,9 +239,6 @@ export default defineComponent({ object-fit: cover; } - .app-content { - } - ion-card { border-radius: 40px; transition: .4s cubic-bezier(0.59, 0.08, 0.05, 1.4);