【发布时间】:2018-07-11 02:52:58
【问题描述】:
我无法弄清楚为什么我的一个 Firebase 应用程序使用 auth() 进行初始化,而一个没有。我已经按照这里的节点安装选项:https://firebase.google.com/docs/web/setup
**编辑:** 澄清一下,可行的方法是在 firebase 函数中使用 admin sdk。但是,我不明白它是如何连接到前端客户端 sdk 接口的。
每当我尝试在没有它的情况下在应用程序上调用任何 firebase.auth() 方法时,我都会不断收到错误消息。
什么原因会阻止我的应用在没有 auth() 的情况下进行初始化?我已经倾注了代码,并相信两者都以相同的方式合并了 firebase,但我一定遗漏了什么?
导致错误的示例函数
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
console.log("User signed in!", user);
} else {
console.log("User NOT signed in!");
}
});
产生的错误
index.js:40 Uncaught TypeError: firebase.auth is not a function
at Object.<anonymous> (index.js:40)
at __webpack_require__ (bootstrap 06efabd01e08e38c858a:19)
at bootstrap 06efabd01e08e38c858a:62
at bootstrap 06efabd01e08e38c858a:62
(anonymous) @ index.js:40
__webpack_require__ @ bootstrap 06efabd01e08e38c858a:19
(anonymous) @ bootstrap 06efabd01e08e38c858a:62
(anonymous) @ bootstrap 06efabd01e08e38c858a:62
应用 1 - 初始化 我已将我的数据替换为 xxx
var firebase = require('firebase/app');
require('firebase/auth');
var config = {
apiKey: "xxx",
authDomain: "xxx.firebaseapp.com",
databaseURL: "https://xxx.firebaseio.com",
projectId: "xxx",
storageBucket: "xxx.appspot.com",
messagingSenderId: "xxx"
};
firebase.initializeApp(config);
console.log("FIREBASE: ", firebase);
App 1 - Firebase 对象的控制台日志 注意“auth”存在
{__esModule: true, initializeApp: ƒ, app: ƒ, Promise: ƒ, …}
INTERNAL
:
{registerService: ƒ, createFirebaseNamespace: ƒ, extendNamespace: ƒ, createSubscribe: ƒ, ErrorFactory: ƒ, …}
Promise
:
ƒ Promise()
SDK_VERSION
:
"4.9.0"
User
:
ƒ Bk(a,b,c)
app
:
ƒ app(name)
apps
:
(...)
auth
:
ƒ (appArg)
default
:
{__esModule: true, initializeApp: ƒ, app: ƒ, Promise: ƒ, …}
initializeApp
:
ƒ initializeApp(options, name)
__esModule
:
true
get apps
:
ƒ getApps()
__proto__
:
Object
App 1 - 包括功能服务器端管理 SDK
var serviceAccount = require("./xxxxx62ba.json");
// Initialize the firebase admin app
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://xxx.firebaseio.com"
});
应用 2 - 初始化 我已将我的数据替换为 xxx
var firebase = require('firebase/app');
require('firebase/auth');
var config = {
apiKey: "xxx",
authDomain: "xxx.firebaseapp.com",
databaseURL: "https://xxx.firebaseio.com",
projectId: "xxx",
storageBucket: "xxx.appspot.com",
messagingSenderId: "xxx"
};
firebase.initializeApp(config);
应用 2 - Firebase 对象的控制台日志 请注意缺少“auth”
{__esModule: true, initializeApp: ƒ, app: ƒ, Promise: ƒ, …}
INTERNAL
:
{registerService: ƒ, createFirebaseNamespace: ƒ, extendNamespace: ƒ, createSubscribe: ƒ, ErrorFactory: ƒ, …}
Promise
:
ƒ Promise()
SDK_VERSION
:
"4.9.0"
app
:
ƒ app(name)
apps
:
(...)
default
:
{__esModule: true, initializeApp: ƒ, app: ƒ, Promise: ƒ, …}
initializeApp
:
ƒ initializeApp(options, name)
__esModule
:
true
get apps
:
ƒ getApps()
__proto__
:
Object
【问题讨论】:
标签: node.js firebase firebase-authentication