Skip to content

Commit

Permalink
Merge pull request #163 from sanskar345/singleLogout
Browse files Browse the repository at this point in the history
Implemented: support for single logout
  • Loading branch information
ravilodhi authored Oct 11, 2023
2 parents 633601b + aef5599 commit c659720
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 12 deletions.
12 changes: 8 additions & 4 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,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 All @@ -50,7 +53,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}`
}
Expand Down
3 changes: 2 additions & 1 deletion src/adapter/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { api, client, getConfig, initialise, resetConfig, updateInstanceUrl, updateToken } from '@hotwax/oms-api'
import { api, client, getConfig, initialise, logout, resetConfig, updateInstanceUrl, updateToken } from '@hotwax/oms-api'

export {
api,
client,
getConfig,
initialise,
logout,
resetConfig,
updateInstanceUrl,
updateToken
Expand Down
1 change: 1 addition & 0 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"Loading": "Loading",
"Login": "Login",
"Logging in": "Logging in",
"Logging out": "Logging out",
"Logout": "Logout",
"No item has been picked": "No item has been picked",
"No permission": "No permission",
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import i18n from './i18n'
import store from './store'
import { dxpComponents } from '@hotwax/dxp-components'
import { login, logout, loader } from './user-utils';
import { getConfig, initialise } from '@hotwax/oms-api';
import { getConfig, initialise } from '@/adapter'

const app = createApp(App)
.use(IonicVue, {
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 @@ -6,8 +6,9 @@ import * as types from './mutation-types'
import { hasError, showToast } from '@/utils'
import { translate } from '@/i18n'
import { Settings } from 'luxon';
import { updateInstanceUrl, updateToken, resetConfig } from '@/adapter'
import { logout, updateInstanceUrl, updateToken, resetConfig } from '@/adapter'
import { useAuthStore } from '@hotwax/dxp-components'
import emitter from '@/event-bus'

const actions: ActionTree<UserState, RootState> = {

Expand Down Expand Up @@ -58,7 +59,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) {
console.error('Error parsing data', err)
}

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

const authStore = useAuthStore()
// TODO add any other tasks if need
commit(types.USER_END_SESSION)
Expand All @@ -70,6 +96,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
10 changes: 7 additions & 3 deletions src/views/Settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,14 @@ export default defineComponent({
return timeZoneModal.present();
},
logout () {
this.store.dispatch('user/logout').then(() => {
this.store.dispatch('user/logout', { isUserUnauthorised: false }).then((redirectionUrl) => {
this.store.dispatch('picklist/clearPicklist')
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}`
}
})
},
setFacility (facility: any) {
Expand Down

0 comments on commit c659720

Please sign in to comment.