Spaces:
Sleeping
Sleeping
File size: 1,048 Bytes
883d885 |
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 |
import bcrypt
import logging
logger = logging.getLogger(__name__)
def check_login(username: str, password: str, users_collection) -> bool:
"""Checks if the provided username and password are valid against MongoDB."""
logger.info(f"Login attempt for user: {username}")
user = users_collection.find_one({"username": username})
if user:
stored_hash = user["password"]
# Ensure we always have bytes for bcrypt.checkpw
stored_hash_bytes = stored_hash.encode('utf-8') if isinstance(stored_hash, str) else stored_hash
try:
if bcrypt.checkpw(password.encode('utf-8'), stored_hash_bytes):
logger.info(f"User '{username}' logged in successfully.")
return True
else:
logger.warning(f"Invalid password attempt for user: {username}")
except Exception as e:
logger.error(f"Error checking password for user {username}: {e}")
else:
logger.warning(f"Login failed, user not found: {username}")
return False |