Firebase - create a user on the Node.js server

We have a large SPA using Firebase v2. We would like to switch to the new API, but we have the following problem:

Since the application is quite large, we have developed many integration tests, and for these tests we always need to reset the database and initialize it to a state where some users exist. However, we found that there really is no such thing as creating a user on the server ( Firebase createUserWithEmailAndPassword undefined method in node.js ), and we are not sure how to update the API and still be able to reset and initialize the database from the server.

In addition, we are forced to make this update because we noticed that Firebase v2 still uses the outdated Graph API v2.0 for Facebook OAuth and is not recommended after 8.8.2016. We understand that Firebase v2 will probably not update Graph API calls because v2 is deprecated. This, however, leaves us in complete solitude at the moment.

Any help on this topic please?

+2
firebase firebase-authentication
source share
2 answers

Like Firebase v3.3.0, you can create user accounts using Node, but the documentation is not very good at how to expose these methods.

To use user management methods, you need to initialize the application in node using the web API key, and not the configuration of the service account, which is viewed in the configuration guide.

// The Usual Service Account Init // This will not contain any user management methods on firebase.auth() this.app = firebase.initializeApp( { serviceAccount: 'path/to/serviceaccount/file.json', databaseURL: 'https://mydbfb.firebaseio.com' }, 'MyAppName'); // Web Client Init in Node.js // firebase.auth() will now contain user management methods this.app = firebase.initializeApp( { "apiKey": "my-api-key", "authDomain": "somedomain.firebaseapp.com", "databaseURL": "https://mydbfb.firebaseio.com", "storageBucket": "myfbdb.appspot.com", "messagingSenderId": "SomeId" }, 'MyAppName'); 

You can grab the api client key from the Firebase console from the web setup guide https://firebase.google.com/docs/web/setup

This is the only link I can find that explicitly refers to the need to initialize with the api key to make this work. https://groups.google.com/forum/#!msg/firebase-talk/_6Rhro3zBbk/u8hB1oVRCgAJ

+4
source share

The following is a working example of creating a Firebase user through Node.js

 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); var defaultAuth = admin.auth(); admin.auth().createUser({ email: email, emailVerified: false, password: password, displayName: name, disabled: false }) .then(function(userRecord) { 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('collname', 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); }); } 

See the following blog post for more details.

http://navraj.net/?p=53

thanks

+1
source share

All Articles