2023-02-12 06:29:18 -05:00
|
|
|
from tinydb import TinyDB, where, Query
|
|
|
|
import uuid
|
2023-02-11 10:06:36 -05:00
|
|
|
import hasher
|
2023-02-11 08:37:10 -05:00
|
|
|
|
2023-02-12 06:29:18 -05:00
|
|
|
db = TinyDB("./database.json", indent=4)
|
|
|
|
query = Query()
|
|
|
|
|
2023-02-11 08:37:10 -05:00
|
|
|
users = db.table("users");
|
|
|
|
|
|
|
|
def get_users():
|
|
|
|
return (users.all())
|
|
|
|
|
|
|
|
def get_user_by_email(email: str):
|
2023-02-12 06:29:18 -05:00
|
|
|
user_lst = users.search(query.email == email)
|
|
|
|
if (user_lst == []):
|
|
|
|
return (None)
|
|
|
|
return (user_lst[0])
|
|
|
|
|
|
|
|
def email_exist(email: str) -> bool:
|
|
|
|
return (get_user_by_email(email) != None)
|
2023-02-11 08:37:10 -05:00
|
|
|
|
|
|
|
def user_exist(email: str):
|
|
|
|
return (get_user_by_email(email) != None)
|
|
|
|
|
|
|
|
def add_user(email: str, password: str):
|
2023-02-11 10:06:36 -05:00
|
|
|
password_hashed = hasher.hash_text(password)
|
2023-02-12 15:55:04 -05:00
|
|
|
users.insert({"email": email, "password": str(password_hashed), "id": uuid.uuid4()});
|
2023-02-11 08:37:10 -05:00
|
|
|
|
|
|
|
def check_password(email: str, password: str):
|
2023-02-12 06:29:18 -05:00
|
|
|
password_hashed = get_user_by_email(email).get("password")
|
2023-02-11 10:06:36 -05:00
|
|
|
password_hashed = bytes(password_hashed[2:-1], "utf-8")
|
|
|
|
return (hasher.is_same(password, password_hashed))
|
|
|
|
|
2023-02-12 06:29:18 -05:00
|
|
|
def change_user_password(email: str, password: str):
|
|
|
|
password_hashed = hasher.hash_text(password)
|
|
|
|
db.update({"password": password_hashed}, query.email == email)
|
|
|
|
|
2023-02-11 10:06:36 -05:00
|
|
|
resets = db.table("resets")
|
2023-02-12 06:29:18 -05:00
|
|
|
|
|
|
|
def get_email_by_reset_code(code: str):
|
|
|
|
user_lst = resets.search(query.code == code)
|
|
|
|
if (user_lst == []):
|
|
|
|
return (None)
|
|
|
|
return (user_lst[0])
|
|
|
|
|
|
|
|
def reset_code_exist(code: str) -> bool:
|
|
|
|
return (get_email_by_reset_code(code) != None)
|
|
|
|
|
|
|
|
def remove_reset_code_by_email(email: str):
|
|
|
|
resets.remove(query.email == email)
|
|
|
|
|
|
|
|
def remove_reset_code_by_code(code: str):
|
|
|
|
resets.remove(query.code == code)
|
|
|
|
|
|
|
|
def create_reset_code_by_email(email: str):
|
|
|
|
code = str(uuid.uuid4());
|
|
|
|
remove_reset_code_by_email(email);
|
|
|
|
resets.insert({"email": email, "code": code})
|
|
|
|
return (code)
|