You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

更新firebase-tools后,本地运行Firebase Functions调用Firestore触发认证错误

Fix: Cloud Firestore Emulator Connection Error After Firebase-Tools Update

Let me break down what's happening here and how to fix it:

When you updated firebase-tools, the emulator setup logic changed compared to the old firebase serve command. Your cloud function is currently trying to connect to production Firestore services instead of the local emulator, which triggers that invalid_grant auth error. Here are the actionable fixes:

1. Configure Emulator Ports via firebase.json

Even though you can't set ports via the command line anymore, you can define them in your project's firebase.json file. This ensures the functions and Firestore emulator use consistent addresses:

{
  "emulators": {
    "firestore": {
      "port": 8080
    },
    "functions": {
      "port": 9000
    },
    "ui": {
      "enabled": true
    }
  }
}

Save this file, then restart the emulator with firebase emulators:start—it’ll pick up these port settings automatically.

2. Explicitly Point Your Cloud Function to the Local Emulator

Add a check in your function code to force it to use the Firestore emulator when running locally. This prevents it from trying to hit production APIs:

const admin = require('firebase-admin');
const functions = require('firebase-functions');

admin.initializeApp();

// Connect to local Firestore emulator during development
if (process.env.FUNCTIONS_EMULATOR === 'true') {
  admin.firestore().settings({
    host: 'localhost:8080', // Match the port in firebase.json
    ssl: false
  });
}

exports.yourUserFetchFunction = functions.https.onRequest(async (req, res) => {
  try {
    const user = req.query.user; // Adjust based on your request structure
    const userSnapshot = await admin.firestore().collection('users').doc(user).get();
    
    if (!userSnapshot.exists) {
      return res.status(404).send('User not found');
    }
    
    res.status(200).json(userSnapshot.data());
  } catch (error) {
    res.status(500).send(`Error: ${error.message}`);
  }
});

The FUNCTIONS_EMULATOR environment variable is automatically set by the Firebase emulator, so you don’t need to manually define it.

3. Clear Stored Authentication Credentials

The invalid_grant error often comes from stale auth tokens. Run these commands to reset your local Firebase auth:

firebase logout
firebase login --reauth

This ensures you’re using fresh, valid credentials for local development.

4. Wait for All Emulators to Fully Start

Sometimes the Functions emulator starts before Firestore is ready. Wait until you see logs like these in your terminal before testing your API:

✔  firestore: Firestore Emulator logging to firestore-debug.log
✔  functions: Functions Emulator logging to functions-debug.log
✔  emulators: All emulators started, it is now safe to connect.

5. Downgrade Firebase-Tools (If All Else Fails)

If the above steps don’t work, you can roll back to the version that worked with firebase serve. Find your old version (check npm logs or package manager history) and install it:

npm install -g firebase-tools@<your-working-version>

内容的提问来源于stack exchange,提问作者Indhu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:28:18