简体   繁体   English

Nuxt.js 插件,用于 Firebase - 服务器端

[英]Nuxt.js plugin for Firebase - server side

I'm trying to connect Nuxt 2.14 with firebase firestore我正在尝试将 Nuxt 2.14 与 firebase 火库连接
I'm writing a plugin for firebase that runs on the server我正在为在服务器上运行的 firebase 编写一个插件
I'm using "inject" to pass firebase.firestore() object as db.我正在使用“注入”将firebase.firestore() object 作为数据库传递。

But, when I'm using the $db I just injected但是,当我使用刚刚注入的 $db
and try to make a data variable equal to $db via asyncData,并尝试通过 asyncData 使数据变量等于 $db,

I get an error:我收到一个错误:
" Maximum call stack size exceeded " 超出最大调用堆栈大小


I guess the firebase.firestore() object has a "circular structure"我猜 firebase.firestore() object 有一个“圆形结构”
Any suggestions?有什么建议么?
Should I approach this differently?我应该以不同的方式处理这个问题吗?
Thank you谢谢

Here is the code for the different files I'm using:这是我正在使用的不同文件的代码:

.env .env

FB_DB_PATH=XXXXXX
FB_API_KEY=YYYYYY
FB_PROJECT_ID=ZZZZZZ
FB_AUTH_DOMAIN=EEEEEEE

nuxt.config.js nuxt.config.js

plugins: [
    { src: '~plugins/firebase.js', mode: 'server' }, 
    // runs on server to get the firebase privateRuntimeConfig params

],
  
publicRuntimeConfig: {
  baseURL: process.env.BASE_URL // just for test
    
},
privateRuntimeConfig: {
  fbProjectID: process.env.FB_PROJECT_ID,
  fbDBPath: process.env.FB_DB_PATH,
  fbApiKey: process.env.FB_API_KEY,
  fbAuthDomain: process.env.FB_AUTH_DOMAIN
},

firebase.js - this is the plugin file firebase.js - 这是插件文件

import firebase from 'firebase/app';
import 'firebase/auth';
import 'firebase/firestore';

export default function (context, inject) {
   
  if (!firebase.apps.length) {

    firebase.initializeApp({
      apiKey: context.$config.fbApiKey,
      authDomain: context.$config.fbAuthDomain,
      //databaseURL: context.$config.fbDBPath,
      projectId: context.$config.fbProjectID
    })
    
  }
  
 let auth = firebase.auth();
 let db = firebase.firestore();

 inject('db', db);
}

in page code:在页面代码中:

asyncData ( context ) {
      return { db: context.$db }
}

AFAIK, what you're trying to do won't work, and is not secure anyways. AFAIK,你试图做的事情是行不通的,而且也不安全。 Injecting your Firebase auth and firestore will inherently expose the API key client side, because they still need to use it there to communicate with the Firebase.注入您的 Firebase auth 和 firestore 将固有地暴露 API 密钥客户端,因为他们仍然需要在那里使用它来与 Firebase 通信。

Further, passing back an object with circular references doesn't work, as Vue cannot serialize it properly, and wouldn't know how to deserialize it on the client either.此外,使用循环引用传回 object 不起作用,因为 Vue 无法正确序列化它,并且也不知道如何在客户端上反序列化它。

If you're just fetching data from Firebase at page load, then you should do so in the asyndData method, and return the fetched data.如果您只是在页面加载时从 Firebase 获取数据,那么您应该在 asyndData 方法中执行此操作,并返回获取的数据。 However, if you need to make calls during runtime, such as storing user data, then you will need an API to keep the keys secure.但是,如果您需要在运行时进行调用,例如存储用户数据,那么您将需要一个 API 来保证密钥的安全。 I usually recommend starting with Nuxts serverMiddleware , as it works out of the box on Nuxt projects.我通常建议从 Nuxts serverMiddleware开始,因为它在 Nuxt 项目上开箱即用。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM