app.auth(...)。signInWithCredential不是函数firebase错误

时间:2019-10-06 17:34:06

标签: node.js reactjs firebase firebase-authentication firebase-admin

我正在尝试将Google登录名添加到我的node js firebase应用程序中。我收到以下错误: app.auth(...)。signInWithCredential不是函数

这是服务器上的代码

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

在客户端上

 // We need to register an Observer on Firebase Auth to make sure auth is initialized.
        var unsubscribe = app.auth().onAuthStateChanged((firebaseUser) => {
            unsubscribe();
            // Check if we are already signed-in Firebase with the correct user.
            // Build Firebase credential with the Google ID token.
            var credential = firebase.auth.GoogleAuthProvider.credential(googleUser.getAuthResponse().id_token);
            // Sign in with credential from the Google user.
            firebase.auth().signInWithCredential(credential).catch( (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;
                console.dir(errorMessage);
            }).then( (success) => {
                console.dir(success);

                var id_token = googleUser.getAuthResponse().id_token;

                axios.post('/auth/google-login', {
                    id_token: id_token
                }).then(response => {
                    var result = response.data;
                    if (result.status === "ok") {
                        this.setState({complete: true});
                    } else if (result.error) {
                        this.setState({error: result.error});
                    }
                }).catch(error => {
                    console.dir(error);
                    this.setState({error: "error"});
                });
            });
        });

我做错了什么?

在nodejs文件的顶部,我有

var app = require('./base');

其中base:

var serviceAccount = require("./service-account-file.json");

const app =   fadmin.initializeApp({
    credential: fadmin.credential.cert(serviceAccount),
    databaseURL: "https://test.firebaseio.com"
});
module.exports = app;

2 个答案:

答案 0 :(得分:0)

嗨,有迹象表明该问题始于节点版本6.21。那 app.auth()。signInWithCredential(凭证) 不工作

在v6.2.0中有效。

尝试signInWithEmailAndPassword 使用最新版本的节点

答案 1 :(得分:0)

函数signInWithCredentialFirebase JavaScript SDK的一部分

  

Firebase JavaScript SDK实现了使用Firebase服务的应用程序使用的 客户端 库。

Firebase Admin SDK旨在在服务器或其他特权环境上运行。 Admin SDK不包括登录功能。