Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node - this.f is not a constructor when calling firebase.auth() #1906

Closed
mikkopaderes opened this issue Jun 24, 2019 · 15 comments
Closed

Node - this.f is not a constructor when calling firebase.auth() #1906

mikkopaderes opened this issue Jun 24, 2019 · 15 comments
Assignees

Comments

@mikkopaderes
Copy link

mikkopaderes commented Jun 24, 2019

[REQUIRED] Describe your environment

  • Operating System version: Cloud Function
  • Browser version: Node 8
  • Firebase SDK version: 6.2.2
  • Firebase Product: Auth

[REQUIRED] Describe the problem

Steps to reproduce:

Simply calling firebase.auth().signInWithEmailAndPassword() in a Node environment doesn't work. This is for Firebase JS SDK, not Firebase Admin.

Relevant Code:

Someone posted the issue in SO but I'm seeing the problem too on my side.

@jpreynat
Copy link

We have the exact same problem, but it only happens in the Node 10 runtime, not with Node 8.
Here is the stacktrace of our error (read from bottom to top):

image

The auth.signInWithAuthToken is internal and calls the firebase.auth().signInWithCustomToken() under the hood, which is where this error happens.

It seems that the pre-installed version of Firebase in Node uses the google-closure-library which leads to this error.

@rapomon
Copy link

rapomon commented Jun 24, 2019

I have the same issue. I use node 12.4.0

@wti806
Copy link
Contributor

wti806 commented Jun 24, 2019

@Feiyang1 Auth breaks on Node starting from v6.2.1. It works in v6.2.0. Is there anything being changed since 6.2.1 that could cause this issue?

@bojeil-google
Copy link
Contributor

The issue is likely due to this commit: 7866e22

They replaced:
import { XMLHttpRequest } from 'xmlhttprequest';

With:
import XMLHttpRequest from 'xmlhttprequest';

@subhamtandon
Copy link

subhamtandon commented Jun 24, 2019

I am also getting the same issue. I am using firebase v6.2.2. Link to my question on stackoverflow
https://stackoverflow.com/questions/56727931/how-to-fix-an-error-undefined-and-error-message-this-f-is-not-a-constructor-i

@wti806
Copy link
Contributor

wti806 commented Jun 24, 2019

The bug was introduced in v6.2.1. You can use the v6.2.0 as for now. We are working on a fix for this in #1909. And We will make a patch release ASAP. Thanks for report the issue.

@mikkopaderes
Copy link
Author

I'm not sure why but I'm also unable to use Firestore and Auth starting in 5.x. They keep showing that firestore/auth is not a function. 6.2.2 works for Firestore but auth returns the f not a constructor error.

Thus, Firebase is completely unusable on my case for node js.

@rapomon
Copy link

rapomon commented Jun 25, 2019

New release 6.2.3 fix this issue.

@mikkopaderes
Copy link
Author

@rapomon this brings back my problem though of firebase.auth() is not a function. My Cloud Function in Node 8 is as simple as this:

const functions = require('firebase-functions');
const firebase = require('firebase/app');

require('firebase/auth');

exports.app = functions.https.onRequest(async (req, res) => {
  firebase.initializeApp({ ... });
  await firebase.auth().signInWithEmailAndPassword(...);
  res.send("Hello from Firebase!");
});

image

Again, I can trace back that problem for both firestore and auth all the way back to Firebase 5.x version. The version 6.2.2 fixes the issue for Firestore but Auth gets the this.f not a constructor error. Now in 6.2.3, both Firestore and Auth gets the not a function error again.

@rapomon
Copy link

rapomon commented Jun 25, 2019

@rapomon this brings back my problem though of firebase.auth() is not a function. My Cloud Function in Node 8 is as simple as this:

const functions = require('firebase-functions');
const firebase = require('firebase/app');

require('firebase/auth');

exports.app = functions.https.onRequest(async (req, res) => {
  firebase.initializeApp({ ... });
  await firebase.auth().signInWithEmailAndPassword(...);
  res.send("Hello from Firebase!");
});

image

Again, I can trace back that problem for both firestore and auth all the way back to Firebase 5.x version. The version 6.2.2 fixes the issue for Firestore but Auth gets the this.f not a constructor error. Now in 6.2.3, both Firestore and Auth gets the not a function error again.

I'm not using signInWithEmailAndPassword, I use signInWithCredential. Yesterday with version 6.2.2 gets the this.f not a constructor error, with 6.2.0 works fine, and today, after update to 6.2.3 works fine again.

// Build Firebase credential with the Facebook access token.
        var credential = firebase.auth.FacebookAuthProvider.credential(accessToken);

        // Sign in with credential from the Google user.
        firebase.auth().signInWithCredential(credential)
            .then(function(result) {
                if (result.credential) {
                // This gives you a Facebook Access Token. You can use it to access the Facebook API.
                    var token = result.credential.accessToken;
                // ...
                }
                // The signed-in user info.
                var user = result.user;
                json(res, 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;
                // ...
                throw new AppError(error);
            });

@mikkopaderes
Copy link
Author

This makes me wonder if the Node.js Client SDK actually works for Cloud Functions. Maybe it only allows the Admin SDK?

Can you confirm this @bojeil-google ?

My use case is I have an Ember app that I want to hook up to Cloud Functions for server side rendering. This should be similar to Angular Universal, just using Ember framework.

@bojeil-google
Copy link
Contributor

The Node.js client SDK should work in a Cloud Functions environment. There are no restrictions on it based on environment.
The firebase.auth() is not a function is unrelated to this thread. The issue in this thread is about a missing Node.js polyfill which should be now fixed.

@Feiyang1
Copy link
Member

@mikebonnell There is a compatibility issue when using firebase js sdk and firebase-admin together. I provided a detailed explanation and workaround here - #1696 (comment)
Hope it helps.

@mikkopaderes
Copy link
Author

@Feiyang1 I'm not usinf Firebase Admin at all so I don't think that's the problem.

@bojeil-google have you guys tried it out? Sure the issue for f constructor is gone but the auth library itself still isn't working for me. If any, the error was just replaces by something else.

My code snippet is very simple. Please try deploying it on a Cloud Function running in Node 8.

const functions = require('firebase-functions');
const firebase = require('firebase/app'); 

require('firebase/auth');

exports.app = functions.https.onRequest(async (req, res) => {
  firebase.initializeApp({ ... });
  await firebase.auth().signInWithEmailAndPassword(...);
  res.send("Hello from Firebase!");
});

@mikkopaderes
Copy link
Author

So it turns out that Firebase Admin is being implicitly initialized in Cloud Functions even if you're not using it. I was able to confirm this due to a small log when running locally. That same log doesn't show in Cloud Functions when it's already deployed. So what @Feiyang1 posted was actually the issue.

Is there a benefit on why Firebase Admin is being implicitly initialized? I think this should be noted in the docs if it isn't yet. I also think that #1696 shouldn't be resolved just because the question was addressed. This is a bug and should be actually fixed.

@firebase firebase locked and limited conversation to collaborators Oct 10, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

8 participants