#/modules/auth/auth.py import bcrypt from modules.database.sql_db import get_user, create_user import gradio as gr def authenticate_user(username, password): """Autentica a un usuario.""" user = get_user(username) if user and bcrypt.checkpw(password.encode('utf-8'), user['password'].encode('utf-8')): return True, user['role'] return False, None def register_user(username, password, role): """Registra un nuevo usuario.""" hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8') return create_user(username, hashed_password, role) def create_auth_interface(): """Crea la interfaz de autenticación.""" with gr.Blocks() as auth_interface: gr.Markdown("# Login") username = gr.Textbox(label="Usuario") password = gr.Textbox(label="Contraseña", type="password") login_btn = gr.Button("Iniciar Sesión") message = gr.Markdown() def handle_login(user, pwd): success, role = authenticate_user(user, pwd) return f"Bienvenido, {user} ({role})" if success else "Credenciales incorrectas." login_btn.click(fn=handle_login, inputs=[username, password], outputs=message) return auth_interface