From f5f48773483bbf6b39960469151617a3e950c1d0 Mon Sep 17 00:00:00 2001 From: Sanskar Soni Date: Tue, 10 Oct 2023 10:57:13 +0530 Subject: [PATCH 1/2] Implemented: support for single logout --- src/App.vue | 13 ++++++++----- src/locales/en.json | 1 + src/store/modules/user/actions.ts | 30 +++++++++++++++++++++++++++++- src/views/Settings.vue | 21 +++++++++++++++------ 4 files changed, 53 insertions(+), 12 deletions(-) diff --git a/src/App.vue b/src/App.vue index 15ff14da..2d37660c 100644 --- a/src/App.vue +++ b/src/App.vue @@ -42,13 +42,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(); @@ -62,8 +65,8 @@ export default defineComponent({ async unauthorized() { // Mark the user as unauthorised, this will help in not making the logout api call in actions this.store.dispatch("user/logout", { isUserUnauthorised: true }); - const redirectUrl = window.location.origin + '/login'; - window.location.href = `${process.env.VUE_APP_LOGIN_URL}?redirectUrl=${redirectUrl}`; + const redirectUrl = window.location.origin + '/login' + window.location.href = `${process.env.VUE_APP_LOGIN_URL}?redirectUrl=${redirectUrl}` } }, created() { diff --git a/src/locales/en.json b/src/locales/en.json index 8f41e7d0..a993c0d4 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -39,6 +39,7 @@ "Loading": "Loading", "Login": "Login", "Logging in": "Logging in", + "Logging out": "Logging out", "Logout": "Logout", "Make sure you have entered all the inventory you received. You cannot edit this information after proceeding.": "Make sure you have entered all the inventory you received. { space } You cannot edit this information after proceeding.", "No more shipments found": "No more shipments found", diff --git a/src/store/modules/user/actions.ts b/src/store/modules/user/actions.ts index 7e25d60b..fe117723 100644 --- a/src/store/modules/user/actions.ts +++ b/src/store/modules/user/actions.ts @@ -14,6 +14,7 @@ import { setPermissions } from '@/authorization' import { useAuthStore } from '@hotwax/dxp-components' +import emitter from '@/event-bus' const actions: ActionTree = { @@ -104,10 +105,29 @@ const actions: ActionTree = { * Logout user */ async logout ({ commit }, payload) { + // 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; + + // wrapping the parsing logic in try catch as in some case the logout api makes redirection, and then we are unable to parse the resp and thus the logout process halts + try { + 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) + } catch(err) { + console.error('Error parsing data', err) + } + + if(resp?.logoutAuthType == 'SAML2SSO') { + redirectionUrl = resp.logoutUrl + } } const authStore = useAuthStore() @@ -121,6 +141,14 @@ const actions: ActionTree = { // reset plugin state on logout authStore.$reset() + + // 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; }, /** diff --git a/src/views/Settings.vue b/src/views/Settings.vue index 70e9b899..93166e39 100644 --- a/src/views/Settings.vue +++ b/src/views/Settings.vue @@ -219,9 +219,14 @@ export default defineComponent({ { text: this.$t('Ok'), handler: () => { - this.store.dispatch('product/clearUploadProducts'); - this.store.dispatch('user/logout').then(() => { - this.router.push('/login'); + this.store.dispatch('user/logout', { isUserUnauthorised: false }).then((redirectionUrl) => { + this.store.dispatch('product/clearUploadProducts'); + + // if not having redirection url then redirect the user to launchpad + if (!redirectionUrl) { + const redirectUrl = window.location.origin + '/login' + window.location.href = `${process.env.VUE_APP_LOGIN_URL}?isLoggedOut=true&redirectUrl=${redirectUrl}` + } }) } }] @@ -229,12 +234,16 @@ export default defineComponent({ await alert.present(); }, logout () { - this.store.dispatch('user/logout').then(() => { + this.store.dispatch('user/logout', { isUserUnauthorised: false }).then((redirectionUrl) => { this.store.dispatch('shipment/clearShipments'); this.store.dispatch('return/clearReturns'); this.store.dispatch("party/resetReceiversDetails"); - const redirectUrl = window.location.origin + '/login' - window.location.href = `${process.env.VUE_APP_LOGIN_URL}?isLoggedOut=true&redirectUrl=${redirectUrl}` + + // if not having redirection url then redirect the user to launchpad + if (!redirectionUrl) { + const redirectUrl = window.location.origin + '/login' + window.location.href = `${process.env.VUE_APP_LOGIN_URL}?isLoggedOut=true&redirectUrl=${redirectUrl}` + } }) }, goToLaunchpad() { From 070e45c7b61044e764c2a8adaf092bec1535f3d8 Mon Sep 17 00:00:00 2001 From: Sanskar Soni Date: Tue, 10 Oct 2023 11:00:34 +0530 Subject: [PATCH 2/2] Improved: indentation --- src/App.vue | 4 ++-- src/views/Settings.vue | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/App.vue b/src/App.vue index 2d37660c..cc299a54 100644 --- a/src/App.vue +++ b/src/App.vue @@ -65,8 +65,8 @@ export default defineComponent({ async unauthorized() { // Mark the user as unauthorised, this will help in not making the logout api call in actions this.store.dispatch("user/logout", { isUserUnauthorised: true }); - const redirectUrl = window.location.origin + '/login' - window.location.href = `${process.env.VUE_APP_LOGIN_URL}?redirectUrl=${redirectUrl}` + const redirectUrl = window.location.origin + '/login'; + window.location.href = `${process.env.VUE_APP_LOGIN_URL}?redirectUrl=${redirectUrl}`; } }, created() { diff --git a/src/views/Settings.vue b/src/views/Settings.vue index 93166e39..10402778 100644 --- a/src/views/Settings.vue +++ b/src/views/Settings.vue @@ -233,8 +233,8 @@ export default defineComponent({ }); await alert.present(); }, - logout () { - this.store.dispatch('user/logout', { isUserUnauthorised: false }).then((redirectionUrl) => { + logout() { + this.store.dispatch('user/logout', { isUserUnauthorised: false }).then((redirectionUrl) => { this.store.dispatch('shipment/clearShipments'); this.store.dispatch('return/clearReturns'); this.store.dispatch("party/resetReceiversDetails");