From 26a79fec2509e40a9fa3e82428a3c7ed30dce4f5 Mon Sep 17 00:00:00 2001
From: "N. Taylor Mullen" <ntaylormullen@google.com>
Date: Sat, 12 Jul 2025 20:43:35 -0700
Subject: feat: Add GEMINI_DEFAULT_AUTH_TYPE support (#4002)

---
 packages/cli/src/ui/components/AuthDialog.tsx | 50 +++++++++++++++++++++++----
 1 file changed, 43 insertions(+), 7 deletions(-)

(limited to 'packages/cli/src/ui/components/AuthDialog.tsx')

diff --git a/packages/cli/src/ui/components/AuthDialog.tsx b/packages/cli/src/ui/components/AuthDialog.tsx
index d46feb0b..ae076ee7 100644
--- a/packages/cli/src/ui/components/AuthDialog.tsx
+++ b/packages/cli/src/ui/components/AuthDialog.tsx
@@ -18,18 +18,47 @@ interface AuthDialogProps {
   initialErrorMessage?: string | null;
 }
 
+function parseDefaultAuthType(
+  defaultAuthType: string | undefined,
+): AuthType | null {
+  if (
+    defaultAuthType &&
+    Object.values(AuthType).includes(defaultAuthType as AuthType)
+  ) {
+    return defaultAuthType as AuthType;
+  }
+  return null;
+}
+
 export function AuthDialog({
   onSelect,
   settings,
   initialErrorMessage,
 }: AuthDialogProps): React.JSX.Element {
-  const [errorMessage, setErrorMessage] = useState<string | null>(
-    initialErrorMessage
-      ? initialErrorMessage
-      : process.env.GEMINI_API_KEY
-        ? 'Existing API key detected (GEMINI_API_KEY). Select "Gemini API Key" option to use it.'
-        : null,
-  );
+  const [errorMessage, setErrorMessage] = useState<string | null>(() => {
+    if (initialErrorMessage) {
+      return initialErrorMessage;
+    }
+
+    const defaultAuthType = parseDefaultAuthType(
+      process.env.GEMINI_DEFAULT_AUTH_TYPE,
+    );
+
+    if (process.env.GEMINI_DEFAULT_AUTH_TYPE && defaultAuthType === null) {
+      return (
+        `Invalid value for GEMINI_DEFAULT_AUTH_TYPE: "${process.env.GEMINI_DEFAULT_AUTH_TYPE}". ` +
+        `Valid values are: ${Object.values(AuthType).join(', ')}.`
+      );
+    }
+
+    if (
+      process.env.GEMINI_API_KEY &&
+      (!defaultAuthType || defaultAuthType === AuthType.USE_GEMINI)
+    ) {
+      return 'Existing API key detected (GEMINI_API_KEY). Select "Gemini API Key" option to use it.';
+    }
+    return null;
+  });
   const items = [
     {
       label: 'Login with Google',
@@ -55,6 +84,13 @@ export function AuthDialog({
       return item.value === settings.merged.selectedAuthType;
     }
 
+    const defaultAuthType = parseDefaultAuthType(
+      process.env.GEMINI_DEFAULT_AUTH_TYPE,
+    );
+    if (defaultAuthType) {
+      return item.value === defaultAuthType;
+    }
+
     if (process.env.GEMINI_API_KEY) {
       return item.value === AuthType.USE_GEMINI;
     }
-- 
cgit v1.2.3

