File size: 1,254 Bytes
316a6b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#/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