Skip to content

Commit

Permalink
Merge pull request #235 from ymaheshwari1/preorder/single-logout
Browse files Browse the repository at this point in the history
Improved: code to redirect the user to SSO screen when enabled otherwise redirect to launchpad on logout
  • Loading branch information
ravilodhi authored Sep 21, 2023
2 parents befbe3e + d37c4b9 commit 50f3a35
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
23 changes: 22 additions & 1 deletion src/store/modules/user/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Settings } from 'luxon'
import { updateInstanceUrl, updateToken, resetConfig, logout } from '@/adapter'
import { useAuthStore } from '@hotwax/dxp-components';
import { getServerPermissionsFromRules, prepareAppPermissions, resetPermissions, setPermissions } from '@/authorization'
import emitter from '@/event-bus'

const actions: ActionTree<UserState, RootState> = {

Expand Down Expand Up @@ -83,10 +84,21 @@ const actions: ActionTree<UserState, RootState> = {
* 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')
// 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 = 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()
Expand All @@ -103,6 +115,15 @@ const actions: ActionTree<UserState, RootState> = {

// reset plugin state on logout
authStore.$reset()

emitter.emit('dismissLoader')
// If we get any url in logout api resp then we will redirect the user to the url
if(redirectionUrl) {
window.location.href = redirectionUrl
return redirectionUrl;
}

return '';
},

/**
Expand Down
9 changes: 6 additions & 3 deletions src/views/settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,12 @@ export default defineComponent({
},
methods: {
logout: function() {
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

0 comments on commit 50f3a35

Please sign in to comment.