diff --git a/src/App.vue b/src/App.vue index 3ae6932e4..c751f9afe 100644 --- a/src/App.vue +++ b/src/App.vue @@ -26,14 +26,17 @@ 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 currently loader is not present then creating a new loader 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(); @@ -46,7 +49,8 @@ export default defineComponent({ } }, async unauthorised() { - this.store.dispatch("user/logout"); + // 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}` } diff --git a/src/adapter/index.ts b/src/adapter/index.ts index 2d936fb37..deea40417 100644 --- a/src/adapter/index.ts +++ b/src/adapter/index.ts @@ -1,11 +1,13 @@ import { api, client, + getConfig, hasError, getUserFacilities, getNotificationEnumIds, getNotificationUserPrefTypeIds, initialise, + logout, resetConfig, removeClientRegistrationToken, storeClientRegistrationToken, @@ -18,11 +20,13 @@ import { export { api, client, + getConfig, hasError, getUserFacilities, getNotificationEnumIds, getNotificationUserPrefTypeIds, initialise, + logout, resetConfig, removeClientRegistrationToken, storeClientRegistrationToken, diff --git a/src/locales/en.json b/src/locales/en.json index e0adbfd23..76991c243 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -55,6 +55,7 @@ "Loading": "Loading", "Login": "Login", "Logging in": "Logging in", + "Logging out": "Logging out", "Logout": "Logout", "Mismatch": "Mismatch", "More": "More", diff --git a/src/locales/es.json b/src/locales/es.json index ca63ff360..37745ac9a 100644 --- a/src/locales/es.json +++ b/src/locales/es.json @@ -55,6 +55,7 @@ "Loading": "Cargando", "Login": "Iniciar sesión", "Logging in": "Logging in", + "Logging out": "Logging out", "Logout": "Cerrar sesión", "Mismatch": "Desajuste", "More": "Más", diff --git a/src/locales/ja.json b/src/locales/ja.json index fa909b0bf..06358ccce 100644 --- a/src/locales/ja.json +++ b/src/locales/ja.json @@ -55,6 +55,7 @@ "Loading": "読み込み中", "Login": "ログイン", "Logging in": "Logging in", + "Logging out": "Logging out", "Logout": "ログアウト", "Mismatch": "不一致", "More": "More", diff --git a/src/store/modules/user/actions.ts b/src/store/modules/user/actions.ts index b8b5fd758..af4af5975 100644 --- a/src/store/modules/user/actions.ts +++ b/src/store/modules/user/actions.ts @@ -7,7 +7,7 @@ import * as types from './mutation-types' import { showToast } from '@/utils' import i18n, { translate } from '@/i18n' import { DateTime, Settings } from 'luxon'; -import { hasError, updateInstanceUrl, updateToken, resetConfig, getUserFacilities } from '@/adapter' +import { hasError, logout, updateInstanceUrl, updateToken, resetConfig, getUserFacilities } from '@/adapter' import { getServerPermissionsFromRules, prepareAppPermissions, @@ -21,6 +21,7 @@ import { storeClientRegistrationToken } from '@/adapter'; import { generateDeviceId, generateTopicName } from '@/utils/firebase' +import emitter from '@/event-bus' const actions: ActionTree = { @@ -109,7 +110,32 @@ const actions: ActionTree = { /** * Logout user */ - async logout ({ commit, dispatch }) { + async logout ({ commit, dispatch }, 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) { + 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() // TODO add any other tasks if need dispatch("product/clearProducts", null, { root: true }) @@ -121,6 +147,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/user-utils/index.ts b/src/user-utils/index.ts deleted file mode 100644 index e69de29bb..000000000 diff --git a/src/utils/user.ts b/src/utils/user.ts index faf98e48b..44c37aba1 100644 --- a/src/utils/user.ts +++ b/src/utils/user.ts @@ -4,7 +4,7 @@ import { loadingController } from '@ionic/vue' const login = async (payload: any) => store.dispatch('user/login', payload); -const logout = async () => store.dispatch('user/logout'); +const logout = async (payload: any) => store.dispatch('user/logout', payload); const loader = { value: null as any, diff --git a/src/views/Settings.vue b/src/views/Settings.vue index 6f742d11f..7e0ae62fd 100644 --- a/src/views/Settings.vue +++ b/src/views/Settings.vue @@ -355,9 +355,12 @@ export default defineComponent({ console.error(error) } - this.store.dispatch('user/logout').then(() => { - const redirectUrl = window.location.origin + '/login' - window.location.href = `${process.env.VUE_APP_LOGIN_URL}?isLoggedOut=true&redirectUrl=${redirectUrl}` + this.store.dispatch('user/logout', { isUserUnauthorised: false }).then((redirectionUrl) => { + // 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() {