60 lines
1.7 KiB
Python
60 lines
1.7 KiB
Python
from tinydb import TinyDB, where, Query
|
|
import uuid
|
|
import hasher
|
|
|
|
db = TinyDB("./database.json", indent=4)
|
|
query = Query()
|
|
|
|
users = db.table("users");
|
|
|
|
def get_users():
|
|
return (users.all())
|
|
|
|
def get_user_by_email(email: str):
|
|
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)
|
|
|
|
def user_exist(email: str):
|
|
return (get_user_by_email(email) != None)
|
|
|
|
def add_user(email: str, password: str):
|
|
password_hashed = hasher.hash_text(password)
|
|
users.insert({"email": email, "password": str(password_hashed), "id": uuid.uuid4()});
|
|
|
|
def check_password(email: str, password: str):
|
|
password_hashed = get_user_by_email(email).get("password")
|
|
password_hashed = bytes(password_hashed[2:-1], "utf-8")
|
|
return (hasher.is_same(password, password_hashed))
|
|
|
|
def change_user_password(email: str, password: str):
|
|
password_hashed = hasher.hash_text(password)
|
|
db.update({"password": password_hashed}, query.email == email)
|
|
|
|
resets = db.table("resets")
|
|
|
|
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)
|