-
Notifications
You must be signed in to change notification settings - Fork 417
UX-913: silently refresh embedded auth on stale tabs #2328
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
Open
malinskibeniamin
wants to merge
1
commit into
master
Choose a base branch
from
codex/ux-913-silent-auth-refresh
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import { render } from '@testing-library/react'; | ||
| import type { ReactNode } from 'react'; | ||
|
|
||
| const { mockCreateConnectTransport, mockCreateRouter, mockFetch, mockSetup } = vi.hoisted(() => ({ | ||
| mockCreateConnectTransport: vi.fn((options) => options), | ||
| mockCreateRouter: vi.fn(() => ({ | ||
| invalidate: vi.fn().mockResolvedValue(undefined), | ||
| })), | ||
| mockFetch: vi.fn(), | ||
| mockSetup: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock('@connectrpc/connect-query', () => ({ | ||
| TransportProvider: ({ children }: { children?: ReactNode }) => <>{children}</>, | ||
| })); | ||
|
|
||
| vi.mock('@connectrpc/connect-web', () => ({ | ||
| createConnectTransport: mockCreateConnectTransport, | ||
| })); | ||
|
|
||
| vi.mock('@redpanda-data/ui', () => ({ | ||
| ChakraProvider: ({ children }: { children?: ReactNode }) => <>{children}</>, | ||
| redpandaTheme: {}, | ||
| redpandaToastOptions: {}, | ||
| })); | ||
|
|
||
| vi.mock('@tanstack/react-query', async () => { | ||
| const actual = await vi.importActual('@tanstack/react-query'); | ||
| return { | ||
| ...actual, | ||
| QueryClientProvider: ({ children }: { children?: ReactNode }) => <>{children}</>, | ||
| }; | ||
| }); | ||
|
|
||
| vi.mock('@tanstack/react-router', () => ({ | ||
| createRouter: mockCreateRouter, | ||
| RouterProvider: () => <div data-testid="router-provider" />, | ||
| })); | ||
|
|
||
| vi.mock('custom-feature-flag-provider', () => ({ | ||
| CustomFeatureFlagProvider: ({ children }: { children?: ReactNode }) => <>{children}</>, | ||
| })); | ||
|
|
||
| vi.mock('protobuf-registry', () => ({ | ||
| protobufRegistry: {}, | ||
| })); | ||
|
|
||
| vi.mock('./components/misc/not-found-page', () => ({ | ||
| NotFoundPage: () => <div>Not Found</div>, | ||
| })); | ||
|
|
||
| vi.mock('./config', () => ({ | ||
| addBearerTokenInterceptor: vi.fn((next) => next), | ||
| checkExpiredLicenseInterceptor: vi.fn((next) => next), | ||
| getGrpcBasePath: vi.fn(() => 'http://localhost:9090'), | ||
| setup: mockSetup, | ||
| })); | ||
|
|
||
| vi.mock('./routeTree.gen', () => ({ | ||
| routeTree: {}, | ||
| })); | ||
|
|
||
| vi.mock('./state/app-global', () => ({ | ||
| appGlobal: { | ||
| historyLocation: vi.fn(() => ({ pathname: '/topics' })), | ||
| historyPush: vi.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| vi.mock('./state/backend-api', () => ({ | ||
| api: { | ||
| refreshUserData: vi.fn().mockResolvedValue(undefined), | ||
| }, | ||
| })); | ||
|
|
||
| import EmbeddedApp from './embedded-app'; | ||
|
|
||
| describe('EmbeddedApp', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| test('uses the host-provided fetch for the root dataplane transport', () => { | ||
| render(<EmbeddedApp fetch={mockFetch} isConsoleReadyToMount={true} />); | ||
|
|
||
| expect(mockSetup).toHaveBeenCalled(); | ||
| expect(mockCreateConnectTransport).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| baseUrl: 'http://localhost:9090', | ||
| fetch: mockFetch, | ||
| }) | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,6 +52,12 @@ import { | |
| } from './config'; | ||
| import { routeTree } from './routeTree.gen'; | ||
| import { appGlobal } from './state/app-global'; | ||
| import { api } from './state/backend-api'; | ||
| import { | ||
| getRegisteredTokenRefreshInterceptor, | ||
| TokenRefreshInterceptorProvider, | ||
| } from './utils/token-refresh-interceptor'; | ||
| import { useEmbeddedAuthPrewarm } from './utils/use-embedded-auth-prewarm'; | ||
|
|
||
| // Regex for normalizing paths by removing trailing slashes | ||
| const TRAILING_SLASH_REGEX = /\/+$/; | ||
|
|
@@ -86,6 +92,10 @@ export interface EmbeddedProps extends SetConfigArguments { | |
| } | ||
|
|
||
| function EmbeddedApp({ basePath = '', ...p }: EmbeddedProps) { | ||
| const tokenRefreshInterceptor = getRegisteredTokenRefreshInterceptor(); | ||
| const defaultFetch = useMemo(() => window.fetch.bind(window), []); | ||
| const configuredFetch = p.fetch ?? defaultFetch; | ||
|
|
||
| useEffect(() => { | ||
| const shellNavigationHandler = (event: Event) => { | ||
| const pathname = (event as CustomEvent<string>).detail; | ||
|
|
@@ -115,12 +125,17 @@ function EmbeddedApp({ basePath = '', ...p }: EmbeddedProps) { | |
| () => | ||
| createConnectTransport({ | ||
| baseUrl: getGrpcBasePath(p.urlOverride?.grpc), | ||
| interceptors: [addBearerTokenInterceptor, checkExpiredLicenseInterceptor], | ||
| fetch: configuredFetch, | ||
| interceptors: [ | ||
| addBearerTokenInterceptor, | ||
| ...(tokenRefreshInterceptor ? [tokenRefreshInterceptor] : []), | ||
| checkExpiredLicenseInterceptor, | ||
| ], | ||
| jsonOptions: { | ||
| registry: protobufRegistry, | ||
| }, | ||
| }), | ||
| [p.urlOverride?.grpc] | ||
| [configuredFetch, p.urlOverride?.grpc, tokenRefreshInterceptor] | ||
| ); | ||
|
|
||
| // Create router with dynamic basePath | ||
|
|
@@ -139,20 +154,33 @@ function EmbeddedApp({ basePath = '', ...p }: EmbeddedProps) { | |
| [basePath, dataplaneTransport] | ||
| ); | ||
|
|
||
| useEmbeddedAuthPrewarm({ | ||
| enabled: Boolean(p.isConsoleReadyToMount), | ||
| prewarm: async () => { | ||
| await api.refreshUserData().catch(() => { | ||
| // Best-effort prewarm only; embedded hosts handle any hard auth failures. | ||
| }); | ||
|
|
||
| await Promise.allSettled([queryClient.invalidateQueries(), router.invalidate()]); | ||
| }, | ||
| }); | ||
|
|
||
| if (!p.isConsoleReadyToMount) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <CustomFeatureFlagProvider initialFlags={p.featureFlags}> | ||
| <ChakraProvider resetCSS={false} theme={redpandaTheme} toastOptions={redpandaToastOptions}> | ||
| <TransportProvider transport={dataplaneTransport}> | ||
| <QueryClientProvider client={queryClient}> | ||
| <RouterProvider router={router} /> | ||
| </QueryClientProvider> | ||
| </TransportProvider> | ||
| </ChakraProvider> | ||
| </CustomFeatureFlagProvider> | ||
| <TokenRefreshInterceptorProvider value={tokenRefreshInterceptor}> | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. module federation v1 vs v2. token refresh interceptor provider needs to work in both |
||
| <CustomFeatureFlagProvider initialFlags={p.featureFlags}> | ||
| <ChakraProvider resetCSS={false} theme={redpandaTheme} toastOptions={redpandaToastOptions}> | ||
| <TransportProvider transport={dataplaneTransport}> | ||
| <QueryClientProvider client={queryClient}> | ||
| <RouterProvider router={router} /> | ||
| </QueryClientProvider> | ||
| </TransportProvider> | ||
| </ChakraProvider> | ||
| </CustomFeatureFlagProvider> | ||
| </TokenRefreshInterceptorProvider> | ||
| ); | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we please provide more comments on how these
fetchmechanism works across apps? It was never clear to me and it's a bit confusing.