Skip to content

fix: initialize auth in requirePermissions before projectId check - #10880

Open
joehan wants to merge 2 commits into
mainfrom
fix-adc-auth-error
Open

fix: initialize auth in requirePermissions before projectId check#10880
joehan wants to merge 2 commits into
mainfrom
fix-adc-auth-error

Conversation

@joehan

@joehan joehan commented Jul 31, 2026

Copy link
Copy Markdown
Member

Description

Fixes an issue where running commands with Application Default Credentials (ADC) without a selected/configured project causes a confusing Unable to refresh auth: not yet authenticated error.

When a command that performs client-side IAM checks via requirePermissions is run without an active project, requirePermissions returned early before calling requireAuth. This left the local auth state (e.g. lastOptions and accessToken) uninitialized. When the command's action then proceeded to make API calls (which fallback to the active auth token), the auth client encountered an uninitialized auth state, resulting in a rejected promise with the Unable to refresh auth message.

This PR fixes it by calling requireAuth(options) at the beginning of requirePermissions, ensuring that the auth client and local tokens are always initialized before any early return.

Fixes #10879

Scenarios Tested

  1. Tested running firestore:locations under ADC without an active project (expected behavior: proper GCP API/project error instead of auth failure).
  2. Tested running firestore:locations under ADC with a valid project (expected behavior: lists locations successfully).
  3. Ran full Mocha test suite (npm run mocha:fast) to ensure no regressions in auth hooks.

Sample Commands

  • Running without a project (before fix vs after fix):
    firebase firestore:locations --debug

    • Before Fix:
      Error: Unable to refresh auth: not yet authenticated.
      
    • After Fix:
      Error: Request to https://firestore.googleapis.com/v1/projects/null/locations had HTTP Error: 403, Permission denied on resource project null.
      
  • Running with a project under ADC:
    firebase firestore:locations --project adam-test-c4a4d --debug

    • Before / After Fix:
      Lists locations successfully.

### Description
Fixes an issue where running commands with Application Default Credentials (ADC) without a selected/configured project causes a confusing `Unable to refresh auth: not yet authenticated` error.

When a command that performs client-side IAM checks via `requirePermissions` is run without an active project, `requirePermissions` returned early before calling `requireAuth`. This left the local auth state (e.g. `lastOptions` and `accessToken`) uninitialized. When the command's action then proceeded to make API calls (which fallback to the active auth token), the auth client encountered an uninitialized auth state, resulting in a rejected promise with the `Unable to refresh auth` message.

This PR fixes it by calling `requireAuth(options)` at the beginning of `requirePermissions`, ensuring that the auth client and local tokens are always initialized before any early return.

Fixes #10879

### Scenarios Tested
1. Tested running `firestore:locations` under ADC without an active project (expected behavior: proper GCP API/project error instead of auth failure).
2. Tested running `firestore:locations` under ADC with a valid project (expected behavior: lists locations successfully).
3. Ran full Mocha test suite (`npm run mocha:fast`) to ensure no regressions in auth hooks.

### Sample Commands
- Running without a project (before fix vs after fix):
  `firebase firestore:locations --debug`
  - **Before Fix:**
    ```
    Error: Unable to refresh auth: not yet authenticated.
    ```
  - **After Fix:**
    ```
    Error: Request to https://firestore.googleapis.com/v1/projects/null/locations had HTTP Error: 403, Permission denied on resource project null.
    ```

- Running with a project under ADC:
  `firebase firestore:locations --project adam-test-c4a4d --debug`
  - **Before / After Fix:**
    Lists locations successfully.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request modifies src/requirePermissions.ts to call requireAuth(options) at the very beginning of the requirePermissions function, before retrieving and checking the project ID. There are no review comments, and I have no feedback to provide.

@joehan
joehan requested a review from richieforeman July 31, 2026 22:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Firebase CLI does not use Google application default credentials consistently

2 participants