Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/433.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Firebase Google身份验证-检查用户是否首次登录_Javascript_Firebase_Firebase Authentication - Fatal编程技术网

Javascript Firebase Google身份验证-检查用户是否首次登录

Javascript Firebase Google身份验证-检查用户是否首次登录,javascript,firebase,firebase-authentication,Javascript,Firebase,Firebase Authentication,我在我的网站上使用了一个注册/登录,它也支持谷歌登录,问题是我在Firestore中用注册的uid创建了一个新的数据文档,但我不能在谷歌登录时这样做,否则它会删除现有的条目,因为我只能用谷歌登录,但我需要一种方法来检查用户是否已经存在 googlelogin: function() { var provider = new firebase.auth.GoogleAuthProvider(); firebase.auth().signInWithPopup(provider)

我在我的网站上使用了一个注册/登录,它也支持谷歌登录,问题是我在Firestore中用注册的uid创建了一个新的数据文档,但我不能在谷歌登录时这样做,否则它会删除现有的条目,因为我只能用谷歌登录,但我需要一种方法来检查用户是否已经存在

 googlelogin: function()
{
    var provider = new firebase.auth.GoogleAuthProvider();

    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;

         db.collection("user").doc(firebase.auth().currentUser.uid).set({
            name: firebase.auth().currentUser.email,
            favorites: []
  })

        $('#modal1').modal('close');

      // ...
        }).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;
      // ...
      });
},

您可以检查
result.additionalUserInfo.isNewUser
以查看用户是否是第一次登录。
signInWithPopup
返回的结果是一个
UserCredential
对象:

Firebase云函数有一个内置函数,只有在创建新用户时才运行,您可以使用它来触发特定功能。@AlexanderStaroselsky非常感谢这一帮助,我现在就可以使用它了。