Friday 22 October 2021

How to import firebase accounts with the same password?

I'm using Firebase authentication to manage my users accounts.

Now, I need to change the uid of the users, then I'm trying to delete the user and import it again with the same password using python.

I'm trying to follow the documentation. But I might be missing something.

So, in the Firebase authentication page, I'm going to menu (in the right upper corner) and getting the base64_signer_key and base64_salt_separator values.

And trying to use the code below to delete the user, import the user and update the other fields:

    for user in auth.list_users().iterate_all():
        if user.email == 'myname@yahoo.com':

            newId = CPF(cpf()).rawValue
            oldId = user.uid

            print('User: {}'.format(user._data))

            # Delete the user
            auth.delete_user(oldId)

            # Recreate the user
            users = [
                auth.ImportUserRecord(
                    uid=newId,
                    email=user.email,
                    password_hash=user.password_hash.encode('utf-8'),
                    password_salt=None
                ),
            ]

            hash_alg = auth.UserImportHash.scrypt(
                key=base64.b64decode(base64_signer_key),
                salt_separator=base64.b64decode(base64_salt_separator),
                rounds=8,
                memory_cost=14
            )       
            try:
                result = auth.import_users(users, hash_alg=hash_alg)
                print('Successfully imported {0} users. Failed to import {1} users.'.format(
                    result.success_count, result.failure_count))
                for err in result.errors:
                    print('Failed to import {0} due to {1}'.format(users[err.index].uid, err.reason))
            except Exception as e:
                print(e)

            # Update user
            auth.update_user(
                newId,
                phone_number=user.phone_number,
                email_verified=user.email_verified,
                display_name=user.display_name,
                disabled=user.disabled
            )

I'm following this documentation https://firebase.google.com/docs/auth/admin/import-users#import_users_with_firebase_scrypt_hashed_passwords

I'm able to delete and recreate the user, but when I try to login with the same user/password I'm getting FirebaseInvalidPasswordError.

What should I do recreate the user with same password and be able to authenticate in the standard way ?



from How to import firebase accounts with the same password?

No comments:

Post a Comment