Skip to content

Instantly share code, notes, and snippets.

@quietcricket
Created February 6, 2020 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quietcricket/2923f449f1ae4748f7c991b7a9652cea to your computer and use it in GitHub Desktop.
Save quietcricket/2923f449f1ae4748f7c991b7a9652cea to your computer and use it in GitHub Desktop.
Add user to firebase's authentication using email and password. Please note that this is not firebase admin script to add user to work on a firebase project
import hashlib
import hmac
import base64
import secrets
import json
import os
print("Create a user account with email and the password is randomly generated.")
email = input("Email: ")
password = secrets.token_urlsafe(8)
secret = secrets.token_urlsafe(16)
hash_key = base64.b64encode(bytes(secret, 'utf-8')).decode('ascii')
password_hash = base64.b64encode(hmac.new(bytes(secret, 'utf-8'), bytes(password, 'utf-8'), digestmod=hashlib.sha256).digest())
data = {'users': [{'localId': email, 'email': email, 'passwordHash': password_hash.decode('ascii')}]}
TEMP_FILENAME = '.temp_user.json'
fh = open(TEMP_FILENAME, 'w')
fh.write(json.dumps(data))
fh.close()
os.system('firebase auth:import %s --hash-algo=HMAC_SHA256 --hash-key=%s' % (TEMP_FILENAME, hash_key))
os.unlink(TEMP_FILENAME)
print("----- Account created successfully -----")
print("Email: %s, Password: %s" % (email, password))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment