Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(mobile): preserve browser login across screen remounts

chenhong3a5e4f06564a
diff --git a/apps/mobile/app/(auth)/login.tsx b/apps/mobile/app/(auth)/login.tsx
index 3d518461b2..1a81a4e51c 100644
--- a/apps/mobile/app/(auth)/login.tsx
+++ b/apps/mobile/app/(auth)/login.tsx
@@ -12,6 +12,7 @@ import { useSafeAreaInsets } from 'react-native-safe-area-context';
import type { AccountDeletionStatus, SocialProvider, VerificationKind } from '@cindy/auth-client';
import { useAuth } from '@/auth/AuthContext';
+import { useLoginScreenInitialization } from '@/auth/useLoginScreenInitialization';
import { LoginCaptchaWebView } from '@/auth/LoginCaptchaWebView';
import { useLoginFirstLaunchLight } from '@/auth/loginFirstLaunchGate';
import {
@@ -143,7 +144,6 @@ export function LoginScreen({
handoffDispatch?.({ type: 'panel-mounted' });
}, [handoffDispatch]);
const handoffPhase: LoginHandoffPhase = handoff?.state.phase ?? 'done';
- const initializedLoginRef = useRef(false);
// identifier 形态 = 构建区域确定性推导(用户拍板 2026-07-21:手机/邮箱分区互斥,
// 双 tab 切换移除);providers 仅兜底区域首选方式未下发的场景。
const identifierKind: VerificationKind = useMemo(
@@ -320,16 +320,7 @@ export function LoginScreen({
[],
);
- useEffect(() => {
- if (
- !auth.initialized ||
- (!additionalAccount && auth.isAuthenticated) ||
- initializedLoginRef.current
- )
- return;
- initializedLoginRef.current = true;
- void auth.dispatchLoginAction({ type: 'reset' });
- }, [additionalAccount, auth]);
+ useLoginScreenInitialization(auth, additionalAccount);
useEffect(() => {
if (additionalAccount || !auth.initialized || auth.isAuthenticated) return;
diff --git a/apps/mobile/src/auth/__tests__/browserAuthSession.test.tsx b/apps/mobile/src/auth/__tests__/browserAuthSession.test.tsx
index 2da72a134f..7b2d85e611 100644
--- a/apps/mobile/src/auth/__tests__/browserAuthSession.test.tsx
+++ b/apps/mobile/src/auth/__tests__/browserAuthSession.test.tsx
@@ -93,6 +93,7 @@ vi.mock('@/update/betaChannelStore', () => ({ prepareBetaChannelForDevice: async
vi.mock('@/update/fetchLatestRelease', () => ({}));
import { AuthProvider, useAuth } from '../AuthContext';
+import { useLoginScreenInitialization } from '../useLoginScreenInitialization';
const pendingKey = 'cindy.mobile.auth.pendingOAuth';
const verifiedOutcome = {
@@ -107,6 +108,18 @@ function Probe() {
return null;
}
+function LoginScreenLifecycle() {
+ const value = useAuth();
+ useLoginScreenInitialization(value, false);
+ return null;
+}
+
+async function mountLoginScreen(key: string) {
+ await act(async () => {
+ root.render(<AuthProvider><Probe /><LoginScreenLifecycle key={key} /></AuthProvider>);
+ });
+}
+
function callbackUrl(state = JSON.parse(native.storage.get(pendingKey)!).state as string) {
return `cindycn://auth?code=fixture-code&state=${state}`;
}
@@ -325,6 +338,44 @@ describe('browser auth session lifecycle (real AuthProvider, mocked native/netwo
expect(native.storage.has(pendingKey)).toBe(false);
});
+ it.each(['before-callback', 'during-exchange'] as const)(
+ 'preserves browser login when the login screen remounts %s', async timing => {
+ await mountLoginScreen('initial');
+ await start();
+ const url = callbackUrl();
+ const pending = native.storage.get(pendingKey);
+ let complete!: (value: typeof verifiedOutcome) => void;
+ native.exchange.mockImplementation(() => new Promise(resolve => { complete = resolve; }));
+ if (timing === 'during-exchange') await emitLink(url);
+ await mountLoginScreen('remounted-after-deep-link');
+ expect(auth.loginState?.step).toBe('browser-redirect');
+ expect(native.storage.get(pendingKey)).toBe(pending);
+ if (timing === 'before-callback') await emitLink(url);
+ expect(native.exchange).toHaveBeenCalledExactlyOnceWith('fixture-code', 'fixture-verifier');
+ await act(async () => { complete(verifiedOutcome); });
+ expect(auth.loginState?.step).toBe('sso-verification');
+ expect(auth.authError).toBeNull();
+ expect(native.storage.has(pendingKey)).toBe(false);
+ },
+ );
+
+ it('still cancels and retries explicitly after the login screen has remounted', async () => {
+ await mountLoginScreen('initial');
+ await start();
+ const oldUrl = callbackUrl();
+ await mountLoginScreen('remounted-after-deep-link');
+ await act(async () => { await auth.dispatchLoginAction({ type: 'reset' }); });
+ expect(auth.loginState?.step).toBe('identifier');
+ expect(native.storage.has(pendingKey)).toBe(false);
+ await start();
+ await emitLink(oldUrl);
+ expect(native.exchange).not.toHaveBeenCalled();
+ await emitLink(callbackUrl());
+ expect(native.exchange).toHaveBeenCalledTimes(1);
+ expect(auth.loginState?.step).toBe('sso-verification');
+ expect(auth.authError).toBeNull();
+ });
+
it('still exchanges only once when Linking and the browser both report success', async () => {
native.open.mockImplementation(async () => {
const url = callbackUrl();
diff --git a/apps/mobile/src/auth/useLoginScreenInitialization.ts b/apps/mobile/src/auth/useLoginScreenInitialization.ts
new file mode 100644
index 0000000000..728d66780e
--- /dev/null
+++ b/apps/mobile/src/auth/useLoginScreenInitialization.ts
@@ -0,0 +1,19 @@
+import { useEffect, useRef } from 'react';
+import type { AuthContextValue } from './AuthContext';
+
+export function useLoginScreenInitialization(auth: AuthContextValue, additionalAccount: boolean) {
+ const initializedLoginRef = useRef(false);
+ useEffect(() => {
+ if (
+ !auth.initialized ||
+ (!additionalAccount && auth.isAuthenticated) ||
+ initializedLoginRef.current
+ )
+ return;
+ initializedLoginRef.current = true;
+ // Deep-link navigation can remount this screen while AuthProvider survives.
+ // Mounting is not a user cancellation of the pending browser authorization.
+ if (auth.loginState?.step === 'browser-redirect') return;
+ void auth.dispatchLoginAction({ type: 'reset' });
+ }, [additionalAccount, auth]);
+}