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

Firebase Cloud Functions报错:admin.auth is not a function 技术求助

Fixing "admin.auth is not a function" in Firebase Cloud Functions

Hey there! That error usually pops up due to a couple of common issues with how you're setting up the Firebase Admin SDK. Let's walk through the fixes step by step:

1. Update your Firebase Admin SDK to the latest version

Older versions of the SDK might have different exports or missing core methods. Run this command in your functions directory to pull the latest stable version:

npm install firebase-admin@latest

2. Simplify your Admin SDK initialization

When deploying to Firebase Cloud Functions, you don't need to manually load the service-account.json or specify the databaseURL—Firebase automatically injects the necessary credentials and environment config for you. Your initialization code can be stripped down to:

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

// This is all you need for Cloud Functions environments
admin.initializeApp();

const cors = require('cors')({origin: true});

exports.login = functions.https.onRequest((req, res) => {
  cors(req, res, () => {
    // Replace this with your actual user validation logic first
    const userId = "unique-user-identifier"; 

    admin.auth().createCustomToken(userId)
      .then(customToken => {
        res.status(200).json({token: customToken});
      })
      .catch(error => {
        res.status(500).json({error: error.message});
      });
  });
});

3. Check for variable conflicts

Double-check your full code to make sure you haven't accidentally redefined the admin variable somewhere else (like var admin = {} later in the file)—that would overwrite the SDK import and break access to admin.auth().

If you still hit issues, verify your package.json in the functions folder lists firebase-admin as a dependency with a recent version (e.g., "firebase-admin": "^12.0.0" or higher).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 07:12:45