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 #276

Merged
merged 5 commits into from
Oct 11, 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
12 changes: 8 additions & 4 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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}`
}
Expand Down
4 changes: 4 additions & 0 deletions src/adapter/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {
api,
client,
getConfig,
hasError,
getUserFacilities,
getNotificationEnumIds,
getNotificationUserPrefTypeIds,
initialise,
logout,
resetConfig,
removeClientRegistrationToken,
storeClientRegistrationToken,
Expand All @@ -18,11 +20,13 @@ import {
export {
api,
client,
getConfig,
hasError,
getUserFacilities,
getNotificationEnumIds,
getNotificationUserPrefTypeIds,
initialise,
logout,
resetConfig,
removeClientRegistrationToken,
storeClientRegistrationToken,
Expand Down
1 change: 1 addition & 0 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"Loading": "Loading",
"Login": "Login",
"Logging in": "Logging in",
"Logging out": "Logging out",
"Logout": "Logout",
"Mismatch": "Mismatch",
"More": "More",
Expand Down
1 change: 1 addition & 0 deletions src/locales/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"Loading": "Cargando",
"Login": "Iniciar sesión",
"Logging in": "Logging in",
"Logging out": "Logging out",
"Logout": "Cerrar sesión",
"Mismatch": "Desajuste",
"More": "Más",
Expand Down
1 change: 1 addition & 0 deletions src/locales/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"Loading": "読み込み中",
"Login": "ログイン",
"Logging in": "Logging in",
"Logging out": "Logging out",
"Logout": "ログアウト",
"Mismatch": "不一致",
"More": "More",
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 @@ -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,
Expand All @@ -21,6 +21,7 @@ import {
storeClientRegistrationToken
} from '@/adapter';
import { generateDeviceId, generateTopicName } from '@/utils/firebase'
import emitter from '@/event-bus'

const actions: ActionTree<UserState, RootState> = {

Expand Down Expand Up @@ -105,7 +106,32 @@ const actions: ActionTree<UserState, RootState> = {
/**
* 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 })
Expand All @@ -116,6 +142,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
Empty file removed src/user-utils/index.ts
Empty file.
2 changes: 1 addition & 1 deletion src/utils/user.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 @@ -305,9 +305,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() {
Expand Down
Loading