Create auth/auth.py
Browse files- modules/auth/auth.py +33 -0
modules/auth/auth.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#/modules/auth/auth.py
|
2 |
+
|
3 |
+
import bcrypt
|
4 |
+
from modules.database.sql_db import get_user, create_user
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
def authenticate_user(username, password):
|
8 |
+
"""Autentica a un usuario."""
|
9 |
+
user = get_user(username)
|
10 |
+
if user and bcrypt.checkpw(password.encode('utf-8'), user['password'].encode('utf-8')):
|
11 |
+
return True, user['role']
|
12 |
+
return False, None
|
13 |
+
|
14 |
+
def register_user(username, password, role):
|
15 |
+
"""Registra un nuevo usuario."""
|
16 |
+
hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
|
17 |
+
return create_user(username, hashed_password, role)
|
18 |
+
|
19 |
+
def create_auth_interface():
|
20 |
+
"""Crea la interfaz de autenticaci贸n."""
|
21 |
+
with gr.Blocks() as auth_interface:
|
22 |
+
gr.Markdown("# Login")
|
23 |
+
username = gr.Textbox(label="Usuario")
|
24 |
+
password = gr.Textbox(label="Contrase帽a", type="password")
|
25 |
+
login_btn = gr.Button("Iniciar Sesi贸n")
|
26 |
+
message = gr.Markdown()
|
27 |
+
|
28 |
+
def handle_login(user, pwd):
|
29 |
+
success, role = authenticate_user(user, pwd)
|
30 |
+
return f"Bienvenido, {user} ({role})" if success else "Credenciales incorrectas."
|
31 |
+
|
32 |
+
login_btn.click(fn=handle_login, inputs=[username, password], outputs=message)
|
33 |
+
return auth_interface
|