简体   繁体   English

如何使用 firebase admin SDK 访问 admin firestore?

[英]How to access admin firestore using firebase admin SDK?

I am working on an application, where it uses Next.js and Firebase.我正在开发一个应用程序,它使用 Next.js 和 Firebase。

I need to implement server-side authentication.我需要实施服务器端身份验证。 Firebase allows connecting the server using Firebase Admin SDK. Firebase 允许使用 Firebase Admin SDK 连接服务器。

const admin = require("firebase-admin");
const serviceAccount = require("../../../../3-firebase/service-account-key/service-account-file.json");

try {
  admin.initializeApp({
    credential: admin.credential.cert({
      client_email: process.env.FIREBASE_CLIENT_EMAIL,
      private_key: process.env.FIREBASE_PRIVATE_KEY,
      project_id: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
    }),
  });
  console.log("Initialized.");
} catch (error) {
  if (!/already exists/u.test(error.message)) {
    console.error("Firebase admin initialization error", error.stack);
  }
}

const db = admin.firestore();
export { db };

I installed the firebase-admin package using NPM and I setup firebase admin SDK using the above code by creating a separate file called "firebase-admin.js"我使用 NPM 安装了 firebase-admin package 并通过创建一个名为“firebase-admin.js”的单独文件使用上述代码设置了 firebase admin SDK

import { initializeApp } from "firebase/app";

const firebaseConfig = {
  apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
  authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
  projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
};

const defaultApp = initializeApp(firebaseConfig);
export default defaultApp;

The above code is the default firebase application setup in a separate file called "firebase.js"上面的代码是一个名为“firebase.js”的单独文件中的默认 firebase 应用程序设置

The problem I encountered is I am not able to access the admin firestore.我遇到的问题是我无法访问管理员 firestore。 However, I can able to access the default firestore.但是,我可以访问默认的 firestore。

What I observed is admin SDK is able to initialize using the credentials (private key).我观察到的是管理员 SDK 能够使用凭据(私钥)进行初始化。 But I don't know why I can't access admin firestore.但是我不知道为什么我无法访问admin firestore。 I mean when i use admin firestore the console gives error of 500 (internal server error)我的意思是当我使用 admin firestore 时,控制台给出错误 500(内部服务器错误)

Here is the error message, when I try to use admin.firestore()这是错误消息,当我尝试使用 admin.firestore()

{"error":{"code":"invalid-argument","name":"FirebaseError"}} {“错误”:{“代码”:“无效参数”,“名称”:“FirebaseError”}}

versions I am using "firebase": "^9.6.6", "firebase-admin": "^10.0.2",我使用的版本 "firebase": "^9.6.6", "firebase-admin": "^10.0.2",

Timely help is much needed非常需要及时的帮助

You're importing the initializeApp method from the Firebase Javascript SDK. You should be importing from the Firebase Admin SDK.您正在从 Firebase Javascript SDK 导入 initializeApp 方法。您应该从 Firebase Admin SDK 导入。

import { initializeApp } from "firebase/app";

vs.对比

import { initializeApp } from "firebase-admin/app";

Using firebase-admin v10.0.2 I was able to successfully access the db adding this in my config.js file, similar to your firebase.js file.使用firebase-admin v10.0.2我能够成功访问数据库,将其添加到我的config.js文件中,类似于您的firebase.js文件。

const ServiceAccount = require('../superSecretServiceKeyFile.json');
const app = initializeApp(ServiceAccount);

const { getFirestore } = require('firebase-admin/firestore');
const db = getFirestore(app);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firestore 使用 Firebase Client SDK V9 Modular with Admin 权限 - Firestore using Firebase Client SDK V9 Modular with Admin Privileges Firebase Function 使用管理员连接到 Firestore 失败 SDK - Firebase Function fails connecting to Firestore using Admin SDK 您如何使用 admin SDK 计算 firestore 集合中的文档? - How do you count documents in a firestore collection using the admin SDK? Node JS / Firebase Admin SDK / Firestore - 使用快照接收 firebase 文档后获取空数组 - Node JS / Firebase Admin SDK / Firestore - Getting empty array after receiving firebase documents using snapshots 使用 Firebase 函数从没有管理员 SDK 的 firestore collections 获取数据 - Get data from firestore collections without admin SDK using Firebase Functions 如何使用 firebase admin sdk 列出所有用户 - How can I list all users using firebase admin sdk 使用 admin sdk 和 Firebase 云函数从 Firestore 检索对象数组 - Retrieving an array of objects from Firestore with the admin sdk and Firebase cloud functions 如何使用 Admin SDK Java 将 PDF 文件上传到 Firebase 存储? - How to upload PDF file to Firebase Storage using Admin SDK Java? 使用模拟器从 firebase 管理员 SDK 创建 Firestore 索引 - Create firestore index from firebase admin SDK with emulator 通过 Firebase 管理员访问 Firebase 访问令牌 PHP SDK - Access Firebase access token through Firebase Admin PHP SDK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM