How to fix TypeError when using signInWithCredential on node.js? [EDIT: bug in Firebase 6.2.2]

主宰稳场 提交于 2019-12-24 06:35:53

问题


I am trying to sign in to firebase using a Google Id Token, as I'm developing an app that will be running on a raspberry pi, but when trying to sign in using the received token firebase crashes when using signInWithCredential. Here's my minimal reproducible example

var firebase = require("firebase/app");
require("firebase/auth");

const firebaseConfig = {
    ...
};
  // Initialize Firebase

firebase.initializeApp(firebaseConfig);

const id_token = "A_GOOGLE_ID_TOKEN";

var credential = firebase.auth.GoogleAuthProvider.credential(id_token);
firebase.auth().signInWithCredential(credential);

and it crashes with

TypeError: this.f is not a constructor
    at ai.a (C:\Dev\Crashing\node_modules\@firebase\auth\dist\auth.js:188:361)
    at yh (C:\Dev\Crashing\node_modules\@firebase\auth\dist\auth.js:171:191)
    at bi.o (C:\Dev\Crashing\node_modules\@firebase\auth\dist\auth.js:193:175)
    at ji (C:\Dev\Crashing\node_modules\@firebase\auth\dist\auth.js:191:239)
    at C:\Dev\Crashing\node_modules\@firebase\auth\dist\auth.js:197:181
    at new C (C:\Dev\Crashing\node_modules\@firebase\auth\dist\auth.js:18:736)
    at pi (C:\Dev\Crashing\node_modules\@firebase\auth\dist\auth.js:197:161)
    at C:\Dev\Crashing\node_modules\@firebase\auth\dist\auth.js:209:203
    at e.g (C:\Dev\Crashing\node_modules\@firebase\auth\dist\auth.js:22:101)
    at Kb (C:\Dev\Crashing\node_modules\@firebase\auth\dist\auth.js:25:195)

I tried it with several valid ID Tokens, but it seems that part is actually not broken, the credential itself appears to be fine, because signInWithCredential dies the same way even when I pass an arbitrary string as the id_token.

What am I doing wrong? Or could it possibly be an issue with Firebase JS SDK itself?

I am working on Windows 10, ver. 1809, running Node v10.15.3 and firebase JS SDK 6.2.2 (npm firebase package).

EDIT: I tried Firebase JS SDK version 6.2.0 and the code worked as expected! There is a bug in version 6.2.2 though.


回答1:


Firebase JS SDK 6.2.3 was just released today, and it fixes this bug: https://firebase.google.com/support/release-notes/js#authentication

Look like this is the git commit that fixes it: https://github.com/firebase/firebase-js-sdk/commit/728f4f54b4589c07a2d474deb94328a332c8fe39

I verified it with this mocha unit test:

const firebase = require('../../firebaseApp')
const chai = require('chai')

describe('firebase javascript sdk', () => {

  // This unit test verifies that the error message is as expected,
  // and not the error "this.f is not a constructor", which was caused
  // by a bug in version 6.2.1, and fixed in versin 6.2.3.
  // https://stackoverflow.com/questions/56716255/how-to-fix-typeerror-when-using-signinwithcredential-on-node-js-edit-bug-in-f
  it('should be able to checkActionCode', () => {
    return firebase.auth().checkActionCode('xyz')
      .catch(error => {
        // https://stackoverflow.com/questions/56716255/how-to-fix-typeerror-when-using-signinwithcredential-on-node-js-edit-bug-in-f
        chai.assert.equal(error.message,
          "The action code is invalid. This can happen if the code is malformed, expired, or has already been used.")
      })
  })
})




回答2:


The Firebase client SDKs generally do not work with nodejs. Firebase Authentication depends heavily on running inside a web browser in order to work correctly.

If you're running node, you might want to consider just using the Firebase Admin SDK to access your project without having to sign in.



来源:https://stackoverflow.com/questions/56716255/how-to-fix-typeerror-when-using-signinwithcredential-on-node-js-edit-bug-in-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!