feat(auth): improve API key authentication flow (#11760)

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
This commit is contained in:
Gal Zahavi
2025-10-29 18:58:08 -07:00
committed by GitHub
parent 6c8a48db13
commit 06035d5d43
25 changed files with 1216 additions and 76 deletions
+48 -5
View File
@@ -44,6 +44,7 @@ import {
clearCachedCredentialFile,
recordExitFail,
ShellExecutionService,
saveApiKey,
debugLogger,
coreEvents,
CoreEvent,
@@ -356,10 +357,14 @@ export const AppContainer = (props: AppContainerProps) => {
initializationResult.themeError,
);
const { authState, setAuthState, authError, onAuthError } = useAuthCommand(
settings,
config,
);
const {
authState,
setAuthState,
authError,
onAuthError,
apiKeyDefaultValue,
reloadApiKey,
} = useAuthCommand(settings, config);
const { proQuotaRequest, handleProQuotaChoice } = useQuotaAndFallback({
config,
@@ -408,6 +413,34 @@ Logging in with Google... Please restart Gemini CLI to continue.
[settings, config, setAuthState, onAuthError],
);
const handleApiKeySubmit = useCallback(
async (apiKey: string) => {
try {
if (!apiKey.trim() && apiKey.length > 1) {
onAuthError(
'API key cannot be empty string with length greater than 1.',
);
return;
}
await saveApiKey(apiKey);
await reloadApiKey();
await config.refreshAuth(AuthType.USE_GEMINI);
setAuthState(AuthState.Authenticated);
} catch (e) {
onAuthError(
`Failed to save API key: ${e instanceof Error ? e.message : String(e)}`,
);
}
},
[setAuthState, onAuthError, reloadApiKey, config],
);
const handleApiKeyCancel = useCallback(() => {
// Go back to auth method selection
setAuthState(AuthState.Updating);
}, [setAuthState]);
// Sync user tier from config when authentication changes
useEffect(() => {
// Only sync when not currently authenticating
@@ -1163,7 +1196,9 @@ Logging in with Google... Please restart Gemini CLI to continue.
isEditorDialogOpen ||
showPrivacyNotice ||
showIdeRestartPrompt ||
!!proQuotaRequest;
!!proQuotaRequest ||
isAuthDialogOpen ||
authState === AuthState.AwaitingApiKeyInput;
const pendingHistoryItems = useMemo(
() => [...pendingSlashCommandHistoryItems, ...pendingGeminiHistoryItems],
@@ -1180,6 +1215,8 @@ Logging in with Google... Please restart Gemini CLI to continue.
isConfigInitialized,
authError,
isAuthDialogOpen,
isAwaitingApiKeyInput: authState === AuthState.AwaitingApiKeyInput,
apiKeyDefaultValue,
editorError,
isEditorDialogOpen,
showPrivacyNotice,
@@ -1335,6 +1372,8 @@ Logging in with Google... Please restart Gemini CLI to continue.
historyManager,
embeddedShellFocused,
showDebugProfiler,
apiKeyDefaultValue,
authState,
],
);
@@ -1369,6 +1408,8 @@ Logging in with Google... Please restart Gemini CLI to continue.
handleProQuotaChoice,
setQueueErrorMessage,
popAllMessages,
handleApiKeySubmit,
handleApiKeyCancel,
}),
[
handleThemeSelect,
@@ -1395,6 +1436,8 @@ Logging in with Google... Please restart Gemini CLI to continue.
handleProQuotaChoice,
setQueueErrorMessage,
popAllMessages,
handleApiKeySubmit,
handleApiKeyCancel,
],
);