Firebase初始化而无需身份验证-firebase.auth不是函数 [英] Firebase Initializing Without Auth - firebase.auth is not a function
问题描述
我无法弄清楚为什么我的Firebase应用程序之一使用auth()进行初始化,而为什么不进行初始化.我在这里遵循了节点安装选项: https://firebase.google.com/docs/web /setup
I cannot figure out why one of my Firebase apps initializes with auth() and one does not. I have followed the node installation options here: https://firebase.google.com/docs/web/setup
****为了明确起见,一种有效的方法也是在firebase函数内部使用admin sdk.但是,我不知道该如何连接到前端客户端sdk接口.
** ** To clarify, the one that works is also using the admin sdk inside firebase functions. However, I don't understand how that might be connected to the front-end client sdk interface.
每当我尝试在没有它的情况下初始化的应用程序上调用任何firebase.auth()方法时,我都会不断收到错误消息.
I am continuously getting an error, whenever I try to call any firebase.auth() methods on the app that initializes without it.
什么原因会导致我的应用在没有auth()的情况下无法初始化?我已经仔细检查了代码,并相信两者都以相同的方式合并了Firebase,但是我一定会遗漏一些东西吗?
What are reasons that would prevent my app from initializing without auth()? I have poured over the code and believe both incorporate firebase in the same way, but I must be missing something?
导致错误的示例功能
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-包括服务器端admin 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对象的控制台日志 注意缺少身份验证"
{__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
推荐答案
更新:我最近开始再次遇到此错误. Firebase在没有Auth的情况下再次初始化(参见图片).当我更新或重新安装node_modules时,似乎偶尔会发生.
Update: I started encountering this error again recently. Firebase was initializing without Auth again (see image). It seemed to happen sporadically when I updated or re-installed my node_modules.
我在下面的链接上针对一个类似的问题发布了更新的答案-还列出了其他人尝试过的其他几种可能的解决方案.我的修复程序涉及从Mac上完全删除npm,nvm和节点,并使用nvm进行全新安装:
I posted an updated answer at the following link for a similar question - which also lists several other potential solutions other people experimented with. My fix involves completely removing npm, nvm, and node from my Mac, and doing a clean install with nvm: firebase.auth is not a function
我为此苦苦挣扎了3天,尝试完全按照文档说明以及其他许多示例的方式来实现sdk的各种不同方式,最后找到了一个可行的示例.这个git仓库有一个很棒的非常简单的例子,类似于我的react-router v4设置. tylermcginnis/react-router-firebase-auth
I struggled with this for 3 days, trying all sorts of different ways of implementing the sdk, exactly as according to documentation, and many other examples, and finally found an example that worked. This git repository has a great, and very simple example that was similar to my react-router v4 setup. tylermcginnis/react-router-firebase-auth
本质上,我的实现如下
firebase.js 我将firebase配置代码移到了一个单独的文件,然后将firebase.auth导出为名为firebaseAuth的常量.
firebase.js I moved the firebase configuration code to a separate file, and then exported firebase.auth as a constant called 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()作为函数被调用.
Redux Actions / Where it's Used I am using redux to handle the various actions than can happen with my components. In my actions file, I am importing the firebaseAuth variable, and using that to call the various firebase auth functions. As you can see here, firebaseAuth() is called as a function.
这成功在我的Firebase项目中创建了用户. (我知道所有内容都标记为"signIn",但它使用的是创建帐户"功能.)
This successfully created the user in my firebase project. (I am aware that everything is labeled "signIn", but it's using the create account function).
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中间件在操作中具有dispatch和getState.
By the way, I am using redux-form for user content entry, and redux-thunk middleware to have dispatch and getState in the actions.
这篇关于Firebase初始化而无需身份验证-firebase.auth不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!