Coder1000 Coder1000 - 1 year ago 61
Node.js Question

How to register a User in Firebase using Node.js?


0) The user is created in the auth system in Firebase (I see it in the Auth tab),

1) But no changes are made to the database.

2) The page seems to be stuck infinitely loading.

3) Only "Started 1..." is logged to the console.

CODE:'/register', function(req, res, next) {
var username = req.body.username;
var email =;
var password = req.body.password;
var password2 = req.body.password2;

// Validation
req.checkBody('username', 'Username is required').notEmpty();
req.checkBody('email', 'Email is required').notEmpty();
req.checkBody('email', 'Email is not valid').isEmail();
req.checkBody('password', 'Password is required').notEmpty();
req.checkBody('password2', 'Passwords do not match').equals(req.body.password);

var errors = req.validationErrors();

res.render('users/register', {
errors: errors
} else {
console.log("Started 1...");
firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error, userData) {
console.log("Started 2...");
var errorCode = error.code;
var errorMessage = error.message;
req.flash('error_msg', 'Registration Failed. Make sure all fields are properly filled.' + error.message);
console.log("Error creating user: ", error);
} else {
console.log("Successfully created");
console.log("Successfully created user with uid:", userData.uid);
var user = {
uid: userData.uid,
email: email,
username: username

var userRef = firebase.database().ref('users/');

req.flash('success_msg', 'You are now registered and can login');



This is what seems to be happening:

The user is created in the auth System. The page loads for about 5 minutes (very long!), then tells me the registration failed because the email address is already in use (it was not).

It seems the user is created but the registration fails because the code loops back after the user is created as if it had not been created, therefore creating an error of type : this email address is already in our database.

But why does this happen ?

Uzi Uzi
Answer Source

not sure this is all, but here's what i think happens:

the user is created, but you are not handling it properly. there is no handling of a success case (fulfilled promise), only a rejected one. also, when you're trying to write to the database when an error occurs, that means the user is not authenticated, which means that if you haven't changed your firebase security rules, you won't be able to write. (the default firebase security rules prevents non-authenticated users from reading/writing to your database)

this line:

firebase.auth().createUserWithEmailAndPassword(email,password) .catch(function(error, userData) { ...

should be changed to this:

firebase.auth().createUserWithEmailAndPassword(email, password) .then(userData => { // success - do stuff with userData }) .catch(error => { // do stuff with error })

mind that when an error occurs, you won't have access to userData, just the errors.

Hope this helps!