├── .gitignore ├── README.md ├── create_user.py ├── delete_user.py ├── initialise_firebase_admin.py ├── requirements.txt ├── send_email_verification_link.py ├── set_password.py ├── sign_in_with_email_and_password.py └── update_user.py /.gitignore: -------------------------------------------------------------------------------- 1 | venv 2 | .idea 3 | __pycache__ 4 | python-admin-sdk-* 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # python-firebase-admin-sdk-demo 2 | 3 | For the complete walkthrough, please visit this [tutorial](https://medium.com/@billydharmawan/user-management-with-firebase-and-python-749a7a87b2b6?source=friends_link&sk=7260aeac1295cc9b921f6e04f4839d66) and [this](https://medium.com/@billydharmawan/verify-user-email-address-with-firebase-and-python-adda433ff48d?source=friends_link&sk=86198e8020406ca7b2e06b1f6238b2f9) for email verification link part. 4 | 5 | This repo contains Python scripts that allow you to do the following with Firebase: 6 | * create user 7 | * update user details 8 | * set user password 9 | * authenticate user with email and password 10 | * delete user 11 | * send email verification link 12 | -------------------------------------------------------------------------------- /create_user.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | from typing import Optional 3 | 4 | from firebase_admin import auth 5 | from firebase_admin.auth import UserRecord 6 | 7 | from initialise_firebase_admin import app 8 | 9 | 10 | def get_email_arg(): 11 | parser = argparse.ArgumentParser(description="Create a new user in Firebase") 12 | parser.add_argument("--email", required=True, help="The email address of the new user to be created") 13 | parser.add_argument("--user-id", required=False, 14 | help="The user id to assign to the new user. If this is not specified, " 15 | "Firebase will generate a random string.") 16 | 17 | return parser.parse_args() 18 | 19 | 20 | def create_user(email: str, user_id: Optional[str]) -> UserRecord: 21 | return auth.create_user(email=email, uid=user_id) if user_id else auth.create_user(email=email) 22 | 23 | 24 | if __name__ == "__main__": 25 | arg = get_email_arg() 26 | new_user: UserRecord = create_user(arg.email, arg.user_id) 27 | 28 | print(f"Firebase successfully created a new user with email - {new_user.email} and user id - {new_user.uid}") 29 | -------------------------------------------------------------------------------- /delete_user.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | 3 | from firebase_admin import auth 4 | 5 | from initialise_firebase_admin import app 6 | 7 | 8 | def get_user_id_arg(): 9 | parser = argparse.ArgumentParser(description="Delete user in Firebase") 10 | parser.add_argument("--user-id", required=True, help="The user id of the user to be deleted.") 11 | 12 | return parser.parse_args() 13 | 14 | 15 | def delete_user(user_id: str): 16 | return auth.delete_user(user_id) 17 | 18 | 19 | if __name__ == "__main__": 20 | args = get_user_id_arg() 21 | 22 | user = delete_user(args.user_id) 23 | print(f"Firebase has deleted user with user id - {args.user_id}") 24 | -------------------------------------------------------------------------------- /initialise_firebase_admin.py: -------------------------------------------------------------------------------- 1 | import firebase_admin 2 | from firebase_admin import App 3 | 4 | app: App = firebase_admin.initialize_app() 5 | 6 | if __name__ == "__main__": 7 | print(app) 8 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | firebase-admin==4.0.0 2 | 3 | -------------------------------------------------------------------------------- /send_email_verification_link.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import json 3 | import os 4 | from pprint import pprint 5 | 6 | import requests 7 | 8 | FIREBASE_WEB_API_KEY = os.environ.get("FIREBASE_WEB_API_KEY") 9 | rest_api_url = "https://identitytoolkit.googleapis.com/v1/accounts:sendOobCode" 10 | 11 | 12 | def get_id_token_arg(): 13 | parser = argparse.ArgumentParser(description="Send email verification link to user") 14 | 15 | parser.add_argument("--firebase-id-token", required=True, help="The Firebase ID token of the user to verify.") 16 | 17 | return parser.parse_args() 18 | 19 | 20 | def send_email_verification_link(id_token: str): 21 | payload = json.dumps({ 22 | "requestType": "VERIFY_EMAIL", 23 | "idToken": id_token 24 | }) 25 | 26 | r = requests.post(rest_api_url, 27 | params={"key": FIREBASE_WEB_API_KEY}, 28 | data=payload) 29 | 30 | return r.json() 31 | 32 | 33 | if __name__ == "__main__": 34 | arg = get_id_token_arg() 35 | email_address_to_verify = send_email_verification_link(arg.firebase_id_token) 36 | pprint(email_address_to_verify) 37 | -------------------------------------------------------------------------------- /set_password.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | 3 | from firebase_admin import auth 4 | from firebase_admin.auth import UserRecord 5 | 6 | from initialise_firebase_admin import app 7 | 8 | 9 | def get_args(): 10 | parser = argparse.ArgumentParser(description="Set user password in Firebase") 11 | 12 | parser.add_argument("--user-id", required=True, help="The user id of the user whose password is to be set.") 13 | parser.add_argument("--password", required=False, help="The password to set for the given user id.") 14 | 15 | return parser.parse_args() 16 | 17 | 18 | def set_password(user_id: str, password: str) -> UserRecord: 19 | return auth.update_user(user_id, password=password) 20 | 21 | 22 | if __name__ == "__main__": 23 | args = get_args() 24 | 25 | user = set_password(args.user_id, args.password) 26 | print(f"Firebase has updated the password for user with user id - {user.uid}") 27 | -------------------------------------------------------------------------------- /sign_in_with_email_and_password.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import json 3 | import os 4 | import requests 5 | import pprint 6 | 7 | FIREBASE_WEB_API_KEY = os.environ.get("FIREBASE_WEB_API_KEY") 8 | rest_api_url = f"https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword" 9 | 10 | 11 | def get_args(): 12 | parser = argparse.ArgumentParser(description="Sign in a Firebase user with email and password") 13 | 14 | parser.add_argument("--email", required=True, help="The email address which the user wants to sign in with.") 15 | parser.add_argument("--password", required=True, help="The password of the user.") 16 | 17 | return parser.parse_args() 18 | 19 | 20 | def sign_in_with_email_and_password(email: str, password: str, return_secure_token: bool = True): 21 | payload = json.dumps({ 22 | "email": email, 23 | "password": password, 24 | "returnSecureToken": return_secure_token 25 | }) 26 | 27 | r = requests.post(rest_api_url, 28 | params={"key": FIREBASE_WEB_API_KEY}, 29 | data=payload) 30 | 31 | return r.json() 32 | 33 | 34 | if __name__ == "__main__": 35 | args = get_args() 36 | token = sign_in_with_email_and_password(args.email, args.password) 37 | pprint.pprint(token) 38 | -------------------------------------------------------------------------------- /update_user.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | 3 | from firebase_admin import auth 4 | from firebase_admin.auth import UserRecord 5 | 6 | from initialise_firebase_admin import app 7 | 8 | 9 | def get_args(): 10 | parser = argparse.ArgumentParser(description="Update user details in Firebase") 11 | 12 | parser.add_argument("--user-id", required=True, help="The user id of the user whose details are to be updated.") 13 | parser.add_argument("--email", required=False, help="The new email address to replace the existing email.") 14 | parser.add_argument("--mobile-number", required=False, help="The new mobile number to replace the existing number.") 15 | parser.add_argument("--display-name", required=False, help="The new display name to replace the existing name.") 16 | 17 | return parser.parse_args() 18 | 19 | 20 | def update_email(user_id: str, email: str) -> UserRecord: 21 | return auth.update_user(user_id, email=email) 22 | 23 | 24 | def update_mobile(user_id: str, mobile_no: str) -> UserRecord: 25 | return auth.update_user(user_id, phone_number=mobile_no) 26 | 27 | 28 | def update_display_name(user_id: str, display_name: str) -> UserRecord: 29 | return auth.update_user(user_id, display_name=display_name) 30 | 31 | 32 | if __name__ == "__main__": 33 | args = get_args() 34 | 35 | if args.email: 36 | updated_user = update_email(args.user_id, args.email) 37 | print(f"Updated user email to {updated_user.email}") 38 | 39 | if args.mobile_number: 40 | updated_user = update_mobile(args.user_id, args.mobile_number) 41 | print(f"Updated user mobile number to {updated_user.phone_number}") 42 | 43 | if args.display_name: 44 | updated_user = update_display_name(args.user_id, args.display_name) 45 | print(f"Updated user display name to {updated_user.display_name}") 46 | --------------------------------------------------------------------------------