From 2cedf20c4f91bf3bd7616ad3c98ab0db5aaae3da Mon Sep 17 00:00:00 2001 From: Yash Maheshwari Date: Fri, 22 Sep 2023 15:27:44 +0530 Subject: [PATCH 1/2] Improved: code to redirect the user to SSO screen when enabled otherwise redirect to login, and added support to loader for custom message in case of logout --- src/App.vue | 19 +++++++++++++++---- src/locales/en.json | 1 + src/store/auth.ts | 23 ++++++++++++++++++++++- src/views/Login.vue | 8 +++++--- 4 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src/App.vue b/src/App.vue index 06a7c86..a681b45 100644 --- a/src/App.vue +++ b/src/App.vue @@ -25,13 +25,16 @@ export default defineComponent({ } }, methods: { - async presentLoader() { + async presentLoader(options = { message: '', backdropDismiss: true }) { + // When having a custom message remove already existing loader + if(options.message && this.loader) this.dismissLoader(); + if (!this.loader) { this.loader = await loadingController .create({ - message: this.$t("Click the backdrop to dismiss."), + message: options.message ? this.$t(options.message) : this.$t("Click the backdrop to dismiss."), translucent: true, - backdropDismiss: true + backdropDismiss: options.backdropDismiss }); } this.loader.present(); @@ -67,7 +70,7 @@ export default defineComponent({ }, async unauthorized() { // Mark the user as unauthorised, this will help in not making the logout api call in actions - this.authStore.logout({ isUserUnauthorised: true }) + await this.authStore.logout({ isUserUnauthorised: true }) this.router.push("/login") } }, @@ -87,6 +90,14 @@ export default defineComponent({ } }) }, + mounted() { + emitter.on('presentLoader', this.presentLoader) + emitter.on('dismissLoader', this.dismissLoader) + }, + unmounted() { + emitter.off('presentLoader', this.presentLoader) + emitter.off('dismissLoader', this.dismissLoader) + }, setup () { const router = useRouter(); const authStore = useAuthStore(); diff --git a/src/locales/en.json b/src/locales/en.json index 5c75a25..ea2f513 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -4,6 +4,7 @@ "Continue": "Continue", "Re-login": "Re-login", "Launch Pad": "Launch Pad", + "Logging out": "Logging out", "Login": "Login", "Logout": "Logout", "Next": "Next", diff --git a/src/store/auth.ts b/src/store/auth.ts index 33564a9..3bdd002 100644 --- a/src/store/auth.ts +++ b/src/store/auth.ts @@ -4,6 +4,7 @@ import { UserService } from '@/services/UserService'; import { hasError, logout, updateInstanceUrl, updateToken } from '@/adapter'; import { showToast } from '@/util'; import { translate } from '@/i18n' +import emitter from '@/event-bus'; export const useAuthStore = defineStore('authStore', { state: () => ({ @@ -87,10 +88,22 @@ export const useAuthStore = defineStore('authStore', { } }, async logout(payload?: any) { + // store the url on which we need to redirect the user after logout api completes in case of SSO enabled + let redirectionUrl = '' + + emitter.emit('presentLoader', { message: 'Logging out', backdropDismiss: false }) + // Calling the logout api to flag the user as logged out, only when user is authorised // if the user is already unauthorised then not calling the logout api as it returns 401 again that results in a loop, thus there is no need to call logout api if the user is unauthorised if(!payload?.isUserUnauthorised) { - await logout(); + let resp = await logout(); + + // Added logic to remove the `//` from the resp as in case of get request we are having the extra characters and in case of post we are having 403 + resp = JSON.parse(resp.startsWith('//') ? resp.replace('//', '') : resp) + + if(resp.logoutAuthType == 'SAML2SSO') { + redirectionUrl = resp.logoutUrl + } } // resetting the whole state except oms @@ -102,6 +115,14 @@ export const useAuthStore = defineStore('authStore', { } this.redirectUrl = '' updateToken(''); + + // If we get any url in logout api resp then we will redirect the user to the url + if(redirectionUrl) { + window.location.href = redirectionUrl + } + + emitter.emit('dismissLoader') + return redirectionUrl; } }, persist: true diff --git a/src/views/Login.vue b/src/views/Login.vue index f65c7a8..606b231 100644 --- a/src/views/Login.vue +++ b/src/views/Login.vue @@ -126,7 +126,7 @@ export default defineComponent({ // logout from Launchpad if logged out from the app if (this.$route.query?.isLoggedOut === 'true') { // We will already mark the user as unuauthorised when log-out from the app - this.authStore.logout({ isUserUnauthorised: true }) + await this.authStore.logout({ isUserUnauthorised: true }) } // fetch login options only if OMS is there as API calls require OMS @@ -276,9 +276,11 @@ export default defineComponent({ text: translate('Re-login'), handler: async () => { const redirectUrl = this.authStore.getRedirectUrl - await this.authStore.logout() + let redirectionUrl = await this.authStore.logout() + if(!redirectionUrl) { + this.isConfirmingForActiveSession = false; + } this.authStore.setRedirectUrl(redirectUrl) - this.isConfirmingForActiveSession = false; } }] }); From 8b194c6a03117836a7d576b7f9fcca2b36dc9a25 Mon Sep 17 00:00:00 2001 From: Yash Maheshwari Date: Mon, 25 Sep 2023 10:12:02 +0530 Subject: [PATCH 2/2] Improved: type for presentLoader params and added missing label in en.json --- src/App.vue | 10 ++++++++-- src/locales/en.json | 1 + 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/App.vue b/src/App.vue index a681b45..3fef0ac 100644 --- a/src/App.vue +++ b/src/App.vue @@ -25,7 +25,7 @@ export default defineComponent({ } }, methods: { - async presentLoader(options = { message: '', backdropDismiss: true }) { + async presentLoader(options = { message: '', backdropDismiss: true } as any) { // When having a custom message remove already existing loader if(options.message && this.loader) this.dismissLoader(); @@ -90,7 +90,13 @@ export default defineComponent({ } }) }, - mounted() { + async mounted() { + this.loader = await loadingController + .create({ + message: this.$t("Click the backdrop to dismiss."), + translucent: true, + backdropDismiss: true + }); emitter.on('presentLoader', this.presentLoader) emitter.on('dismissLoader', this.dismissLoader) }, diff --git a/src/locales/en.json b/src/locales/en.json index ea2f513..0a88c85 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -1,6 +1,7 @@ { "Already active session": "Already active session", "A session for is already active for. Do you want to continue or login again?": "A session for {partyName} is already active for {oms}. Do you want to continue or login again?", + "Click the backdrop to dismiss.": "Click the backdrop to dismiss.", "Continue": "Continue", "Re-login": "Re-login", "Launch Pad": "Launch Pad",