Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix(cli): resolve infinite loader on device code screen (#46137)

Danny Whitedf77fc901187
diff --git a/apps/studio/pages/cli/login.tsx b/apps/studio/pages/cli/login.tsx
index 5c9e88f0bc7..e87e1885478 100644
--- a/apps/studio/pages/cli/login.tsx
+++ b/apps/studio/pages/cli/login.tsx
@@ -18,6 +18,7 @@ import CopyButton from '@/components/ui/CopyButton'
import { InlineLink } from '@/components/ui/InlineLink'
import { createCliLoginSession } from '@/data/cli/login'
import { withAuth } from '@/hooks/misc/withAuth'
+import { useStaticEffectEvent } from '@/hooks/useStaticEffectEvent'
import { buildStudioPageTitle } from '@/lib/page-title'
import { useProfile } from '@/lib/profile'
import type { NextPageWithLayout } from '@/types'
@@ -99,7 +100,7 @@ export const CliLoginScreen = ({
publicKey,
tokenName,
deviceCode,
- navigate,
+ navigate: navigateProp,
}: {
isLoggedIn: boolean
routerReady: boolean
@@ -112,6 +113,9 @@ export const CliLoginScreen = ({
const { profile } = useProfile()
const [status, setStatus] = useState<CliLoginStatus>({ _tag: 'loading' })
const startedForSessionIdRef = useRef<string | undefined>(undefined)
+ // Keep navigate in a ref so changing the prop never re-triggers the effect
+ // or cancels an in-flight POST via the isActive cleanup.
+ const navigate = useStaticEffectEvent(navigateProp)
const displayName = profile?.primary_email ?? profile?.username
useEffect(() => {
@@ -163,7 +167,7 @@ export const CliLoginScreen = ({
return () => {
isActive = false
}
- }, [deviceCode, isLoggedIn, navigate, publicKey, routerReady, sessionId, tokenName])
+ }, [deviceCode, isLoggedIn, publicKey, routerReady, sessionId, tokenName, navigate])
if (status._tag === 'loading') {
return (
@@ -174,7 +178,7 @@ export const CliLoginScreen = ({
<div className="flex flex-col gap-5">
<Card className="shadow-none">
<CardContent className="flex items-center gap-3 border-none px-4 py-3">
- <ShimmeringLoader className="size-8 flex-shrink-0 rounded-full py-0" />
+ <ShimmeringLoader className="size-8 shrink-0 rounded-full py-0" />
<div className="min-w-0 flex-1 space-y-2">
<ShimmeringLoader className="h-3 w-20 py-0" />
<ShimmeringLoader className="h-4 w-40 max-w-full py-0" />
diff --git a/apps/studio/tests/pages/cli/login.test.tsx b/apps/studio/tests/pages/cli/login.test.tsx
new file mode 100644
index 00000000000..0f931d3bb18
--- /dev/null
+++ b/apps/studio/tests/pages/cli/login.test.tsx
@@ -0,0 +1,78 @@
+import { waitFor } from '@testing-library/dom'
+import { expect, test, vi } from 'vitest'
+
+import * as cliLogin from '@/data/cli/login'
+import { ProfileContextType } from '@/lib/profile'
+import { CliLoginScreen } from '@/pages/cli/login'
+import { customRender } from '@/tests/lib/custom-render'
+
+const DEFAULT_PROFILE_CONTEXT: ProfileContextType = {
+ profile: {
+ id: 1,
+ auth0_id: 'auth0|test',
+ gotrue_id: 'gotrue-test',
+ username: 'testuser',
+ primary_email: '[email protected]',
+ first_name: null,
+ last_name: null,
+ mobile: null,
+ is_alpha_user: false,
+ is_sso_user: false,
+ disabled_features: [],
+ free_project_limit: null,
+ },
+ error: null,
+ isLoading: false,
+ isError: false,
+ isSuccess: true,
+}
+
+test('still navigates after parent re-renders during an in-flight POST', async () => {
+ // Resolve manually so we can inject a re-render while the POST is in flight.
+ let resolveSession: (value: { nonce: string }) => void = () => {}
+ const createCliLoginSessionMock = vi.spyOn(cliLogin, 'createCliLoginSession').mockImplementation(
+ () =>
+ new Promise<{ nonce: string }>((resolve) => {
+ resolveSession = resolve
+ })
+ )
+ const initialNavigate = vi.fn()
+ const { rerender } = customRender(
+ <CliLoginScreen
+ isLoggedIn
+ routerReady
+ sessionId="session-test"
+ publicKey="public-key-test"
+ tokenName="local-dev"
+ navigate={initialNavigate}
+ />,
+ { profileContext: DEFAULT_PROFILE_CONTEXT }
+ )
+
+ await waitFor(() => {
+ expect(createCliLoginSessionMock).toHaveBeenCalledTimes(1)
+ })
+
+ // Parent re-renders mid-POST with a brand-new navigate ref. This was the
+ // production hang: the cleanup would invalidate the success handler and
+ // the ref guard would skip the retry, leaving the screen on the loader.
+ const laterNavigate = vi.fn()
+ rerender(
+ <CliLoginScreen
+ isLoggedIn
+ routerReady
+ sessionId="session-test"
+ publicKey="public-key-test"
+ tokenName="local-dev"
+ navigate={laterNavigate}
+ />
+ )
+
+ resolveSession({ nonce: 'ABCDEFGH12345678' })
+
+ await waitFor(() => {
+ expect(laterNavigate).toHaveBeenCalledWith('/cli/login?device_code=ABCDEFGH')
+ })
+ expect(initialNavigate).not.toHaveBeenCalled()
+ expect(createCliLoginSessionMock).toHaveBeenCalledTimes(1)
+})