TypeError:firebase.auth(...)。onAuthStateChanged不是函数

时间:2018-03-08 19:56:29

标签: javascript firebase npm firebase-authentication google-cloud-functions

我正在尝试使用onAuthStateChanged触发器,但在使用“firebase deploy”时我得到“不是函数”。 执行时:

firebase deploy

我收到以下错误:

Error: Error occurred while parsing your function triggers.
TypeError: firebase.auth(...).onAuthStateChanged is not a function
    at Object.<anonymous> (/home/ihab/Desktop/BlueDot/bluedot/functions/index.js:33:17)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Module.require (module.js:587:17)
    at require (internal/module.js:11:18)
    at /usr/lib/node_modules/firebase-tools/lib/triggerParser.js:18:11
    at Object.<anonymous> (/usr/lib/node_modules/firebase-tools/lib>/triggerParser.js:38:3)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Function.Module.runMain (module.js:684:10)

我也尝试了在Joao Lopes在this question发布的解决方案中提到的firebase.default.auth.onAuthStateChanged,它给出了同样的错误!

我已经在我的域中添加了Firebase项目&gt;身份验证&gt;登录方法&gt;授权域。

我正在使用:Ubuntu 16.04,npm 5.6.0,Firebase Spark。 这是我的package.json:

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "serve": "firebase serve --only functions",
    "shell": "firebase experimental:functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "dependencies": {
    "consolidate": "^0.15.0",
    "express": "^4.16.2",
    "firebase-admin": "^5.8.2",
    "firebase-functions": "^0.8.1",
    "handlebars": "^4.0.11"
  },
  "private": true
}

和我的index.js:

const functions = require('firebase-functions');
const firebase=require('firebase-admin');
const express=require('express');
const engines=require('consolidate'); 

firebase.initializeApp(functions.config().firebase);


const app=express();
app.engine('hbs',engines.handlebars);
app.set('views','./views');
app.set('view engine','hbs');

app.get('/',(req,res)=>{
    res.render('index',{Loginsignup:"LoginSignup"});
});

/*Here is the probleme*/
firebase.auth().onAuthStateChanged(function(user) {
     user.sendEmailVerification();
     if(!auth.currentUser.emailVerified){
         user.sendEmailVerification();
         console.log('signed in');
     }
});
/********************/

exports.app = functions.https.onRequest(app);

那么我错过了什么?

2 个答案:

答案 0 :(得分:1)

onAuthStateChanged SDK中没有firebase-admin个功能。此功能仅在客户端软件包中可用。

Firebase管理员身份验证参考:https://firebase.google.com/docs/reference/admin/node/admin.auth.Auth

Firebase Javascript验证参考:https://firebase.google.com/docs/reference/js/firebase.auth.Auth

答案 1 :(得分:0)

您正尝试使用管理员SDK登录。

onAuthStateChanged不要住在这里。 https://firebase.google.com/docs/reference/admin/node/admin.auth

首先将firebase客户端软件包添加到您的项目中。

$ npm install firebase --save

并更改const firebase=require('firebase-admin'); =&gt; const firebase=require('firebase');

这可能就足够了。不确定在客户端是否需要将不同的凭据传递给initializeApp()

这是客户端文档中的授权。 https://firebase.google.com/docs/reference/node/firebase.auth.Auth