Skip to content

Instantly share code, notes, and snippets.

@hurlatunde
Last active August 28, 2018 11:38
Show Gist options
  • Save hurlatunde/7810bc323aa961d6acb0bb0a1fb0e785 to your computer and use it in GitHub Desktop.
Save hurlatunde/7810bc323aa961d6acb0bb0a1fb0e785 to your computer and use it in GitHub Desktop.
//Firebase Auth instance
const auth = firebase.auth();
//Firebase Database instance
const database = firebase.database();
//get all input element as an array
var formData = formToArray('signUp');
const email = formData.email_address;
const password = formData.password;
const promise = auth.createUserWithEmailAndPassword(email, password);
promise.then(function (user) {
// get the current loggeg in users details
console.log(user);
saveCreatedAccount(formData, user, email);
})
promise.catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// ...
});
function saveCreatedAccount(formData, user, email) {
// the unique id from the saved user
var node_id = user.user.uid;
formData.node_id = node_id; // generated UId by firebase
formData.email = email; // emaill address
formData.type = 'password'; // the type of which the account was created
formData.created = new Date().valueOf(); // Current time inwhich the account was created
//save new user to data
writeUserData(node_id, formData);
}
function writeUserData(node_id, formData) {
database.ref('users/' + node_id).set(formData, function (error) {
if (error) {
showAlert(error);
showLoading('false', 'div_holder');
} else {
showAlert(node_id+' Saved. Please login', 1);
showLoading('false', 'div_holder');
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment