Firebase authentication (is not a function, is not a constructor)

14,623

Solution 1

Your first error probably comes from a typo somewhere.

firebase.auth(...).signInWithLoginAndPassword is not a function

Notice it says signInWithLoginAndPassword, the function is called signInWithEmailAndPassword. In the posted code it's used correctly, so it's probably somewhere else.

firebase.auth(...).GoogleAuthProviders is not a constructor

You have not posted the code where you use this, but I assume this error happens when you create your provider variable, that you use in firebase.auth().signInWithPopup(provider)

That line should be var provider = new firebase.auth.GoogleAuthProvider();

Based on the error message, I think you might be doing new firebase.auth().GoogleAuthProvider(); Omit the brackets after auth, if that's the case.

Solution 2

There is no way to sign your node.js app into firebase with email+password or one of the social providers.

Server-side processes instead sign into Firebase using so-called service accounts. The crucial difference is in the way you initialize the app:

var admin = require('firebase-admin');
admin.initializeApp({
  serviceAccount: "path/to/serviceAccountCredentials.json",
  databaseURL: "https://databaseName.firebaseio.com"
});

See this page of the Firebase documentation for details on setting up a server-side process.

Solution 3

Do not call GoogleAuthProvider via an Auth() function.

According to the documentation you have to create an instance of GoogleAuthProvider.

let provider = new firebase.auth.GoogleAuthProvider()

Please check the following link https://firebase.google.com/docs/auth/web/google-signin

Share:
14,623
elzoy
Author by

elzoy

javascript/typescript/angular2/nodejs/express/mongodb/mongoose

Updated on June 07, 2022

Comments

  • elzoy
    elzoy almost 2 years

    I don't know what is wrong. I'm using Node.js and trying to log in using email/password and Google authentication. I have enabled all of them in Firebase console.

    npm Firebase version - 3.1.0

    part of code:

    var firebase = require('firebase');
    
    var config = {
      apiKey: "AIzaSyAH27JhfgCQfGmoGTdv_VaGIaX4P-qAs_A",
      authDomain: "pgs-intern.firebaseapp.com",
      databaseURL: "https://pgs-intern.firebaseio.com",
      storageBucket: "pgs-intern.appspot.com",
    };
    
    firebase.initializeApp(config);
    
    app.post('/login', function(req, res) {
      var auth = firebase.auth();
    
      firebase.auth().signInWithEmailAndPassword(req.body.login, req.body.password).catch(function(error) {
        // Handle Errors here.
        var errorCode = error.code;
        var errorMessage = error.message;
        // ...
      });
    }
    

    Error: firebase.auth(...).signInWithLoginAndPassword is not a function or Error: firebase.auth(...).GoogleAuthProviders is not a constructor when I write

    firebase.auth().signInWithPopup(provider).then(function(result) {
      // This gives you a Google Access Token. You can use it to access the Google API.
      var token = result.credential.accessToken;
      // The signed-in user info.
      var user = result.user;
      // ...
    }).catch(function(error) {
      // Handle Errors here.
      var errorCode = error.code;
      var errorMessage = error.message;
      // The email of the user's account used.
      var email = error.email;
      // The firebase.auth.AuthCredential type that was used.
      var credential = error.credential;
      // ...
    });
    

    I just did exactly what is in documentation.