Firestore将社交登录添加到数据库

时间:2017-12-14 01:39:33

标签: firebase firebase-authentication google-cloud-firestore

任何人都有关于如何开展这项工作的建议。我有一个Facebook登录,如果该用户没有数据写入数据。

let users = firebase.usersCollection;


signUserInFacebook () {
const self = this;
var provider = new Firebase.auth.FacebookAuthProvider();
Firebase.auth().signInWithPopup(provider)
 .then(function(user) {
  usersRef.orderByChild('email').equalTo(user.email).once('value', 
   function(snapshot){
    var exist = (snapshot.val() !== null);

     if(!exist){
      users.add(user)
      }
    }

})

},

1 个答案:

答案 0 :(得分:0)

有几种方法可以做到这一点

  1. 如果您将用户的电子邮件(必须是唯一的)作为其信息的一部分,则可以执行此操作

            //check if this is first sign in, if yes save new user to db
        userRef.orderByChild('email').equalTo(user.email).once('value', function(snapshot){
            var exist = (snapshot.val() !== null);
    
            if(!exist){
              //add new user here
            }
    

    我通常喜欢将新创建的用户的uid设置为其密钥,因此每当我需要查询用户而不是使用像电子邮件这样的唯一值时,我都可以使用它们。

相关问题