AJGronevelt
AJGronevelt

Reputation: 561

Using Node.js to list all users in a Firebase iOS App

I am trying to create a list of all my firebase users in node.js. I am following the description as offered on https://firebase.google.com/docs/auth/admin/manage-users

When running the below code, I throws an error saying

TypeError: admin.auth(...).listUsers is not a function

I have a suspicion that the firebase documentation uses 'admin' in different ways in different places and that the 'admin' I use below from initialisation is not the one I should be using to list the users, but I can't quite work out how to correct it. The documentation is not very clear.

Please note that for the .initializeApp({...}) code I added some '...' where otherwise I have the correct text.

var admin = require('firebase-admin');
var serviceAccount = require('../serviceAccountKey');

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: 'https://...',
  databaseAuthVariableOverride: {
    uid: ' ... ',
  },
});

const db = admin.database();
const refUsersServer = db.ref('usersServer');

function listAllUsers(nextPageToken) {
  // List batch of users, 1000 at a time.
  // admin.auth().listUsers(1000, nextPageToken)

  admin.auth().listUsers(1000, nextPageToken)
    .then(function(listUsersResult) {
      listUsersResult.users.forEach(function(userRecord) {
        console.log("user", userRecord.toJSON());
      });

      // List next batch of users.
      if (listUsersResult.pageToken) {
        listAllUsers(listUsersResult.pageToken)
      }
    })
    .catch(function(error) {
      console.log("Error listing users:", error);
    });
}

// Start listing users from the beginning, 1000 at a time.
listAllUsers();

Upvotes: 1

Views: 257

Answers (1)

AJGronevelt
AJGronevelt

Reputation: 561

The code works fine but only in the latest firebase-admin version. In the earlier versions, the listUsers function didn't exit.

My mistake was that I didn't run the latest firebase-admin version which thanks to David 'G' I realised. Thanks.

Upvotes: 1

Related Questions