diff options
| author | Tommaso Sciortino <[email protected]> | 2025-06-11 13:26:41 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-06-11 13:26:41 -0700 |
| commit | 24c61147b839b3173fa1ad79781f3c4c0f4144fa (patch) | |
| tree | a6b58447ddc7bccc424faf9d4658b30d4ec8a5be /packages/core/src/code_assist/setup.ts | |
| parent | c0580eaf4b8b7f02a048ade43a0f0b652fa01129 (diff) | |
Cache oauth credentials (#927)
Diffstat (limited to 'packages/core/src/code_assist/setup.ts')
| -rw-r--r-- | packages/core/src/code_assist/setup.ts | 30 |
1 files changed, 23 insertions, 7 deletions
diff --git a/packages/core/src/code_assist/setup.ts b/packages/core/src/code_assist/setup.ts index a3162c81..0efe5f0c 100644 --- a/packages/core/src/code_assist/setup.ts +++ b/packages/core/src/code_assist/setup.ts @@ -7,6 +7,8 @@ import { ClientMetadata, OnboardUserRequest } from './types.js'; import { CcpaServer } from './ccpaServer.js'; import { OAuth2Client } from 'google-auth-library'; +import { GaxiosError } from 'gaxios'; +import { clearCachedCredentials } from './oauth2.js'; /** * @@ -38,13 +40,27 @@ export async function setupUser( cloudaicompanionProject: loadRes.cloudaicompanionProject || '', metadata: clientMetadata, }; + try { + // Poll onboardUser until long running operation is complete. + let lroRes = await ccpaServer.onboardUser(onboardReq); + while (!lroRes.done) { + await new Promise((f) => setTimeout(f, 5000)); + lroRes = await ccpaServer.onboardUser(onboardReq); + } - // Poll onboardUser until long running operation is complete. - let lroRes = await ccpaServer.onboardUser(onboardReq); - while (!lroRes.done) { - await new Promise((f) => setTimeout(f, 5000)); - lroRes = await ccpaServer.onboardUser(onboardReq); + return lroRes.response?.cloudaicompanionProject?.id || ''; + } catch (e) { + if (e instanceof GaxiosError) { + const detail = e.response?.data?.error?.details[0].detail; + if (detail && detail.includes('projectID is empty')) { + await clearCachedCredentials(); + console.log( + '\n\nEnterprise users must specify GOOGLE_CLOUD_PROJECT ' + + 'in your environment variables or .env file.\n\n', + ); + process.exit(1); + } + } + throw e; } - - return lroRes.response?.cloudaicompanionProject?.id || ''; } |
