File size: 2,601 Bytes
279a849
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""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