我无法弄清楚为什么我的一个 Firebase 应用程序使用 auth() 进行初始化,而另一个则没有。我在这里遵循了节点安装选项:https://firebase.google.com/docs/web/setup 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);
应用程序 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
应用程序 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
Update: 我最近又开始遇到这个错误。 Firebase 再次在没有身份验证的情况下进行初始化(参见图片)。当我更新或重新安装我的node_modules时,它似乎偶尔发生。
我在以下链接中发布了类似问题的更新答案 - 其中还列出了其他人尝试过的其他几个潜在解决方案。我的修复涉及从我的 Mac 中完全删除 npm、nvm 和 node,并使用 nvm 进行全新安装:firebase.auth 不是函数 https://stackoverflow.com/questions/48592656/firebase-auth-is-not-a-function/54427116#54427116
我为此苦苦挣扎了3天,尝试了各种不同的方式来实现sdk,完全按照文档和许多其他示例,终于找到了一个有效的示例。这个 git 存储库有一个很棒且非常简单的示例,类似于我的 React-router v4 设置。tylermcginnis/react-router-firebase-auth https://github.com/tylermcginnis/react-router-firebase-auth
本质上,我的实现如下
firebase.js我将 firebase 配置代码移至单独的文件中,然后将 firebase.auth 导出为名为 firebaseAuth 的常量。
import firebase from 'firebase'
const config = {
apiKey: "AIza....pPk",
authDomain: "project.firebaseapp.com",
databaseURL: "https://project.firebaseio.com",
}
firebase.initializeApp(config)
export const firebaseAuth = firebase.auth
Redux 操作/使用地点我正在使用 redux 来处理组件可能发生的各种操作。在我的操作文件中,我导入 firebaseAuth 变量,并使用它来调用各种 firebase auth 函数。正如您在这里所看到的,firebaseAuth() 作为函数被调用。
这成功地在我的 firebase 项目中创建了用户。 (我知道所有内容都标记为“登录”,但它使用的是创建帐户功能)。
import { firebaseAuth } from '../../config/firebase'
/**
* Sign In User
* Signs in our end user using firebase auth
*/
export const signIn = () => {
return (dispatch, getState) => {
console.log("Sign In - In Progress");
// Get the state from the redux store
const reduxState = getState();
let email = reduxState.form.signIn.values.email;
let password = reduxState.form.signIn.values.password;
firebaseAuth().createUserWithEmailAndPassword(email, password).catch(function(error) {
// Handle Errors here.
console.log("Login Errors: " + error.code + error.message);
// ...
});
}
}
顺便说一句,我使用 redux-form 进行用户内容输入,并使用 redux-thunk 中间件在操作中进行调度和 getState。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)