-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.py
34 lines (26 loc) · 932 Bytes
/
auth.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
# auth.py
import hashlib
from db import get_users_collection
def hash_password(password):
return hashlib.sha256(password.encode()).hexdigest()
def register_user(username, email, password):
users_collection = get_users_collection()
if users_collection.find_one({"email": email}):
return "User already exists"
hashed_password = hash_password(password)
user = {
"username": username,
"email": email,
"password": hashed_password
}
users_collection.insert_one(user)
return "User registered successfully"
def login_user(email, password):
users_collection = get_users_collection()
user = users_collection.find_one({"email": email})
if not user:
return "Invalid credentials"
hashed_password = hash_password(password)
if user['password'] != hashed_password:
return "Invalid credentials"
return "Login successful"