Skip to content

Commit

Permalink
feat: enhance session management with subpath handling
Browse files Browse the repository at this point in the history
Add subpath to NewSessionEvent and LogoutEvent types. 
Update BffProvider to include applicationBasePath in 
postMessage dependencies. Modify event handling to 
only act on events matching the current subpath, 
ensuring proper session management across multiple 
tabs/windows/iframes.
  • Loading branch information
snaerth committed Dec 11, 2024
1 parent 5c8c1b9 commit f2df371
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
41 changes: 23 additions & 18 deletions libs/react-spa/bff/src/lib/BffProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,27 @@ export const BffProvider = ({
const oldLoginPath = `${applicationBasePath}/login`

const { postMessage } = useBffBroadcaster((event) => {
if (
isLoggedIn &&
event.data.type === BffBroadcastEvents.NEW_SESSION &&
isNewUser(state.userInfo, event.data.userInfo)
) {
setSessionExpiredScreen(true)
} else if (event.data.type === BffBroadcastEvents.LOGOUT) {
// We will wait 1 seconds before we dispatch logout action.
// The reason is that IDS will not log the user out immediately.
// Note! The bff poller may have triggered logout by that time anyways.
setTimeout(() => {
dispatch({
type: ActionType.LOGGED_OUT,
})

signIn()
}, 1000)
// Only act if subpath matches application base path.
// Reason: Broadcaster broadcasts to all tabs/windows/iframes on the same origin no matter the subpath, so we need to handle this manually.
if (event.data.subpath === applicationBasePath) {
if (
isLoggedIn &&
event.data.type === BffBroadcastEvents.NEW_SESSION &&
isNewUser(state.userInfo, event.data.userInfo)
) {
setSessionExpiredScreen(true)
} else if (event.data.type === BffBroadcastEvents.LOGOUT) {
// We will wait 1 seconds before we dispatch logout action.
// The reason is that IDS will not log the user out immediately.
// Note! The bff poller may have triggered logout by that time anyways.
setTimeout(() => {
dispatch({
type: ActionType.LOGGED_OUT,
})

signIn()
}, 1000)
}
}
})

Expand All @@ -71,9 +75,10 @@ export const BffProvider = ({
postMessage({
type: BffBroadcastEvents.NEW_SESSION,
userInfo: state.userInfo,
subpath: applicationBasePath,
})
}
}, [postMessage, state.userInfo, isLoggedIn])
}, [postMessage, state.userInfo, isLoggedIn, applicationBasePath])

/**
* Builds authentication query parameters for login redirection:
Expand Down
2 changes: 2 additions & 0 deletions libs/react-spa/bff/src/lib/bff.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@ export enum BffBroadcastEvents {
type NewSessionEvent = {
type: BffBroadcastEvents.NEW_SESSION
userInfo: BffUser
subpath: string
}

type LogoutEvent = {
type: BffBroadcastEvents.LOGOUT
subpath: string
}

export type BffBroadcastEvent = NewSessionEvent | LogoutEvent
Expand Down

0 comments on commit f2df371

Please sign in to comment.