Venus / src /services /auth_service.py
MetiMiester's picture
Update src/services/auth_service.py
f4cb7b4 verified
raw
history blame contribute delete
956 Bytes
from passlib.context import CryptContext
from src.db.connection import get_conn
PWD_CTX = CryptContext(
schemes=["bcrypt"],
deprecated="auto",
)
def authenticate_user(db_path: str, username: str, password: str):
if not username or not password:
return None
conn = get_conn(db_path)
try:
row = conn.execute(
"""
SELECT username, role, password_hash, is_active
FROM users
WHERE username=?
""",
(username,),
).fetchone()
if not row or not row["is_active"]:
return None
if not PWD_CTX.verify(password, row["password_hash"]):
return None
return {
"username": row["username"],
"role": row["role"],
}
finally:
conn.close()
def is_admin(auth_state) -> bool:
if not auth_state:
return False
return auth_state.get("role") == "admin"