* Retrieves and sets the access token for the current user. * Returns account email if found. * @param options CLI options. * @param authScopes scopes to be obtained.
(options: Options, authScopes: string[])
| 50 | * @param authScopes scopes to be obtained. |
| 51 | */ |
| 52 | async function autoAuth(options: Options, authScopes: string[]): Promise<null | string> { |
| 53 | const client = getAuthClient({ scopes: authScopes, projectId: options.project }); |
| 54 | const token = await client.getAccessToken(); |
| 55 | token !== null ? apiv2.setAccessToken(token) : false; |
| 56 | logger.debug(`Running auto auth`); |
| 57 | |
| 58 | let clientEmail; |
| 59 | try { |
| 60 | const timeoutMillis = isFirebaseMcp() ? 5000 : 15000; |
| 61 | const credentials = await timeoutError( |
| 62 | client.getCredentials(), |
| 63 | new FirebaseError( |
| 64 | `Authenticating with default credentials timed out after ${timeoutMillis / 1000} seconds. Please try running \`firebase login\` instead.`, |
| 65 | ), |
| 66 | timeoutMillis, |
| 67 | ); |
| 68 | clientEmail = credentials.client_email; |
| 69 | } catch (e) { |
| 70 | // Make sure any error here doesn't block the CLI, but log it. |
| 71 | logger.debug(`Error getting account credentials.`); |
| 72 | } |
| 73 | if (isFirebaseStudio() && token && clientEmail) { |
| 74 | // Within monospace, this a OAuth token for the user, so we make it the active user. |
| 75 | const activeAccount = { |
| 76 | user: { email: clientEmail }, |
| 77 | tokens: { |
| 78 | access_token: token, |
| 79 | expires_at: client.cachedCredential?.credentials.expiry_date, |
| 80 | } as TokensWithExpiration, |
| 81 | }; |
| 82 | setActiveAccount(options, activeAccount); |
| 83 | setGlobalDefaultAccount(activeAccount); |
| 84 | |
| 85 | // project is also selected in monospace auth flow |
| 86 | options.projectId = await client.getProjectId(); |
| 87 | } |
| 88 | return clientEmail || null; |
| 89 | } |
| 90 | |
| 91 | export async function refreshAuth(): Promise<Tokens> { |
| 92 | if (!lastOptions) { |
no test coverage detected
searching dependent graphs…