Skip to content

Commit fdbdde2

Browse files
Firebase Management Scipts (avinashkranjan#935)
1 parent 176d9c3 commit fdbdde2

File tree

7 files changed

+267
-0
lines changed

7 files changed

+267
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import os
2+
3+
from dotenv import load_dotenv
4+
5+
dotenv_path = "./credentials.env"
6+
load_dotenv(dotenv_path)
7+
8+
firebaseConfig = {
9+
"apiKey": "",
10+
"authDomain": "",
11+
"databaseURL": "",
12+
"projectId": "",
13+
"storageBucket": "",
14+
"messagingSenderId": "",
15+
"appId": "",
16+
"measurementId": ""
17+
}
18+
19+
20+
def get_fireBase_credentials():
21+
try:
22+
23+
# Accessing variables.
24+
25+
apiKey = os.getenv('apiKey')
26+
authDomain = os.getenv('authDomain')
27+
databaseURL = os.getenv('databaseURL')
28+
projectId = os.getenv('projectId')
29+
storageBucket = os.getenv('storageBucket')
30+
messagingSenderId = os.getenv('messagingSenderId')
31+
appId = os.getenv('appId')
32+
measurementId = os.getenv('measurementId')
33+
credentials = [apiKey, authDomain, databaseURL, projectId, storageBucket, messagingSenderId, appId,
34+
measurementId]
35+
if any(not(credential) for credential in credentials):
36+
raise ValueError("Value cannot be None ")
37+
else:
38+
firebaseConfig["apiKey"] = apiKey
39+
firebaseConfig["authDomain"] = authDomain
40+
firebaseConfig["databaseURL"] = databaseURL
41+
firebaseConfig["projectId"] = projectId
42+
firebaseConfig["storageBucket"] = storageBucket
43+
firebaseConfig["messagingSenderId"] = messagingSenderId
44+
firebaseConfig["appId"] = appId
45+
firebaseConfig["measurementId"] = measurementId
46+
47+
except:
48+
print("error while getting the Keys ")
49+
raise
50+
51+
return firebaseConfig
52+
53+
get_fireBase_credentials()

FirebaseScripts/Readme.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
## Script for Firebase Management for beginners
2+
3+
This python script can be used to access or manage different features of firebase database by google.
4+
5+
This provides features like :
6+
7+
### Authentication
8+
9+
1. Create new User
10+
2. Sign In Used based on email or password
11+
3. Authorize a new uer
12+
4. email verification
13+
5. reset password
14+
15+
### Database :
16+
17+
1. Create your Real Time Database
18+
2. Retrieve Data from the Database
19+
3. update Data
20+
4. Delete Data
21+
22+
Note :
23+
24+
Firebase Real time database is an Non SQL based Database.
25+
26+
### Storage :
27+
28+
1. Retrieve Data
29+
2. Send data
30+
3. Download Data
31+
4. Update / Delete Data
32+
33+
Note :
34+
35+
firebase Storage is used to store all the files/ non text based data in the Database.
36+
37+
## Installation
38+
39+
First of all install [python]("https://www.python.org/downloads/") on your system.
40+
```
41+
pip install pyrebase4
42+
pip install firebase
43+
```
44+
## Steps to Get your Firebase Credentials
45+
46+
1. Go to [FireBase Console]("https://console.firebase.google.com/")
47+
2. Sign in / Login with your Google Account or any other Account you prefer
48+
3. after that in your window the option to create a new Project will be there go and create a new project
49+
4. after that fill in the basic details like project name, etc
50+
5. after that just go and click on add an App icon on the screen
51+
6. in that select web app as the option
52+
7. after that you will see some credentials that have been allocated by the Firebase Cloud service
53+
8. just copy-paste them accordingly in credentials.env and Do
54+
55+
## Learn more
56+
57+
Learn more about firebase and its services [firebase]("https://firebase.google.com/")
58+
59+
Learn more about pyrebase a wrapper framwork for firebase API's [pyrebase]("https://openbase.com/python/Pyrebase")
60+
61+
### Made with ❤️ by Shantam Sultania
62+
63+
You can find me at:-
64+
[Linkedin](https://www.linkedin.com/in/shantam-sultania-737084175/) or [Github](https://github.com/shantamsultania) .
65+
66+
Happy coding ❤️ .

FirebaseScripts/credentials.env

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
apiKey = ""
3+
authDomain = ""
4+
databaseURL = ""
5+
projectId = ""
6+
storageBucket = ""
7+
messagingSenderId = ""
8+
appId = ""
9+
measurementId = ""
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import pyrebase
2+
3+
import FirebaseScripts.CredentialsHelper as credentials
4+
5+
firebase = pyrebase.initialize_app(credentials.get_fireBase_credentials())
6+
7+
auth = firebase.auth()
8+
9+
10+
def create_user_with_token():
11+
token = auth.create_custom_token("enter the token here ")
12+
print(token)
13+
return token
14+
15+
16+
def create_user_with_email(email, password):
17+
user = auth.create_user_with_email_and_password(email, password)
18+
print(user)
19+
return user
20+
21+
22+
def sign_in_user_with_email(email, password):
23+
user = auth.sign_in_with_email_and_password(email=email, password=password)
24+
print(user)
25+
return user
26+
27+
28+
def signIn_user_with_token(token):
29+
user = auth.sign_in_with_custom_token(token)
30+
print(user)
31+
return user
32+
33+
34+
def email_verifications(user):
35+
verification = auth.send_email_verification(user["idToken"])
36+
print(verification)
37+
return verification
38+
39+
40+
def password_reset(email):
41+
password_rest = auth.send_password_reset_email(email)
42+
print(password_rest)
43+
return password_rest
44+
45+
46+
def get_user_account_info(user):
47+
info = auth.get_account_info(user["idToken"])
48+
print(info)
49+
return info
50+
51+
52+
if __name__ == "__main__":
53+
create_user_with_email("email here ", "password here ")
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import pyrebase
2+
3+
import FirebaseScripts.CredentialsHelper as credentials
4+
5+
firebase = pyrebase.initialize_app(credentials.get_fireBase_credentials())
6+
7+
database = firebase.database()
8+
9+
10+
def create_data_path():
11+
response = database.child("testdata")
12+
print(response)
13+
return response
14+
15+
16+
def add_data(data):
17+
response = database.child("testdata").push(data)
18+
print(response)
19+
return response
20+
21+
22+
def set_key_of_data(data):
23+
response = database.child("testdata").child("newKey").set(data)
24+
print(response)
25+
return response
26+
27+
28+
def update_data(updateData):
29+
response = database.child("testdata").child("newKey").update(updateData)
30+
print(response)
31+
return response
32+
33+
34+
def delete_data(key):
35+
response = database.child("testdata").child(key).remove()
36+
print(response)
37+
return response
38+
39+
40+
def retrieve_data():
41+
response = database.child("testdata").get()
42+
print("data " + str(response))
43+
print("Keys : " + str(response.key()))
44+
print("Values : " + str(response.val()))
45+
return response
46+
47+
48+
def print_all_data():
49+
for i in retrieve_data().each():
50+
print(i.key())
51+
print(i.val())
52+
53+
54+
if __name__ == "__main__":
55+
print_all_data()
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import pyrebase
2+
3+
import FirebaseScripts.CredentialsHelper as credentials
4+
5+
firebase = pyrebase.initialize_app(credentials.get_fireBase_credentials())
6+
7+
storage = firebase.storage()
8+
9+
10+
def send_file(firebase_path, source_path):
11+
response = storage.child(firebase_path).put(source_path)
12+
print(response)
13+
return response
14+
15+
16+
def delete_file(firebase_path, name_of_file_to_delete):
17+
response = storage.child(firebase_path).delete(name_of_file_to_delete)
18+
print(response)
19+
return response
20+
21+
22+
def download_file(firebase_path, file_name):
23+
response = storage.child(firebase_path).download(path="./", filename=file_name)
24+
print(response)
25+
return response
26+
27+
28+
if __name__ == "__main__":
29+
download_file("enter your path here ", "enter your file name here ")

FirebaseScripts/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pyrebase4==4.4.3
2+
firebase=3.0.1

0 commit comments

Comments
 (0)