2025-09-06 01:39:02 -04:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright 2025 Google LLC
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
type AuthType,
|
|
|
|
|
type Config,
|
|
|
|
|
getErrorMessage,
|
2026-01-26 06:31:19 -08:00
|
|
|
ValidationRequiredError,
|
2026-02-27 10:18:16 -08:00
|
|
|
isAccountSuspendedError,
|
2026-03-05 00:58:34 +05:30
|
|
|
ProjectIdRequiredError,
|
2025-09-06 01:39:02 -04:00
|
|
|
} from '@google/gemini-cli-core';
|
|
|
|
|
|
2026-02-27 10:18:16 -08:00
|
|
|
import type { AccountSuspensionInfo } from '../ui/contexts/UIStateContext.js';
|
|
|
|
|
|
|
|
|
|
export interface InitialAuthResult {
|
|
|
|
|
authError: string | null;
|
|
|
|
|
accountSuspensionInfo: AccountSuspensionInfo | null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-06 01:39:02 -04:00
|
|
|
/**
|
|
|
|
|
* Handles the initial authentication flow.
|
|
|
|
|
* @param config The application config.
|
|
|
|
|
* @param authType The selected auth type.
|
2026-02-27 10:18:16 -08:00
|
|
|
* @returns The auth result with error message and account suspension status.
|
2025-09-06 01:39:02 -04:00
|
|
|
*/
|
|
|
|
|
export async function performInitialAuth(
|
|
|
|
|
config: Config,
|
|
|
|
|
authType: AuthType | undefined,
|
2026-02-27 10:18:16 -08:00
|
|
|
): Promise<InitialAuthResult> {
|
2025-09-06 01:39:02 -04:00
|
|
|
if (!authType) {
|
2026-02-27 10:18:16 -08:00
|
|
|
return { authError: null, accountSuspensionInfo: null };
|
2025-09-06 01:39:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await config.refreshAuth(authType);
|
|
|
|
|
// The console.log is intentionally left out here.
|
|
|
|
|
// We can add a dedicated startup message later if needed.
|
|
|
|
|
} catch (e) {
|
2026-01-26 06:31:19 -08:00
|
|
|
if (e instanceof ValidationRequiredError) {
|
|
|
|
|
// Don't treat validation required as a fatal auth error during startup.
|
|
|
|
|
// This allows the React UI to load and show the ValidationDialog.
|
2026-02-27 10:18:16 -08:00
|
|
|
return { authError: null, accountSuspensionInfo: null };
|
|
|
|
|
}
|
|
|
|
|
const suspendedError = isAccountSuspendedError(e);
|
|
|
|
|
if (suspendedError) {
|
|
|
|
|
return {
|
|
|
|
|
authError: null,
|
|
|
|
|
accountSuspensionInfo: {
|
|
|
|
|
message: suspendedError.message,
|
|
|
|
|
appealUrl: suspendedError.appealUrl,
|
|
|
|
|
appealLinkText: suspendedError.appealLinkText,
|
|
|
|
|
},
|
|
|
|
|
};
|
2026-01-26 06:31:19 -08:00
|
|
|
}
|
2026-03-05 00:58:34 +05:30
|
|
|
if (e instanceof ProjectIdRequiredError) {
|
|
|
|
|
// OAuth succeeded but account setup requires project ID
|
|
|
|
|
// Show the error message directly without "Failed to login" prefix
|
|
|
|
|
return {
|
|
|
|
|
authError: getErrorMessage(e),
|
|
|
|
|
accountSuspensionInfo: null,
|
|
|
|
|
};
|
|
|
|
|
}
|
2026-02-27 10:18:16 -08:00
|
|
|
return {
|
2026-03-10 12:10:26 -07:00
|
|
|
authError: `Failed to sign in. Message: ${getErrorMessage(e)}`,
|
2026-02-27 10:18:16 -08:00
|
|
|
accountSuspensionInfo: null,
|
|
|
|
|
};
|
2025-09-06 01:39:02 -04:00
|
|
|
}
|
|
|
|
|
|
2026-02-27 10:18:16 -08:00
|
|
|
return { authError: null, accountSuspensionInfo: null };
|
2025-09-06 01:39:02 -04:00
|
|
|
}
|