从服务器端调用Firebase https函数?

问题描述 投票:0回答:4

Firebase https 函数可以从客户端调用,因为函数调用是由客户端 SDK 提供的。

但是可以从服务器端调用它们吗?我找不到任何相关文档或问题/答案。

具体来说,我使用的是 Next.js,我想在服务器端渲染期间调用 https 函数,并通过 props 将结果传递到客户端。

firebase next.js google-cloud-functions server-side-rendering
4个回答
1
投票
我认为这个例子应该涵盖大多数情况:

const token = await admin.credential.applicationDefault().getAccessToken(); const appCheckToken = await admin.appCheck().createToken(admin.app()?.options?.projectId as string); const selfPolizeEmissionResponse = await axios.post( 'https://us-central1-{yourProjectId}.cloudfunctions.net/{yourCallableName}', { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}`, 'X-Firebase-AppCheck': appCheckToken, }, body: JSON.stringify({ data: { // Your Data to be use by callable! }, }), } );
    

0
投票
通用方法描述于

here

对于 Next.js 用例中的 SSR,您需要一些 js http 客户端,例如

axios



0
投票
要从 Next.js 访问 Firebase,我们需要创建 Firebase 服务的服务器实例。

serverApp.ts


// enforces that this code can only be called on the server // https://nextjs.org/docs/app/building-your-application/rendering/composition-patterns#keeping-server-only-code-out-of-the-client-environment import "server-only"; import { headers } from "next/headers"; import { FirebaseServerApp, initializeServerApp } from "firebase/app"; import { firebaseConfig } from "./config"; import { connectAuthEmulator, getAuth as getAuthFirebase} from "firebase/auth"; import { connectFirestoreEmulator, getFirestore as getFirestoreFirebase} from "firebase/firestore"; import { connectFunctionsEmulator, getFunctions as getFunctionsFirebase } from "firebase/functions"; let firestoreEmulatorConnected = false; let authEmulatorConnected = false; let functionsEmulatorConnected = false; export function getServerApp() { const idToken = headers().get("Authorization")?.split("Bearer ")[1]; const firebaseServerApp = initializeServerApp( firebaseConfig, idToken ? { authIdToken: idToken, } : {} ); return firebaseServerApp; } export function getAuth(firebaseServerApp?: FirebaseServerApp) { if (!firebaseServerApp) { firebaseServerApp = getServerApp(); } const auth = getAuthFirebase(firebaseServerApp); if (process.env.NODE_ENV === 'development' && !authEmulatorConnected) { console.log('Connecting to Auth emulator!'); connectAuthEmulator(auth, "http://localhost:9099"); authEmulatorConnected = true; } return auth; } export async function getCurrentUser(firebaseServerApp?: FirebaseServerApp) { const auth = getAuth(firebaseServerApp); await auth.authStateReady(); return auth.currentUser; } export function getFirestore(firebaseServerApp?: FirebaseServerApp) { if (!firebaseServerApp) { firebaseServerApp = getServerApp(); } const db = getFirestoreFirebase(firebaseServerApp); if (process.env.NODE_ENV === 'development' && !firestoreEmulatorConnected) { console.log('Connecting to Firestore emulator!'); connectFirestoreEmulator(db, '127.0.0.1', 8080); firestoreEmulatorConnected = true; } return db; } export function getFunctions(firebaseServerApp?: FirebaseServerApp) { if (!firebaseServerApp) { firebaseServerApp = getServerApp(); } const functions = getFunctionsFirebase(firebaseServerApp); if (process.env.NODE_ENV === 'development' && !functionsEmulatorConnected) { console.log('Connecting to Functions emulator!'); connectFunctionsEmulator(functions, '127.0.0.1', 5001); functionsEmulatorConnected = true; } return functions; }
然后在调用函数时使用 Functions 实例

import { Functions, httpsCallable } from "firebase/functions"; export async function testFunction(functions: Functions, input: string) { const testFunction = httpsCallable<string, RetType>(functions, 'testFunction'); const response = await testFunction(input); return response.data; }
import { getFunctions } from "@/lib/firebase/serverApp";
const functions = getFunctions();
const result = await testFunction(code);
按照这种模式,我们可以在服务器端访问 Firebase 服务。

© www.soinside.com 2019 - 2024. All rights reserved.