Reputation: 293
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);
}
})
Upvotes: 0
Views: 420
Reputation: 18595
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
});
}
Upvotes: 1