Firebase 初始化无需身份验证- firebase.auth 不是一个函数。

9 浏览
0 Comments

Firebase 初始化无需身份验证- firebase.auth 不是一个函数。

我无法理解为什么我的一个Firebase应用程序会初始化auth(),而另一个不会。我已经按照此处的节点安装选项进行操作:https://firebase.google.com/docs/web/setup

**编辑:**为了澄清,能正常工作的那个应用程序也在Firebase函数中使用了管理员sdk。然而,我不明白这可能与前端客户端sdk接口有什么关联。

每当我尝试在没有初始化auth()的应用程序上调用任何firebase.auth()方法时,都会出现错误。

有什么原因会阻止我的应用程序在没有auth()的情况下进行初始化?我仔细检查了代码,认为两者以相同的方式使用firebase,但肯定有什么我漏掉了的地方?

导致错误的示例函数

firebase.auth().onAuthStateChanged(function(user) {
    if (user) {
        console.log("用户已登录!", user);
    } else {
        console.log("用户未登录!");
    }
});

导致错误的结果

index.js:40 Uncaught TypeError: firebase.auth is not a function
    at Object. (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");
// 初始化firebase管理员应用程序
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

0
0 Comments

使用Firebase时出现了一个错误:Firebase Initializing Without Auth - firebase.auth is not a function。这个问题的出现是因为在初始化Firebase时没有包含Auth模块。解决方法是将Firebase的配置代码放在一个单独的文件中,并将firebase.auth导出为一个常量。

首先,将Firebase的配置代码移动到一个名为firebase.js的文件中,并将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的Actions文件中,引入firebaseAuth,并使用它来调用各种Firebase Auth函数。在signIn函数中,通过firebaseAuth()调用createUserWithEmailAndPassword函数来创建用户。代码如下:

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中间件来在actions中使用dispatch和getState函数。

通过以上步骤,我们成功解决了Firebase Initializing Without Auth - firebase.auth is not a function的问题。

0