In this Python Firebase SDK we learn about Working with Python Firebase Authentication.
so using Firebase Authentication you can create and manage users account in Firebase
Project. before this we had two articles on using Firebase SDK with Python Programming
Language, you can check the articles in the below links.
Python Firebase SDK Tutorials
1: Firebase SDK Integration with Real Time Database
2: Firebase SDK working with Cloud Firestore
You can check Firebase Admin SDK features that are available for different programming
languages in this link, Firebase Admin SDK Features .
Also you can watch the complete video for Python Firebase SDK Working with Authentication.
Setup Firebase Project & Service Account
To use the Firebase Admin SDKs, you’ll need the following:
- A Firebase project
- A service account to communicate with Firebase
- A configuration file with your service account’s credentials
OK so after creation of your project in Firebase Console, you need to enable Email and Password
from Authentication section of your project.
after that for integration of Firebase SDK you need to install the SDK for the language of your
choice. because we are using Python, so we do the process for python programming language.
you can easily install Firebase Admin SDK with pip like this.
1 |
pip install firebase-admin |
To authenticate a service account and authorize it to access Firebase services, you must
generate a private key file in JSON format.
- In the Firebase console, open Settings > Service Accounts.
- Click Generate New Private Key, then confirm by clicking Generate Key.
- Securely store the JSON file containing the key.
Admin SDK Integration
So before working with the Firebase Authentication, we need to authenticated our application with
our Firebase Project . after creating of our JSON file from Service Account, copy the file and add
that to your working directory.
1 2 3 4 5 6 7 8 9 10 |
import firebase_admin from firebase_admin import credentials from firebase_admin import auth #service account credentials cred = credentials.Certificate('firebase-sdk.json') #initializing the app firebase_admin.initialize_app(cred) |
So in the above code this is for fetching the service account key JSON file. and make sure
that you have downloaded the key and added that to your working directory.
1 |
cred = credentials.Certificate('firebase-sdk.json') |
So this code is for creating users in your Firebase Project. we can use create_user() function
from auth class. in here we have just given email, password and phone number as arguments
for creating user, but you can add more parameters, for example you can add display name or
you can add a profile pic.
1 2 3 4 5 6 7 8 |
#creating the user email = input('Please enter your email address : ') password = input("Please enter your password : ") user = auth.create_user(email = email, password = password, phone_number = '+93748943493') print("User created successfully : {0}".format(user.uid)) |
In this code snippet we are going to get the user according to email and also phone number.
1 2 3 4 5 6 7 8 9 10 |
#get the user email = 'youremail@gmail.com' phone = "+93748943493" user = auth.get_user_by_email(email) by_phone = auth.get_user_by_phone_number(phone) print("User id is : {0} ".format(user.uid)) print("User id is : {0} ".format(by_phone.uid)) |
For example you have a lot of users in your Firebase Project, now if you want to list these
users you can do like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#list all the users page = auth.list_users() while page: for user in page.users: print("User : " + user.uid) # get next page page = page.get_next_page() |
Sometimes you need to create the user by your own id , so for that you can use this code.
1 2 3 4 5 6 7 8 9 10 11 |
#create user by id email = input('Please enter your email address : ') password = input("Please enter your password : ") id = input("Please enter the id : ") user = auth.create_user(uid = id, email = email, password = password) print("Successfully created new user : {0} ".format(user.uid)) |
This code is used for resetting of user password, it generates a link for resetting of password
in Firebase Project. you can use generate_password_reset_link() function for this. you can
pass some parameters for this, the first one the email address that we want to reset the
password and the second one is the action_code_settings. so the ActionCodeSettings
instance (optional). Defines whether the link is to be handled by a mobile app and the
additional state information to be passed in the deep link.
1 2 3 4 5 6 7 8 |
#creating password reset link email = input("Please enter your email address : ") link = auth.generate_password_reset_link(email, action_code_settings=None) print(link) |
In this code snippet we can create email verification link.
1 2 3 4 5 6 7 8 |
#creating email verification link email = input("Please enter your email address : ") link = auth.generate_email_verification_link(email, action_code_settings=None) print(link) |
So now this is the complete source code for this article.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
import firebase_admin from firebase_admin import credentials from firebase_admin import auth #creating service account key cred = credentials.Certificate('firebase-sdk.json') #initializing the app firebase_admin.initialize_app(cred) #creating the user email = input('Please enter your email address : ') password = input("Please enter your password : ") user = auth.create_user(email = email, password = password, phone_number = '+93748943493') print("User created successfully : {0}".format(user.uid)) #get the user ''' email = 'youremail@gmail.com' phone = "+93748943493" user = auth.get_user_by_email(email) by_phone = auth.get_user_by_phone_number(phone) print("User id is : {0} ".format(user.uid)) print("User id is : {0} ".format(by_phone.uid)) ''' #list all the users ''' page = auth.list_users() while page: for user in page.users: print("User : " + user.uid) # get next page page = page.get_next_page() ''' #create user by id ''' email = input('Please enter your email address : ') password = input("Please enter your password : ") id = input("Please enter the id : ") user = auth.create_user(uid = id, email = email, password = password) print("Successfully created new user : {0} ".format(user.uid)) ''' #creating password reset link ''' email = input("Please enter your email address : ") link = auth.generate_password_reset_link(email, action_code_settings=None) print(link) ''' #creating email verification link ''' email = input("Please enter your email address : ") link = auth.generate_email_verification_link(email, action_code_settings=None) print(link) ''' |
Subscribe and Get Free Video Courses & Articles in your Email
hey thanks for the your service, can you help me on posting photos,videos,music, for making an ecommerce app