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

fix(react-spa-bff): Enhance broadcaster with subpath handling #17212

Merged
merged 8 commits into from
Dec 12, 2024
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
4 changes: 3 additions & 1 deletion libs/react-spa/bff/src/lib/BffPoller.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export const BffPoller = ({
const { signIn, bffUrlGenerator } = useAuth()
const userInfo = useUserInfo()
const { postMessage } = useBffBroadcaster()
const bffBaseUrl = bffUrlGenerator()

const url = useMemo(
() => bffUrlGenerator('/user', { refresh: 'true' }),
Expand Down Expand Up @@ -86,12 +87,13 @@ export const BffPoller = ({
postMessage({
type: BffBroadcastEvents.NEW_SESSION,
userInfo: newUser,
bffBaseUrl,
})

newSessionCb()
}
}
}, [newUser, error, userInfo, signIn, postMessage, newSessionCb])
}, [newUser, error, userInfo, signIn, postMessage, newSessionCb, bffBaseUrl])

return children
}
52 changes: 33 additions & 19 deletions libs/react-spa/bff/src/lib/BffProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,37 @@ export const BffProvider = ({
authState === 'logging-out'
const isLoggedIn = authState === 'logged-in'
const oldLoginPath = `${applicationBasePath}/login`
const bffBaseUrl = bffUrlGenerator()

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)
/**
* Filter broadcast events by matching BFF base url
*
* Since the Broadcaster sends messages to all tabs/windows/iframes
* sharing the same origin (domain), we need to explicitly check if
* the message belongs to our specific BFF instance by comparing base urls.
* This prevents handling events meant for other applications/contexts
* running on the same domain.
*/
if (event.data.bffBaseUrl === bffBaseUrl) {
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 +83,10 @@ export const BffProvider = ({
postMessage({
type: BffBroadcastEvents.NEW_SESSION,
userInfo: state.userInfo,
bffBaseUrl,
})
}
}, [postMessage, state.userInfo, isLoggedIn])
}, [postMessage, state.userInfo, isLoggedIn, bffBaseUrl])

/**
* Builds authentication query parameters for login redirection:
Expand Down Expand Up @@ -175,12 +188,13 @@ export const BffProvider = ({
// Broadcast to all tabs/windows/iframes that the user is logging out
postMessage({
type: BffBroadcastEvents.LOGOUT,
bffBaseUrl,
})

window.location.href = bffUrlGenerator('/logout', {
sid: state.userInfo.profile.sid,
})
}, [bffUrlGenerator, postMessage, state.userInfo])
}, [bffUrlGenerator, postMessage, state.userInfo, bffBaseUrl])

const switchUser = useCallback(
(nationalId?: string) => {
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
bffBaseUrl: string
}

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

export type BffBroadcastEvent = NewSessionEvent | LogoutEvent
Expand Down
Loading