-
Notifications
You must be signed in to change notification settings - Fork 0
/
firebase_modules.py
136 lines (99 loc) · 2.95 KB
/
firebase_modules.py
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# Importing required libraries
import pyrebase
from firebase import firebase
from dotenv import load_dotenv
import os
# Loading enviroment variables
load_dotenv()
# Configuration Key Of firebase
firebaseConfig = {"apiKey": os.environ.get("FIREBASE_APIKEY"),
"authDomain": os.environ.get("FIREBASE_AUTHDOMAIN"),
"databaseURL": os.environ.get("FIREBASE_DATABASEURL"),
"projectId": os.environ.get("FIREBASE_PROJECTID"),
"storageBucket": os.environ.get("FIREBASE_STORAGEBUCKET"),
"messagingSenderId": os.environ.get("FIREBASE_messagingSenderId"),
"appId": os.environ.get("FIREBASE_appId"),
"measurementId": os.environ.get("FIREBASE_measurementId")}
# Init of Pyrebase
pirebase = pyrebase.initialize_app(firebaseConfig)
auth = pirebase.auth()
# Init of Firebase
firebase = firebase.FirebaseApplication(os.environ.get("FIREBASE_URL"), None)
def firebase_user_registerer(name, email, password):
"""Gets and register a new user data to firebase
Parameters
----------
name : str
The name of user
email : str
The email of user
password : str
The password given by user
Returns
-------
None
"""
user = auth.create_user_with_email_and_password(email, password)
Data = {
'Name' : name,
'Email' : email,
}
result = firebase.post('/Users:', Data)
def firebase_user_checker(email):
"""Checks if a email is already registered or not
Parameters
----------
email : str
The email of user
Returns
-------
bool
True / Flase if user is already registered or not
"""
data_list = firebase.get('/',None)
flag = 0
try:
for data in data_list['Users:']:
if data_list['Users:'][data]['Email'] == str(email):
flag = 1
break
return flag
except:
return 0
def firebase_user_loger(email, password):
"""Logs in a user if email amd password are correct
Parameters
----------
email : str
The email of user
password : str
The password given by user
Returns
-------
bool
True / Flase if email amd password are correct or not
"""
try:
login = auth.sign_in_with_email_and_password(email, password)
return 1
except:
return 0
def firebase_data_fetcher(email):
"""Gets User data as per given email
Parameters
----------
email : str
The email of user
Returns
-------
dict
A dictionary containing user data - name,email.
"""
data_list = firebase.get('/',None)
user_data = {}
for data in data_list['Users:']:
if data_list['Users:'][data]['Email'] == str(email):
user_data['name'] = data_list['Users:'][data]['Name']
user_data['email'] = data_list['Users:'][data]['Email']
break
return user_data