Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented: support for single logout #226

Merged
merged 5 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ VUE_APP_VIEW_SIZE=10
VUE_APP_DATE_FORMAT=MM/dd/yyyy
VUE_APP_PERMISSION_ID=
VUE_APP_ALIAS={}
VUE_APP_DEFAULT_LOG_LEVEL="error"
VUE_APP_MAPPING_TYPES={"PO": "PO_MAPPING_PREF","RSTINV": "INV_MAPPING_PREF"}
VUE_APP_MAPPING_PO={"orderId": { "label": "Order ID", "required": true }, "productSku": { "label": "Shopify product SKU", "required": true },"orderDate": { "label": "Arrival date", "required": true }, "quantity": { "label": "Ordered quantity", "required": true }, "facility": { "label": "Facility ID", "required": true }}
VUE_APP_MAPPING_RSTINV={"productSku": { "label": "Product SKU", "required": true }, "quantity": { "label": "Quantity", "required": true }, "facility": { "label": "Facility ID", "required": true }, "locationSeqId": { "label": "Facility Location", "required": true }}
Expand Down
12 changes: 8 additions & 4 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,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();
Expand Down Expand Up @@ -81,7 +84,8 @@ export default defineComponent({
showToast(translate("New version available, please update the app."));
},
async unauthorized() {
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}`;
}
Expand Down
2 changes: 2 additions & 0 deletions src/adapter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
getConfig,
fetchProducts,
initialise,
logout,
isError,
Product,
resetConfig,
Expand All @@ -17,6 +18,7 @@ export {
getConfig,
fetchProducts,
initialise,
logout,
isError,
Product,
resetConfig,
Expand Down
2 changes: 1 addition & 1 deletion src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
"items": "items",
"items selected": "items selected",
"Import": "Import",
"Inventory": "Inventory",
"Instance Url": "Instance Url",
"Items": "Items",
"Invalid input": "Invalid input",
Expand All @@ -71,6 +70,7 @@
"Loading": "Loading",
"Login": "Login",
"Logging in": "Logging in",
"Logging out": "Logging out",
"Logout": "Logout",
"Mapping": "Mapping",
"Mapping details": "Mapping details",
Expand Down
38 changes: 36 additions & 2 deletions src/store/modules/user/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import UserState from './UserState'
import * as types from './mutation-types'
import { hasError, showToast } from '@/utils'
import { translate } from '@/i18n'
import { updateInstanceUrl, updateToken, resetConfig } from '@/adapter'
import { logout, updateInstanceUrl, updateToken, resetConfig } from '@/adapter'
import logger from "@/logger";
import { useAuthStore } from '@hotwax/dxp-components';
import emitter from '@/event-bus'

const actions: ActionTree<UserState, RootState> = {

Expand Down Expand Up @@ -61,7 +62,32 @@ const actions: ActionTree<UserState, RootState> = {
/**
* 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;

// 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) {
logger.error('Error parsing data', err)
}

if(resp?.logoutAuthType == 'SAML2SSO') {
redirectionUrl = resp.logoutUrl
}
}

const authStore = useAuthStore()

// TODO add any other tasks if need
Expand All @@ -75,6 +101,14 @@ const actions: ActionTree<UserState, RootState> = {

// 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;
},

/**
Expand Down
2 changes: 1 addition & 1 deletion src/user-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 6 additions & 3 deletions src/views/Settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,12 @@ export default defineComponent({
this.sampleDateTime = DateTime.now().toFormat(this.dateTimeFormat);
},
logout () {
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() {
Expand Down
Loading