Spaces:
Sleeping
Sleeping
Download src/auth/authenticator.py from NextAI-cl/waterpolice-test: direct link, hf CLI and curl.
- Browser
- Download file 2.6 kB
-
https://huggingface.co/spaces/NextAI-cl/waterpolice-test/resolve/main/src/auth/authenticator.py
- Command line
-
hf download hf://spaces/NextAI-cl/waterpolice-test/src/auth/authenticator.py
-
curl -L -o authenticator.py https://huggingface.co/spaces/NextAI-cl/waterpolice-test/resolve/main/src/auth/authenticator.py
2.6 kB
| """Authentication module for Water Police application.""" | |
| import streamlit as st | |
| import streamlit_authenticator as stauth | |
| import yaml | |
| from pathlib import Path | |
| CONFIG_FILE = Path(__file__).parent.parent / "config.yaml" | |
| def load_config(): | |
| """Carga la configuraci贸n desde config.yaml""" | |
| with open(CONFIG_FILE, encoding="utf-8") as file: | |
| return yaml.safe_load(file) | |
| def save_config(config): | |
| """Guarda la configuraci贸n en config.yaml""" | |
| with open(CONFIG_FILE, "w", encoding="utf-8") as file: | |
| yaml.dump(config, file, default_flow_style=False, allow_unicode=True) | |
| def setup_authenticator(): | |
| """Configura y retorna el autenticador.""" | |
| config = load_config() | |
| # Hashear contrase帽as si est谩n en texto plano (primera ejecuci贸n) | |
| stauth.Hasher.hash_passwords(config["credentials"]) | |
| save_config(config) | |
| # Crear autenticador | |
| authenticator = stauth.Authenticate( | |
| config["credentials"], | |
| config["cookie"]["name"], | |
| config["cookie"]["key"], | |
| config["cookie"]["expiry_days"], | |
| ) | |
| return authenticator | |
| def render_login(authenticator): | |
| """Renderiza la pantalla de login con estilos CSS.""" | |
| # Estilos CSS para centrar y mejorar el dise帽o | |
| st.markdown(""" | |
| <style> | |
| [data-testid="stMainBlockContainer"] { | |
| max-width: 550px; | |
| margin: 0 auto; | |
| padding-top: 80px; | |
| } | |
| .login-header { | |
| text-align: center; | |
| padding: 30px 0 40px 0; | |
| } | |
| .login-header h1 { | |
| color: #1E88E5; | |
| font-size: 2.5rem; | |
| margin-bottom: 15px; | |
| } | |
| .login-header p { | |
| color: #666; | |
| font-size: 1.1rem; | |
| } | |
| .login-icon { | |
| font-size: 5rem; | |
| margin-bottom: 15px; | |
| } | |
| [data-testid="stForm"] { | |
| padding: 30px 20px; | |
| } | |
| </style> | |
| """, unsafe_allow_html=True) | |
| # Header con branding | |
| st.markdown(""" | |
| <div class="login-header"> | |
| <div class="login-icon">馃挧</div> | |
| <h1>Polic铆a del Agua</h1> | |
| <p>Sistema de Predicci贸n de Riesgo H铆drico</p> | |
| </div> | |
| """, unsafe_allow_html=True) | |
| # Formulario de login | |
| authenticator.login( | |
| fields={ | |
| "Form name": "Iniciar Sesi贸n", | |
| "Username": "Usuario", | |
| "Password": "Contrase帽a", | |
| "Login": "Entrar", | |
| } | |
| ) | |
| # Verificar estado despu茅s del intento de login | |
| authentication_status = st.session_state.get("authentication_status") | |
| if authentication_status is False: | |
| st.error("Usuario o contrase帽a incorrectos") | |
| return authentication_status | |