【问题标题】:Firebase Initializing Without Auth - firebase.auth is not a function没有身份验证的 Firebase 初始化 - firebase.auth 不是函数
【发布时间】: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


    【解决方案1】:

    更新:我最近又开始遇到这个错误。 Firebase 在没有 Auth 的情况下再次初始化(见图)。当我更新或重新安装我的 node_modules 时,它似乎偶尔发生。

    我在以下链接中针对类似问题发布了更新的答案 - 其中还列出了其他人尝试过的其他几种潜在解决方案。我的修复包括从我的 Mac 中完全删除 npm、nvm 和 node,并使用 nvm 进行全新安装:firebase.auth is not a function


    我为此苦苦挣扎了 3 天,尝试了各种不同的方式来实现 sdk,完全按照文档和许多其他示例,最后找到了一个有效的示例。这个 git 存储库有一个很棒且非常简单的示例,类似于我的 react-router v4 设置。 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 项目中创建了用户。 (我知道所有东西都标有“signIn”,但它正在使用创建帐户功能)。

    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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多