( showError: boolean = true, )
| 30 | * |
| 31 | */ |
| 32 | export async function requireAuthWrapper( |
| 33 | showError: boolean = true, |
| 34 | ): Promise<User | null> { |
| 35 | // Try to get global default from configstore |
| 36 | pluginLogger.debug("requireAuthWrapper"); |
| 37 | let account = getGlobalDefaultAccount(); |
| 38 | // often overwritten when restarting the extension. |
| 39 | const accounts = getAllAccounts(); |
| 40 | |
| 41 | // helper to determine if VSCode options has the same account as global default |
| 42 | function isUserMatching(account: Account, options: Options) { |
| 43 | if (!options.user || !options.tokens) { |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | const optionsUser = options.user as User; |
| 48 | const optionsTokens = options.tokens as Tokens; |
| 49 | return ( |
| 50 | account && |
| 51 | account.user?.email === optionsUser.email && |
| 52 | account.tokens.refresh_token === optionsTokens.refresh_token // Should check refresh token which is consistent, not access_token which is short lived. |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | // only add account options when vscode is missing account information |
| 57 | if (!isUserMatching(account!, currentOptions.value)) { |
| 58 | currentOptions.value = { ...currentOptions.value, ...account }; |
| 59 | } |
| 60 | |
| 61 | if (!account) { |
| 62 | // If nothing in configstore top level, grab the first "additionalAccount" |
| 63 | for (const additionalAccount of accounts) { |
| 64 | if (additionalAccount.user.email === currentUser.value!.email) { |
| 65 | account = additionalAccount; |
| 66 | setGlobalDefaultAccount(account); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | // `requireAuth()` is not just a check, but will also register SERVICE |
| 71 | // ACCOUNT tokens in memory as a variable in apiv2.ts, which is needed |
| 72 | // for subsequent API calls. Warning: this variable takes precedence |
| 73 | // over Google login tokens and must be removed if a Google |
| 74 | // account is the current user. |
| 75 | try { |
| 76 | const optsCopy = currentOptions.value; |
| 77 | const userEmail = await requireAuth(optsCopy); // client email |
| 78 | // SetAccessToken is necessary here to ensure that access_tokens are available when: |
| 79 | // - we are using tokens from configstore (aka those set by firebase login), AND |
| 80 | // - we are calling CLI code that skips Command (where we normally call this) |
| 81 | currentOptions.value = optsCopy; |
| 82 | if (optsCopy.projectId) { |
| 83 | updateFirebaseRCProject({ |
| 84 | projectAlias: { alias: "default", projectId: optsCopy.projectId }, |
| 85 | }); |
| 86 | } |
| 87 | setAccessToken(await getAccessToken()); |
| 88 | if (userEmail) { |
| 89 | pluginLogger.debug("User found: ", userEmail); |
no test coverage detected
searching dependent graphs…