aboutsummaryrefslogtreecommitdiff
path: root/scripts/users/handle_users.py
blob: 87ff7cdfebaaa87764e9b502dfc4c1885cb39085 (plain)
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
from scripts.database.mongo import mongo_database 
mongo_collection = mongo_database["users"]

def user_exists(user):
    try:
        res = mongo_collection.find_one({"name": user}, {"password": 1})
        if res is None:
            return {"success": True, "user": None}
        else:
            return {"success": True, "user": user, "password": res["password"]}
    except:
        return {"success": False, "error": "Request error, try again"}
    
def authenticate_user(user, password_matches):
    try:
        res = mongo_collection.find_one({"name": user})
        if res is None:
            return {"success": False, "error": "Password doesn't match, try again"}
        else:
            return  {"success": True, "user": user}
    except:
        return {"success": False, "error": "Something went wrong matching your password"}
    
def add_user(user, password):
    try:
        res = mongo_collection.insert_one({"name": user, "password": password})
        return {"success": True, "user": user}
    except:
        print("error")