Skip to content

Commit ed94d9e

Browse files
skeshivesidwan02
authored andcommitted
fix(auth): don't crash when initial auth fails (google-gemini#17308)
1 parent f3b2352 commit ed94d9e

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

packages/cli/src/gemini.test.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,8 @@ describe('gemini.tsx main function exit codes', () => {
11351135
vi.mocked(loadSandboxConfig).mockResolvedValue({} as any);
11361136
vi.mocked(loadCliConfig).mockResolvedValue({
11371137
refreshAuth: vi.fn().mockRejectedValue(new Error('Auth failed')),
1138+
getRemoteAdminSettings: vi.fn().mockReturnValue(undefined),
1139+
isInteractive: vi.fn().mockReturnValue(true),
11381140
} as unknown as Config);
11391141
vi.mocked(loadSettings).mockReturnValue(
11401142
createMockSettings({

packages/cli/src/gemini.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ export async function main() {
373373
// Refresh auth to fetch remote admin settings from CCPA and before entering
374374
// the sandbox because the sandbox will interfere with the Oauth2 web
375375
// redirect.
376+
let initialAuthFailed = false;
376377
if (!settings.merged.security.auth.useExternal) {
377378
try {
378379
if (
@@ -400,8 +401,7 @@ export async function main() {
400401
}
401402
} catch (err) {
402403
debugLogger.error('Error authenticating:', err);
403-
await runExitCleanup();
404-
process.exit(ExitCodes.FATAL_AUTHENTICATION_ERROR);
404+
initialAuthFailed = true;
405405
}
406406
}
407407

@@ -427,6 +427,10 @@ export async function main() {
427427
// another way to decouple refreshAuth from requiring a config.
428428

429429
if (sandboxConfig) {
430+
if (initialAuthFailed) {
431+
await runExitCleanup();
432+
process.exit(ExitCodes.FATAL_AUTHENTICATION_ERROR);
433+
}
430434
let stdinData = '';
431435
if (!process.stdin.isTTY) {
432436
stdinData = await readStdin();

0 commit comments

Comments
 (0)