Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Admin, Import Users doesn't work

My problem is: need to change initial email (email at registered) when User changed email in app. Firebase Support suggests me to use Admin SDK: getUser(uid) and importUsers(UserRecord). I did it, but importUsers(UserRecord) doesn't work for me. My code at index.js file:

Get User data:

 admin.auth().getUser("DRDYMZLfg2MkKm5JklYWc9LmlOo2")
  .then(function(userRecord) {
    // See the UserRecord reference doc for the contents of userRecord.
    console.log('Successfully fetched user data:', userRecord.toJSON());
  })
  .catch(function(error) {
    console.log('Error fetching user data:', error);
  });    

Import User:

 let userImportRecords = [
{
 uid: 'DRDYMZLfg2MkKm5JklYWc9LmlOo2',
 email: '[email protected]',
 emailVerified: false,
 displayName: undefined,
 photoURL: undefined,
 phoneNumber: undefined,
 disabled: false,
 metadata:
  { lastSignInTime: 'Wed, 19 Jun 2019 03:42:38 GMT',
  creationTime: 'Wed, 19 Jun 2019 03:40:01 GMT' },
  passwordHash: 'UkVEQUNURUQ=',
  passwordSalt: undefined,
  customClaims: undefined,
  tokensValidAfterTime: 'Wed, 19 Jun 2019 03:42:40 GMT',
  providerData:
  [ { uid: '[email protected]',
   displayName: undefined,
   email: '[email protected]',
   photoURL: undefined,
   providerId: 'password',
   phoneNumber: undefined } ]
  },
  //...
  ];



admin.auth().importUsers(userImportRecords)
  .then(function(userImportResult) {
    console.log(userImportResult);
    // The number of successful imports is determined via: userImportResult.successCount.
    // The number of failed imports is determined via: userImportResult.failureCount.
    // To get the error details.
    userImportResult.forEach(function(indexedError) {
        // The corresponding user that failed to upload.
        console.log(userImportRecords[indexedError.index].uid +' failed to import',
            indexedError.error);
    });
  })
  .catch(function(error) {
    // Some unrecoverable error occurred that prevented the operation from running.
  });

In Terminal I have

{ successCount: 0,
  failureCount: 1,
  errors: [ { index: 0, error: [FirebaseAuthError] } ] }

How to solve FirebaseAuthError?

UPD: the error is

{ Error: The password hash must be a valid byte buffer.
    at FirebaseAuthError.FirebaseError [as constructor] (/Users/xxx/node_modules/firebase-admin/lib/utils/error.js:42:28)
    at FirebaseAuthError.PrefixedFirebaseError [as constructor] (/Users/xxx/node_modules/firebase-admin/lib/utils/error.js:88:28)
    at new FirebaseAuthError (/Users/xxx/node_modules/firebase-admin/lib/utils/error.js:146:16)
    at populateUploadAccountUser (/Users/xxx/node_modules/firebase-admin/lib/auth/user-import-builder.js:55:19)
    at /Users/xxx/node_modules/firebase-admin/lib/auth/user-import-builder.js:295:30
    at Array.forEach (<anonymous>)
    at UserImportBuilder.populateUsers (/Users/xxx/node_modules/firebase-admin/lib/auth/user-import-builder.js:293:15)
    at new UserImportBuilder (/Users/xxx/node_modules/firebase-admin/lib/auth/user-import-builder.js:121:36)
    at FirebaseAuthRequestHandler.uploadAccount (/Users/xxx/node_modules/firebase-admin/lib/auth/auth-api-request.js:557:33)
    at Auth.BaseAuth.importUsers (/Users/xxx/node_modules/firebase-admin/lib/auth/auth.js:281:40)   errorInfo:    { code: 'auth/invalid-password-hash',
     message: 'The password hash must be a valid byte buffer.' },   codePrefix: 'auth' }
like image 333
Stanislav Putilov Avatar asked Jan 17 '26 06:01

Stanislav Putilov


1 Answers

Even though the documentation states the passwordHash should be a buffer, the SDK accepts a string and not a buffer. (https://firebase.google.com/docs/auth/admin/import-users)

I was using this format, as in the docs:

{
  uid: 'uid1',
  email: '[email protected]',
  passwordHash: Buffer.from('passwordHash1'),
  passwordSalt: Buffer.from('salt1')
}

and still getting the invalid byte buffer error:

Error creating new user: { Error: The password hash must be a valid byte buffer.
    at FirebaseAuthError.FirebaseError [as constructor] (xxxx/node_modules/firebase-admin/lib/utils/error.js:42:28)
    at FirebaseAuthError.PrefixedFirebaseError [as constructor] (xxxx/node_modules/firebase-admin/lib/utils/error.js:88:28)
    at new FirebaseAuthError (xxxx/node_modules/firebase-admin/lib/utils/error.js:147:16)
    at validateCreateEditRequest (xxxx/node_modules/firebase-admin/lib/auth/auth-api-request.js:430:15)
    at xxxx/node_modules/firebase-admin/lib/auth/auth-api-request.js:601:5
    at xxxx/node_modules/firebase-admin/lib/auth/auth-api-request.js:1462:13

If you go to /node_modules/firebase-admin/lib/auth/auth-api-request.js:430:15, you will find the following code:

    // passwordHash has to be a base64 encoded string.
    if (typeof request.passwordHash !== 'undefined' &&
        !validator.isString(request.passwordHash)) {
        throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_PASSWORD_HASH);
    }

The error message hasn't been changed though:

    AuthClientErrorCode.INVALID_PASSWORD_HASH = {
        code: 'invalid-password-hash',
        message: 'The password hash must be a valid byte buffer.',
    };
like image 56
HzJavier Avatar answered Jan 19 '26 18:01

HzJavier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!