Update modules/auth/auth.py
Browse files- modules/auth/auth.py +15 -11
modules/auth/auth.py
CHANGED
@@ -14,7 +14,9 @@ from ..database.sql_db import (
|
|
14 |
record_login,
|
15 |
record_logout
|
16 |
)
|
|
|
17 |
import logging
|
|
|
18 |
from datetime import datetime, timezone
|
19 |
|
20 |
logger = logging.getLogger(__name__)
|
@@ -39,12 +41,10 @@ if not endpoint or not key:
|
|
39 |
|
40 |
key = clean_and_validate_key(key)
|
41 |
|
|
|
42 |
def authenticate_user(username, password):
|
43 |
-
"""
|
44 |
-
Autentica un usuario y registra el inicio de sesi贸n
|
45 |
-
"""
|
46 |
try:
|
47 |
-
# Primero intentar obtener usuario general
|
48 |
user_item = get_user(username)
|
49 |
|
50 |
if not user_item:
|
@@ -54,16 +54,18 @@ def authenticate_user(username, password):
|
|
54 |
if verify_password(user_item['password'], password):
|
55 |
logger.info(f"Usuario autenticado: {username}, Rol: {user_item['role']}")
|
56 |
|
57 |
-
# Registrar la sesi贸n
|
58 |
try:
|
59 |
session_id = record_login(username)
|
60 |
if session_id:
|
61 |
st.session_state.session_id = session_id
|
|
|
62 |
st.session_state.login_time = datetime.now(timezone.utc).isoformat()
|
|
|
|
|
|
|
63 |
except Exception as e:
|
64 |
logger.error(f"Error al registrar inicio de sesi贸n: {str(e)}")
|
65 |
-
|
66 |
-
|
67 |
return True, user_item['role']
|
68 |
|
69 |
logger.warning(f"Contrase帽a incorrecta para usuario: {username}")
|
@@ -150,16 +152,18 @@ def logout():
|
|
150 |
"""Cierra la sesi贸n del usuario"""
|
151 |
try:
|
152 |
if 'session_id' in st.session_state and 'username' in st.session_state:
|
153 |
-
record_logout(
|
154 |
st.session_state.username,
|
155 |
st.session_state.session_id
|
156 |
)
|
157 |
-
|
158 |
-
|
159 |
-
|
|
|
160 |
|
161 |
except Exception as e:
|
162 |
logger.error(f"Error en logout: {str(e)}")
|
|
|
163 |
st.session_state.clear()
|
164 |
|
165 |
def hash_password(password):
|
|
|
14 |
record_login,
|
15 |
record_logout
|
16 |
)
|
17 |
+
|
18 |
import logging
|
19 |
+
|
20 |
from datetime import datetime, timezone
|
21 |
|
22 |
logger = logging.getLogger(__name__)
|
|
|
41 |
|
42 |
key = clean_and_validate_key(key)
|
43 |
|
44 |
+
|
45 |
def authenticate_user(username, password):
|
46 |
+
"""Autentica un usuario y registra el inicio de sesi贸n"""
|
|
|
|
|
47 |
try:
|
|
|
48 |
user_item = get_user(username)
|
49 |
|
50 |
if not user_item:
|
|
|
54 |
if verify_password(user_item['password'], password):
|
55 |
logger.info(f"Usuario autenticado: {username}, Rol: {user_item['role']}")
|
56 |
|
|
|
57 |
try:
|
58 |
session_id = record_login(username)
|
59 |
if session_id:
|
60 |
st.session_state.session_id = session_id
|
61 |
+
st.session_state.username = username
|
62 |
st.session_state.login_time = datetime.now(timezone.utc).isoformat()
|
63 |
+
logger.info(f"Sesi贸n iniciada: {session_id}")
|
64 |
+
else:
|
65 |
+
logger.warning("No se pudo registrar la sesi贸n")
|
66 |
except Exception as e:
|
67 |
logger.error(f"Error al registrar inicio de sesi贸n: {str(e)}")
|
68 |
+
|
|
|
69 |
return True, user_item['role']
|
70 |
|
71 |
logger.warning(f"Contrase帽a incorrecta para usuario: {username}")
|
|
|
152 |
"""Cierra la sesi贸n del usuario"""
|
153 |
try:
|
154 |
if 'session_id' in st.session_state and 'username' in st.session_state:
|
155 |
+
success = record_logout(
|
156 |
st.session_state.username,
|
157 |
st.session_state.session_id
|
158 |
)
|
159 |
+
if success:
|
160 |
+
logger.info(f"Sesi贸n cerrada: {st.session_state.username}")
|
161 |
+
else:
|
162 |
+
logger.warning(f"Error al registrar cierre de sesi贸n: {st.session_state.username}")
|
163 |
|
164 |
except Exception as e:
|
165 |
logger.error(f"Error en logout: {str(e)}")
|
166 |
+
finally:
|
167 |
st.session_state.clear()
|
168 |
|
169 |
def hash_password(password):
|