-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
63 lines (43 loc) · 1.5 KB
/
database.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
import pymongo
import bcrypt
# specification of database
CONNECTION_STRING = "mongodb://localhost:27017/"
CURRENT_DATABASE = "pathFinderDB"
# collection names
USERS_COLLECTION = "users"
myclient = pymongo.MongoClient(CONNECTION_STRING) # connect to the mongoDB
db = myclient[CURRENT_DATABASE] # connect to the Database
"""
Connect to the mongoDb
"""
def get_db():
myclient = pymongo.MongoClient(CONNECTION_STRING) # connect to the mongoDB
return myclient[CURRENT_DATABASE] # connect to the Database
"""
Encrypt Password
"""
def password_hashing(password):
return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
"""
Store Registraion Data into the database
"""
def store_register_data(form_data):
columns = ['firstName', 'lastName', 'dateOfBitrh', 'role', 'email', 'password']
# bcrypt.checkpw(password.encode('utf-8'), user['password'])
form_data[-1] = password_hashing(form_data[-1]) # encrypt password
insert_data = dict(zip(columns, form_data))
print(insert_data)
db_collection = db[USERS_COLLECTION]
db_collection.insert_one(insert_data)
"""
Check Login Credential
"""
def check_login(form_data):
email = form_data[0]
password = form_data[1]
db_collection = db[USERS_COLLECTION]
db_user_data = db_collection.find_one({"email": email})
if db_user_data and bcrypt.checkpw(password.encode('utf-8'), db_user_data['password']):
return True
else:
return False