Spaces:
Running
Running
File size: 2,115 Bytes
1ef4e10 0fb23b8 bc1cd44 0fb23b8 1ef4e10 bc1cd44 1ef4e10 bc1cd44 1ef4e10 0fb23b8 bc1cd44 1ef4e10 bc1cd44 1ef4e10 bc1cd44 1ef4e10 bc1cd44 1ef4e10 0fb23b8 1ef4e10 bc1cd44 1ef4e10 |
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
import gradio as gr
from utils.logger import Logger
from utils.database import get_db
from data.repository.annotator_repo import AnnotatorRepo
from utils.security import verify_password
log = Logger()
class AuthService:
"""
Authenticate users against DB and drive Gradio UI states.
"""
# --------------- LOGIN --------------- #
@staticmethod
def login(username: str, password: str, session: dict):
log.info(f"Login attempt: username={username}")
with get_db() as db:
repo = AnnotatorRepo(db)
user = repo.get_user_by_username(username)
if user is None or not user.is_active:
log.warning(
f"Failed login for username='{username}' (not found / inactive)."
)
return (
"β Wrong username or password!",
gr.update(),
gr.update(visible=False),
gr.update(value=""),
)
if not verify_password(password, user.password):
log.warning(f"Failed login; bad password for '{username}'.")
return (
"β Wrong username or password!",
gr.update(),
gr.update(visible=False),
gr.update(value=""),
)
session["user_id"] = user.id
session["username"] = user.name
log.info(f"User '{username}' logged in successfully.")
return (
None,
gr.update(visible=False),
gr.update(visible=True),
gr.update(value=f"π Welcome, {user.name}!"),
)
# --------------- LOGOUT --------------- #
@staticmethod
def logout(session: dict):
username = session.get("username", "unknown")
session.clear()
log.info(f"User '{username}' logged out.")
return (
gr.update(visible=True),
gr.update(visible=False),
gr.update(value=""),
gr.update(value=""),
)
|