Firebase Integration with Node.js

Firebase provides simple and fully functional user management functionality out of the box. An important functionality is to provide user management from Node.js server side code like adding new user ,reset etc.

Here is the code below to Register a new User

 

exports.addUser = function(req, res) {
var wine = req.body;

var email = req.body.email;
console.log(req.body);
var password = req.body.password;
var name = req.body.name;

console.log(“Creating user for -“+email+”-“+password);
//firebase.auth().createUserWithEmailAndPassword(email, password).then(function(result) {
// wine.userid = result.uid;

// console.log(result)
//}).catch(function(error) {
// console.log(error)
//});

var defaultAuth = admin.auth();
admin.auth().createUser({
email: email,
emailVerified: false,
password: password,
displayName: name,
disabled: false
})
.then(function(userRecord) {
// A UserRecord representation of the newly created user is returned
console.log(“Created Firebase User successfully with id :”, userRecord.uid);
var wine = req.body;
wine.userId = userRecord.uid;
wine.timestamp = Date.now();
delete wine.password;
status = “201”;
var reply = JSON.stringify(wine);

db.collection(‘stars’, function(err, collection) {
collection.insert(wine, {safe:true}, function(err, result) {
if (err) {
wine.status = “200”;
wine.message = “An error occured”;
reply.set(‘status’,”201″);
res.status(201).send(wine);

} else {
console.log(‘Success: ‘ + JSON.stringify(result[0]));
status= “200”;
wine.status = “200”;
wine.message = “Account created Successfully”;
res.status(200).send(wine);
}
});
});
})
.catch(function(error) {
wine.message = “An error occured—“;
wine.status = “201”;

console.log(“User Creation onf Firebase failed:”, error);
res.status(201).send(wine);
});

}