Firebase身份验证 - 将用户从SQL迁移到Firebase [英] Firebase Authentication - Migrate users from SQL to Firebase

查看:164
本文介绍了Firebase身份验证 - 将用户从SQL迁移到Firebase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



所有用户都有自定义额外设置,我不想把服务器放在现在的位置(€103,p / m)。所以我需要这一切被复制。据我所知,它需要逐一完成( createUserWithEmailAndPassword )。在服务器上,我创建了一个JSON对象,并在新的站点上执行:

  $。get(/ alljsonfromalink,函数(rdata){
var $ jData = jQuery.parseJSON(rdata);
var i = 0;
($ jData中的var用户){
whenYouFeelLikIt(user, $ jData);
}
});


函数whenYouFeelLikIt(i,$ jData){
setTimeout(function(){
firebase.auth()。signInWithEmailAndPassword($ jData [i] .email ,RandomPassword)
.then(function(){
console.log(Succes Login);
//如果存在我设置额外的设置$ b $ 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));



$ b $ p
$ b

它可以工作,但即使在2秒的超时时间后,或30个插入:

  firebase.js:73未被捕获的错误:由于不正常的活动,我们已经阻止了来自此设备的所有请求。稍后再试。 

任何人有一个想法如何解决这个问题?

- 更新 -



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

<$ fetchProvidersForEmail($ jData [i] .email)
.then(function(succes){
console.log(succes.length) ;
if(succes.length == 0){
//带有超时值的createUserWithEmailAndPassword()
create(i,$ jData);
}
})


解决方案

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



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

 函数registerPasswordUser(email,displayName,password,photoURL){ 
var user = null;
// NULLIFY EMPTY ARGUMENTS
for(var i = 0; i< arguments.length; i ++){
arguments [i] = arguments [i]?参数[i]:null;

$ auth.createUserWithEmailAndPassword(email,password)
.then(function(){
user = auth.currentUser;
user.sendEmailVerification(); $ b ($ {
})
.then(函数(){
user.updateProfile({
displayName:displayName,
photoURL:photoURL
});
})
.catch(function(error){
console.log(error.message);
});
console.log('验证链接已发送到'+ email +'。');

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

 函数loadMeetings(city,state){

return ref.child('states')。child(state ).child(city).once('value')。then(function(snapshot){
var reads = [];
snapshot.forEach(function(childSnapshot){
var id childSnapshot.key();
var promise = ref.child('meetings')。child(id).once('value')。then(function(snap){
return snap.val ();
},function(error){
// Promise was rejected。
console.error(error);
});
reads.push (promise);
));
return Promise.all(reads);
},function(error){
// Promise was rejected。
console .error(error);
})。then(function(values){
//为每个快照做一些
});
}


I am rewriting a website of mine, to JavaScript in combination with Firebase. So far, I love Firebase, but now I am on a part where I need to migrate all of my old users to the Firebase system.

All the users have custom extra settings and i wan't to take down the server where it's on now (€ 103, p/m). So I need it all to be copied. As far as I know, it needs to be done one by one (createUserWithEmailAndPassword). On the server, I make a JSON object and on the new site I do:

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

It works, but even with a 2-second timeout, I got after 20 or 30 inserts:

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

Anyone has an idea how to work around this?

--Update--

JamieB suggested to use .fetchProvidersForEmail() So now I use

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

解决方案

FIREBASE REFERENCE indicates that createUserWithEmailAndPassword(email, password) returns firebase.Promise containing non-null firebase.User. So, it returns a promise. These can be pushed into an array that can be handed to the .all Promise method. You can use the below function to add the username and photoURL to the Auth subsystem where they will be stored.

Any additional user properties can be store under a users node where each child id is the UID of the user. The script at the bottom shows an example of using Promise.all.

  function registerPasswordUser(email,displayName,password,photoURL){
    var user = null;
    //NULLIFY EMPTY ARGUMENTS
    for (var i = 0; i < arguments.length; i++) {
      arguments[i] = arguments[i] ? arguments[i] : null;
    }
    auth.createUserWithEmailAndPassword(email, password)
    .then(function () {
      user = auth.currentUser;
      user.sendEmailVerification();
    })
    .then(function () {
      user.updateProfile({
        displayName: displayName,
        photoURL: photoURL
      });
    })
    .catch(function(error) {
      console.log(error.message);
    });
    console.log('Validation link was sent to ' + email + '.');
  }

...an example of Promise.all:...

function loadMeetings(city,state) {

    return ref.child('states').child(state).child(city).once('value').then(function(snapshot) {
    var reads = [];
    snapshot.forEach(function(childSnapshot) {
        var id = childSnapshot.key();
        var promise = ref.child('meetings').child(id).once('value').then(function(snap) {
            return snap.val();
        }, function(error) {
            // The Promise was rejected.
            console.error(error);
        });
        reads.push(promise);
    });
    return Promise.all(reads);
    }, function(error) {
        // The Promise was rejected.
        console.error(error);
    }).then(function(values) {
        //for each snapshot do something
    });
}

这篇关于Firebase身份验证 - 将用户从SQL迁移到Firebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆