Firebase身份验证 - 将用户从SQL迁移到Firebase

时间:2016-09-07 08:24:57

标签: javascript jquery firebase firebase-authentication

我正在重写我的网站,与Firebase结合使用JavaScript。到目前为止,我喜欢Firebase,但现在我需要将所有旧用户迁移到Firebase系统。

所有用户都有自定义的额外设置,我不想取下现在的服务器(€103,p / m)。所以我需要全部复制。据我所知,它需要逐个完成(createUserWithEmailAndPassword)。在服务器上,我创建了一个JSON对象,在新网站上我做了:

$.get("/alljsonfromalink", function (rdata) {
      var $jData = jQuery.parseJSON(rdata);
      var i = 0;
      for (var user in $jData) {
      whenYouFeelLikIt(user, $jData);
      }
    });


function whenYouFeelLikIt(i, $jData) {
            setTimeout(function() {
                firebase.auth().signInWithEmailAndPassword($jData[i].email, RandomPassword)
                    .then(function(){
                        console.log("Succes Login");
                        //if exist i set extra settings
                        setusersettings($jData[i], $jData[i].email);
                    })
                    .catch(function(error) {                      
                    var errorCode = error.code;
                    console.log(errorCode);
                    if(errorCode == "auth/user-not-found") {                            
                            firebase.auth().createUserWithEmailAndPassword($jData[i].email, RandomPassword).then(function () {
                                console.log("Created: " + $jData[i].email);
                            });
                        }
                });

                },2000 * (i));
            }

虽然有效,但即使暂停2秒,我也会得到20或30个插页:

firebase.js:73 Uncaught Error: We have blocked all requests from this device due to unusual activity. Try again later.

任何人都知道如何解决这个问题?

- 更新 -

JamieB建议使用.fetchProvidersForEmail()所以现在我使用

firebase.auth().fetchProvidersForEmail($jData[i].email)
                    .then(function(succes){
                        console.log(succes.length);
                        if(succes.length == 0) {
                            // the createUserWithEmailAndPassword() with timeout
                            create(i, $jData);
                        }                          
                    })

1 个答案:

答案 0 :(得分:1)

FIREBASE REFERENCE表示ICloneable返回包含非空createUserWithEmailAndPassword(email, password)的{​​{1}}。所以,它会返回一个承诺。这些可以推送到一个可以传递给firebase.Promise Promise方法的数组中。您可以使用以下函数将用户名和photoURL添加到将存储它们的Auth子系统。

任何其他用户属性都可以存储在firebase.User节点下,其中每个子ID都是用户的UID。底部的脚本显示了使用Promise.all的示例。

.all

...... Promise.all的一个例子:......

users