Unable to login after importing user into Firebase Authentication

We are migrating users into Firebase Auth from legacy db who have their passwords hashed with SHA512.

We are able to successfully import the users into Firebase. However, when we try to login again with those users through Firebase Authentication, it gives an error:

Invalid password or this user has no password

Following are the details of code structure, the command used to import users, and the user.json that we are using.

Password:test
Salt: salt

Password+salt hashed in SHA512:

6c838e934e3feefae6cfa53af11375d4954f85c6f5ed888c02cd7806a71696d1cb449f2be78e9e6ea301a95c81f28ad8766f3ae582f9beaac33c7dc2b7ba9187

Base-64 encoded password+salt:

NmM4MzhlOTM0ZTNmZWVmYWU2Y2ZhNTNhZjExMzc1ZDQ5NTRmODVjNmY1ZWQ4ODhjMDJjZDc4MDZhNzE2OTZkMWNiNDQ5ZjJiZTc4ZTllNmVhMzAxYTk1YzgxZjI4YWQ4NzY2ZjNhZTU4MmY5YmVhYWMzM2M3ZGMyYjdiYTkxODc=

Base-64 encoded salt:

c2FsdA==

User.json:

{
  "users": [
    {
      "localId": "123",
      "emailVerified": true,
      "email": "test@demo.com",
      "passwordHash": "NmM4MzhlOTM0ZTNmZWVmYWU2Y2ZhNTNhZjExMzc1ZDQ5NTRmODVjNmY1ZWQ4ODhjMDJjZDc4MDZhNzE2OTZkMWNiNDQ5ZjJiZTc4ZTllNmVhMzAxYTk1YzgxZjI4YWQ4NzY2ZjNhZTU4MmY5YmVhYWMzM2M3ZGMyYjdiYTkxODc=",
      "salt": "c2FsdA=="
    }
  ]
}

Firebase command for importing user.json:

firebase --project=projectname auth:import user.json --hash-algo=SHA512 --rounds=80

Pleas let us know what we are missing here

Thank You

25 thoughts on “Unable to login after importing user into Firebase Authentication”

  1. You need to add the hash key and salt separator. You can find these details here.

    Console > Project > Authentication > Password hash paremeter
    

    The import command would be something like this.

    firebase auth:import dev-accounts.json --hash-algo=scrypt --rounds=8 --hash-key="your_hash_key" --salt-separator="your_salt_separator" --mem-cost=14
    
    Reply
  2. I used SHA256, got the same issue, and solved it finally using the following Python code.
    Hope these codes are helpful.

    • create password hash and salt
      password_hash_base_64 = base64.standard_b64encode(hashlib.sha256("123456salt").digest())
      salt_base64 = base64.standard_b64encode("salt")
      print password_hash_base_64
      print salt_base64
    
    • import_account_sha256.json
    {
      "users": [
        {
          "localId": "166666",
          "email": "166666@qq.com",
          "emailVerified": false,
          "passwordHash": "5NL5SaQBwE6c0L1BDjHW+BtBOXQVH8RYwY0tGGw3khk=",
          "createdAt": "1613525311156",
          "salt": "c2FsdA==",
          "providerUserInfo": []
        }
      ]
    }
    
    • Firebase CLI
    firebase auth:import import_account_sha256.json --hash-algo=SHA256 --rounds=1 --hash-input-order=PASSWORD_FIRST
    
    • example
      enter image description here
    Reply

Leave a Comment