2
votes

I am trying to use the onAuthStateChanged trigger but I am getting "is not a function" when using "firebase deploy". when executing :

firebase deploy

I got the following error:

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)

I have also tried firebase.default.auth.onAuthStateChanged as mentionned in the solution posted by Joao Lopes in this question ut it gave the same error!

I have already my domain added Firebase project>Authentication>Sign-In Method>Authorized Domains.

I am using:Ubuntu 16.04,npm 5.6.0,Firebase Spark. Here is my 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
}

and my 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);

So what am I missing?

2

2 Answers

4
votes

There is no onAuthStateChanged function in the firebase-admin SDK. This function is available only in the client-side package.

Firebase Admin Auth Reference: https://firebase.google.com/docs/reference/admin/node/admin.auth.Auth

Firebase Javascript Auth Reference: https://firebase.google.com/docs/reference/js/firebase.auth.Auth

1
votes

You're trying to login with the admin SDK.

onAuthStateChanged dosen't reside here. https://firebase.google.com/docs/reference/admin/node/admin.auth

Start by adding the firebase client package to your project.

$ npm install firebase --save

and change const firebase=require('firebase-admin'); => const firebase=require('firebase');

That might be sufficient. Not sure if you need to pass diffrent credentials to initializeApp() once it's on the client side.

Here's the authoriziation in the client documentation. https://firebase.google.com/docs/reference/node/firebase.auth.Auth