g1a / modules /auth /auth.py
AIdeaText's picture
Create auth/auth.py
316a6b2 verified
raw
history blame
1.25 kB
#/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