From fea7a916261f72d758dca4c468eb305b162096d7 Mon Sep 17 00:00:00 2001 From: k2maan Date: Wed, 2 Aug 2023 15:41:41 +0530 Subject: [PATCH 1/2] Improved: flow for accessing login route and added launchpad logout on app logout --- src/router/index.ts | 12 ++++++++++++ src/views/Login.vue | 47 +++++++++++++++++++++++++-------------------- 2 files changed, 38 insertions(+), 21 deletions(-) diff --git a/src/router/index.ts b/src/router/index.ts index cd3906d..51e8b6c 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -2,6 +2,17 @@ import { createRouter, createWebHistory } from '@ionic/vue-router'; import { RouteRecordRaw } from 'vue-router'; import Home from '@/views/Home.vue'; import Login from '@/views/Login.vue'; +import { useAuthStore } from "@/store/auth"; + +const loginGuard = (to: any, from: any, next: any) => { + const authStore = useAuthStore() + if (authStore.isAuthenticated) { + // if route has redirectUrl, stop it there for showing session confirmation popup + if (to.query?.redirectUrl) next() + else next('/home') + } + next(); +}; const routes: Array = [ { @@ -17,6 +28,7 @@ const routes: Array = [ path: '/login', name: 'Login', component: Login, + beforeEnter: loginGuard } ]; diff --git a/src/views/Login.vue b/src/views/Login.vue index 174deea..f3a3642 100644 --- a/src/views/Login.vue +++ b/src/views/Login.vue @@ -16,6 +16,11 @@ + + + + +
@@ -54,6 +59,8 @@ import { IonButton, IonChip, IonContent, + IonFab, + IonFabButton, IonIcon, IonInput, IonItem, @@ -65,7 +72,7 @@ import { defineComponent } from "vue"; import { useRouter } from "vue-router"; import { useAuthStore } from "@/store/auth"; import Logo from '@/components/Logo.vue'; -import { arrowForwardOutline } from 'ionicons/icons' +import { arrowForwardOutline, gridOutline } from 'ionicons/icons' import { UserService } from "@/services/UserService"; import { translate } from "@/i18n"; import { showToast } from "@/util"; @@ -77,6 +84,8 @@ export default defineComponent({ IonButton, IonChip, IonContent, + IonFab, + IonFabButton, IonIcon, IonInput, IonItem, @@ -112,8 +121,13 @@ export default defineComponent({ return } - // show OMS input field if query has OMS or if both query and state does not have OMS - if (this.$route.query?.oms || this.authStore.getOMS) { + // logout from Launchpad if logged out from the app + if (this.$route.query?.isLoggedOut === 'true') { + this.authStore.logout() + } + + // show OMS input field if query or state does not have OMS + if (this.$route.query?.oms || !this.authStore.getOMS) { this.showOmsInput = true } @@ -127,9 +141,11 @@ export default defineComponent({ this.authStore.setRedirectUrl(this.$route.query.redirectUrl as string) } - // if a session is already active, present alerts based on redirectUrl being sent - await this.handleActiveSessionLogin() - + // if a session is already active, present alert + if (this.authStore.isAuthenticated && this.$route.query?.redirectUrl) { + await this.confirmActvSessnLoginOnRedrct() + } + this.instanceUrl = this.authStore.oms; if (this.authStore.oms) { // If the current URL is available in alias show it for consistency @@ -142,7 +158,6 @@ export default defineComponent({ } this.dismissLoader(); this.hideBackground = false - }, async presentLoader(message: string) { if (!this.loader) { @@ -234,14 +249,7 @@ export default defineComponent({ console.error(error) } }, - async handleActiveSessionLogin() { - if (this.authStore.isAuthenticated) { - // optional true parameter for redirectUrl case - if (this.$route.query?.redirectUrl) await this.confirmActiveSessionLogin(true) - else await this.confirmActiveSessionLogin() - } - }, - async confirmActiveSessionLogin(redirect?: boolean) { + async confirmActvSessnLoginOnRedrct() { this.isConfirmingForActiveSession = true const alert = await alertController .create({ @@ -252,9 +260,7 @@ export default defineComponent({ buttons: [{ text: translate('Continue'), handler: () => { - redirect - ? window.location.href = `${this.authStore.getRedirectUrl}?oms=${this.authStore.oms}&token=${this.authStore.token.value}&expirationTime=${this.authStore.token.expiration}` - : this.router.push('/') + window.location.href = `${this.authStore.getRedirectUrl}?oms=${this.authStore.oms}&token=${this.authStore.token.value}&expirationTime=${this.authStore.token.expiration}` this.isConfirmingForActiveSession = false; } }, { @@ -262,9 +268,7 @@ export default defineComponent({ handler: async () => { const redirectUrl = this.authStore.getRedirectUrl await this.authStore.logout() - // re-set the redirectUrl if redirect flow was called - // as it got cleared on logout - if (redirect) this.authStore.setRedirectUrl(redirectUrl) + this.authStore.setRedirectUrl(redirectUrl) this.isConfirmingForActiveSession = false; } }] @@ -278,6 +282,7 @@ export default defineComponent({ return { arrowForwardOutline, authStore, + gridOutline, router }; } From bc81002df6b106ddd9ed4f517a2adf532b992922 Mon Sep 17 00:00:00 2001 From: k2maan Date: Thu, 3 Aug 2023 10:29:10 +0530 Subject: [PATCH 2/2] Improved: condition checks in loginGuard --- src/router/index.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/router/index.ts b/src/router/index.ts index 51e8b6c..1016946 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -6,10 +6,8 @@ import { useAuthStore } from "@/store/auth"; const loginGuard = (to: any, from: any, next: any) => { const authStore = useAuthStore() - if (authStore.isAuthenticated) { - // if route has redirectUrl, stop it there for showing session confirmation popup - if (to.query?.redirectUrl) next() - else next('/home') + if (authStore.isAuthenticated && !to.query?.redirectUrl) { + next('/home') } next(); };