MCPcopy Create free account
hub / github.com/firebase/firebase-tools / autoAuth

Function autoAuth

src/requireAuth.ts:52–89  ·  view source on GitHub ↗

* 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[])

Source from the content-addressed store, hash-verified

50 * @param authScopes scopes to be obtained.
51 */
52async 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
91export async function refreshAuth(): Promise<Tokens> {
92 if (!lastOptions) {

Callers 1

requireAuthFunction · 0.85

Calls 8

isFirebaseMcpFunction · 0.90
timeoutErrorFunction · 0.90
isFirebaseStudioFunction · 0.90
setActiveAccountFunction · 0.90
setGlobalDefaultAccountFunction · 0.90
getAuthClientFunction · 0.85
getAccessTokenMethod · 0.80
getProjectIdMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…