Copy / migrate users from Firebase Auth to Google Identity Platform

873 views Asked by At

I've been using Firebase auth to handle sign-in / sign up in my application (react/express js) which contains now +2000 users.

We are going to migrate to use a new project in Identity Platform (to benefit from SAML etc) which uses the same SDK of the firebase. So now we have 2 separate projects, is there a way to copy all users in that Identity Platform new project? or can I combine them and use them simultaneously?

Thanks

1

There are 1 answers

0
0xori On

from the docs: https://cloud.google.com/identity-platform/docs/migrate-users-between-projects-tenants

var admin = require('firebase-admin');

var sourceApp = admin.initializeApp({
  credential: admin.credential.cert('source-project-service-account.json'),
}, 'source-app');

var targetApp = admin.initializeApp({
  credential: admin.credential.cert('target-project-service-account.json'),
}, 'target-app');

var authFrom = sourceApp.auth();
var authTo = targetApp.auth();

function migrateUsers(userImportOptions, nextPageToken) {
 var pageToken;
 authFrom.listUsers(1000, nextPageToken)
   .then(function(listUsersResult) {
    var users = [];
    listUsersResult.users.forEach(function(user) {
      var modifiedUser = user.toJSON();
      // Convert to bytes.
      if (user.passwordHash) {
       modifiedUser.passwordHash = Buffer.from(user.passwordHash, 'base64');
       modifiedUser.passwordSalt = Buffer.from(user.passwordSalt, 'base64');
      }
      // Delete tenant ID if available. This will be set automatically.
      delete modifiedUser.tenantId;
      users.push(modifiedUser);
    });
    // Save next page token.
    pageToken = listUsersResult.pageToken;
    // Upload current chunk.
    return authTo.importUsers(users, userImportOptions);
   })
   .then(function(results) {
    results.errors.forEach(function(indexedError) {
       console.log('Error importing user ' + indexedError.index);
     });
     // Continue if there is another page.
     if (pageToken) {
         migrateUsers(userImportOptions, pageToken);
     }
   })
   .catch(function(error) {
     console.log('Error importing users:', error);
   });
}
var userImportOptions = {
 hash: {
   algorithm: 'SCRYPT',
   // The following parameters can be obtained from the "Users" page in the
   // Cloud Console. The key must be a byte buffer.
   key: Buffer.from('base64-secret', 'base64'),
   saltSeparator: Buffer.from('base64SaltSeparator', 'base64'),
   rounds: 8,
   memoryCost: 14
 }
};

migrateUsers(userImportOptions);