skip to Main Content

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

As the app 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 out there really is no such thing as creating a user on server anymore ( Firebase createUserWithEmailAndPassword method is undefined in node.js ), and we are quite unsure, how to upgrade the API and yet be able to reset and initialize the database from server.

Moreover, we are quite forced to do this upgrade, because we noticed that the Firebase v2, is still using the deprecated Graph API v2.0 for Facebook OAuth, and is not recommended for use after 8.8.2016. We understand that the Firebase v2 will probably not upgrade the calls to the Graph API, as the v2 is legacy. This, however, leaves us quite cornered for now.

Any help on this topic, please?

2

Answers


  1. As of Firebase v3.3.0 you are able to create user accounts using Node, but the documentation isn’t great on how to expose these methods.

    In order to use the user management methods, you need to initialize an application in node using your Web API key, and not the Service Account config that is walked through in the setup 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 your client api key from your Firebase console from the Web Setup guide
    https://firebase.google.com/docs/web/setup

    This is the only reference I could find that explicitly referenced the need to init with api key to get this to work.
    https://groups.google.com/forum/#!msg/firebase-talk/_6Rhro3zBbk/u8hB1oVRCgAJ

    Login or Signup to reply.
  2. Given below is a working example of creating 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);
    });
    
    }
    

    For details you can see the following blog post

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

    Thanks

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search