diff --git a/.env.example b/.env.example index f6c18e05..94214074 100644 --- a/.env.example +++ b/.env.example @@ -5,7 +5,6 @@ VUE_APP_VIEW_SIZE=10 VUE_APP_BASE_URL= VUE_APP_PERMISSION_ID= VUE_APP_ALIAS={} -VUE_APP_DEFAULT_LOG_LEVEL="error" VUE_APP_MAPPING_TYPES={"IMPORD": "IMP_ORD_MAPPING_PREF","EXPORD": "EXP_PKD_ORD_MAPPING_PREF"} VUE_APP_MAPPING_IMPORD={"orderId": { "label": "Order ID", "value": "" }, "facilityId": { "label": "Facility ID", "value": "" },"trackingCode": { "label": "Tracking Code", "value": "" }} VUE_APP_MAPPING_EXPORD={"shipment-id": { "label": "Shipment ID", "value": "" }, "order-id": { "label": "Order ID", "value": "" },"to-name": { "label": "To Name", "value": "" },"address1": { "label": "Address 1", "value": "" },"address2": { "label": "Address 2", "value": "" },"city": { "label": "City", "value": "" },"state": { "label": "State", "value": "" },"zip-code": { "label": "Zip Code", "value": "" },"country-code": { "label": "Country Code", "value": "" },"full-to-address": { "label": "Full Address", "value": "" },"phone-number": { "label": "Phone Number", "value": "" },"email-address": { "label": "Email", "value": "" },"weight": { "label": "Weight", "value": "" },"quantity": { "label": "Quantity", "value": "" },"product-name": { "label": "Product Name", "value": "" },"product-sku": { "label": "Product Sku", "value": "" },"shipping-method": { "label": "Shipping Method", "value": "" },"facility-name": { "label": "Facility Name", "value": "" },"facility-address1": { "label": "Facility Address 1", "value": "" },"facility-address2": { "label": "Facility Address 2", "value": "" },"facility-city": { "label": "Facility City", "value": "" },"facility-state": { "label": "Facility State", "value": "" },"facility-zip-code": { "label": "Facility Zip Code", "value": "" },"facility-phone": { "label": "Facility Phone", "value": "" },"facility-full-address": { "label": "Facility Full Address", "value": "" }} diff --git a/src/App.vue b/src/App.vue index 77236b5f..e98d5a56 100644 --- a/src/App.vue +++ b/src/App.vue @@ -41,13 +41,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(); @@ -59,7 +62,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 f120d007..b52ba8c6 100644 --- a/src/adapter/index.ts +++ b/src/adapter/index.ts @@ -1,4 +1,4 @@ -import { api, client, getConfig, hasError, initialise, resetConfig, updateInstanceUrl, updateToken, getUserFacilities } from '@hotwax/oms-api' +import { api, client, getConfig, hasError, initialise, logout, resetConfig, updateInstanceUrl, updateToken, getUserFacilities } from '@hotwax/oms-api' export { api, @@ -6,6 +6,7 @@ export { getConfig, hasError, initialise, + logout, resetConfig, updateInstanceUrl, updateToken, diff --git a/src/locales/en.json b/src/locales/en.json index d2130aa1..cb28d56a 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -104,6 +104,7 @@ "Loading": "Loading", "Login": "Login", "Logging in": "Logging in", + "Logging out": "Logging out", "Logout": "Logout", "Loyalty": "Loyalty", "Make sure all the labels provided are correct.": "Make sure all the labels provided are correct.", diff --git a/src/locales/es.json b/src/locales/es.json index 031e46aa..960e19f1 100644 --- a/src/locales/es.json +++ b/src/locales/es.json @@ -104,6 +104,7 @@ "Loading": "Cargando", "Login": "Iniciar Sesión", "Logging in": "Logging in", + "Logging out": "Logging out", "Logout": "Cerrar Sesión", "Loyalty": "Lealtad", "Make sure all the labels provided are correct.": "Asegúrate de que todas las etiquetas proporcionadas sean correctas.", diff --git a/src/store/modules/user/actions.ts b/src/store/modules/user/actions.ts index d825dd60..9dd9b62e 100644 --- a/src/store/modules/user/actions.ts +++ b/src/store/modules/user/actions.ts @@ -8,10 +8,11 @@ import { showToast } from '@/utils' import { hasError } from '@/adapter' import i18n, { translate } from '@/i18n' import { Settings } from 'luxon' -import { updateInstanceUrl, updateToken, resetConfig, getUserFacilities } from '@/adapter' +import { logout, updateInstanceUrl, updateToken, resetConfig, getUserFacilities } from '@/adapter' import logger from '@/logger' import { getServerPermissionsFromRules, prepareAppPermissions, resetPermissions, setPermissions } from '@/authorization' import { useAuthStore } from '@hotwax/dxp-components' +import emitter from '@/event-bus' const actions: ActionTree = { @@ -113,7 +114,25 @@ const actions: ActionTree = { /** * Logout user */ - async logout ({ commit }) { + 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) { + 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 + } + } + const authStore = useAuthStore() // TODO add any other tasks if need commit(types.USER_END_SESSION) @@ -123,6 +142,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 index faf98e48..44c37aba 100644 --- a/src/user-utils/index.ts +++ b/src/user-utils/index.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 05226d0f..8d01f0a8 100644 --- a/src/views/Settings.vue +++ b/src/views/Settings.vue @@ -298,10 +298,14 @@ export default defineComponent({ } }, logout () { - this.store.dispatch('user/logout').then(() => { + this.store.dispatch('user/logout', { isUserUnauthorised: false }).then((redirectionUrl) => { this.store.dispatch('order/clearOrders') - 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() {