Fix SSE event stream errors when impersonating users with limited permissions (#18966)

Clear the SSE client before swapping auth tokens during impersonation,
preventing a userWorkspaceId mismatch between the existing event stream
(created under the admin's identity) and the new impersonation token.
Treat NOT_AUTHORIZED event stream errors as recoverable (destroy +
recreate), matching the existing behavior for
EVENT_STREAM_DOES_NOT_EXIST and EVENT_STREAM_ALREADY_EXISTS.
This commit is contained in:
Thomas Trompette
2026-03-25 18:35:54 +01:00
committed by GitHub
parent d47ddad4c5
commit 511d1bd7ab
2 changed files with 4 additions and 0 deletions
@@ -1,15 +1,18 @@
import { isAppEffectRedirectEnabledState } from '@/app/states/isAppEffectRedirectEnabledState';
import { useAuth } from '@/auth/hooks/useAuth';
import { useClearSseClient } from '@/sse-db-event/hooks/useClearSseClient';
import { useSetAtomState } from '@/ui/utilities/state/jotai/hooks/useSetAtomState';
export const useImpersonationAuth = () => {
const { getAuthTokensFromLoginToken } = useAuth();
const { clearSseClient } = useClearSseClient();
const setIsAppEffectRedirectEnabled = useSetAtomState(
isAppEffectRedirectEnabledState,
);
const executeImpersonationAuth = async (loginToken: string) => {
setIsAppEffectRedirectEnabled(false);
clearSseClient();
await getAuthTokensFromLoginToken(loginToken);
setIsAppEffectRedirectEnabled(true);
};
@@ -93,6 +93,7 @@ export const SSEQuerySubscribeEffect = () => {
const isRecoverable =
subCode === 'EVENT_STREAM_DOES_NOT_EXIST' ||
subCode === 'EVENT_STREAM_ALREADY_EXISTS' ||
subCode === 'NOT_AUTHORIZED' ||
code === 'UNAUTHENTICATED' ||
code === 'FORBIDDEN';