login.html 7.56 KB
<!DOCTYPE html>
<html lang="es">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Inicie Sesión - Ecosistema SIGEM</title>

  <!-- Google Font: Source Sans Pro (Estandar de AdminLTE) -->
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700&display=fallback">
  <!-- Font Awesome -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
  <!-- Theme style AdminLTE 3 -->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/admin-lte@3.2/dist/css/adminlte.min.css">
  <style>
      .login-page {
          background: linear-gradient(135deg, #e0eafc 0%, #cfdef3 100%);
      }
      .card-primary.card-outline {
          border-top: 3px solid #0056b3;
      }
      .btn-primary {
          background-color: #0056b3;
          border-color: #0056b3;
      }
      .btn-primary:hover {
          background-color: #004494;
          border-color: #004494;
      }
      .login-logo a {
          color: #333 !important;
      }
  </style>
</head>
<body class="hold-transition login-page">
<div class="login-box">
  <div class="login-logo">
    <a href="#"><b>SIGEM</b>WEB</a>
  </div>
  <!-- /.login-logo -->
  <div class="card card-outline card-primary shadow-lg">
    <div class="card-body login-card-body rounded">
      <p class="login-box-msg font-weight-bold" style="color: #555;">Visor Georreferenciado Multi-Tenant</p>

      <form id="loginForm">
        <label for="municipioSearch" class="text-xs text-muted mb-1" style="font-size: 0.8rem; text-transform: uppercase;">1. Entidad (Municipalidad)</label>
        <div class="input-group mb-2">
          <input type="text" id="municipioSearch" class="form-control" placeholder="Buscar municipio (ej. 505)" autocomplete="off">
          <div class="input-group-append">
            <div class="input-group-text">
              <span class="fas fa-search"></span>
            </div>
          </div>
        </div>
        <div class="form-group mb-3">
          <select id="entidad" class="form-control" required size="3" style="height: 85px; cursor: pointer; font-size: 0.9rem;">
                <option value="505">Entidad 505 (Piloto GIS)</option>
                <option value="800">Entidad 800 (Pruebas)</option>
                <option value="900">Entidad 900 (Desarrollo)</option>
          </select>
        </div>

        <label class="text-xs text-muted mb-1" style="font-size: 0.8rem; text-transform: uppercase;">2. Credenciales</label>
        <div class="input-group mb-3">
          <input type="text" id="username" class="form-control" placeholder="Usu_alias (ej. operador)" required autocomplete="username">
          <div class="input-group-append">
            <div class="input-group-text">
              <span class="fas fa-user"></span>
            </div>
          </div>
        </div>
        <div class="input-group mb-3">
          <input type="password" id="password" class="form-control" placeholder="Tu clave alfanumérica" required autocomplete="current-password">
          <div class="input-group-append">
            <div class="input-group-text">
              <span class="fas fa-lock"></span>
            </div>
          </div>
        </div>

        <div id="error-msg" class="alert alert-danger p-2 mb-3 text-center" style="display:none; font-size: 13px;"></div>

        <div class="row mt-4">
          <div class="col-12">
            <button type="submit" class="btn btn-primary btn-block font-weight-bold" id="btn-login" style="padding: 10px;">
               <span id="btn-text">INICIAR SESIÓN</span>
               <div class="spinner-border spinner-border-sm text-light" id="spinner" role="status" style="display:none; margin: auto;"></div>
            </button>
          </div>
        </div>
      </form>
    </div>
    <!-- /.login-card-body -->
  </div>
</div>
<!-- /.login-box -->

<!-- jQuery -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<!-- Bootstrap 4 -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>
<!-- AdminLTE App -->
<script src="https://cdn.jsdelivr.net/npm/admin-lte@3.2/dist/js/adminlte.min.js"></script>

<script>
    // Filtro interactivo de municipios
    const searchInput = document.getElementById('municipioSearch');
    const selectEntidad = document.getElementById('entidad');
    const originalOptions = Array.from(selectEntidad.options);

    searchInput.addEventListener('input', (e) => {
        const text = e.target.value.toLowerCase();
        selectEntidad.innerHTML = '';
        originalOptions.forEach(opt => {
            if (opt.text.toLowerCase().includes(text) || opt.value.includes(text)) {
                selectEntidad.appendChild(opt);
            }
        });
        if (selectEntidad.options.length > 0) {
            selectEntidad.options[0].selected = true;
        }
    });

    // Limpiar sesión previa si por error caen al login
    localStorage.removeItem('jwt');
    localStorage.removeItem('user_name');
    
    document.getElementById('loginForm').addEventListener('submit', function(e) {
        e.preventDefault();
        
        const username = document.getElementById('username').value;
        const password = document.getElementById('password').value;
        const entidad = document.getElementById('entidad').value;
        const errorMsg = document.getElementById('error-msg');
        const btnText = document.getElementById('btn-text');
        const spinner = document.getElementById('spinner');
        
        // Bloquear UI
        errorMsg.style.display = 'none';
        btnText.style.display = 'none';
        spinner.style.display = 'block';
        document.getElementById('btn-login').disabled = true;

        const payload = {
            username: username,
            password: password,
            entidad: entidad
        };

        fetch('/gis-geoserver/api/auth/login', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify(payload)
        })
        .then(response => {
            if (!response.ok) {
                return response.json().then(errData => {
                    throw new Error(errData.message || "Credenciales incorrectas.");
                });
            }
            return response.json();
        })
        .then(data => {
            // LOGIN EXITOOSO!
            localStorage.setItem('jwt', data.token);     
            localStorage.setItem('user_name', data.nombre); // Nombre completo del usuario
            localStorage.setItem('entidad', entidad); 
            
            // Persistir metadatos del Visor
            localStorage.setItem('map_lat', data.lat);
            localStorage.setItem('map_lng', data.lng);
            localStorage.setItem('map_zoom', data.zoom);
            localStorage.setItem('map_min_zoom', data.minZoom);
            localStorage.setItem('map_max_zoom', data.maxZoom);
            localStorage.setItem('mapa_base_id', data.mapaBase);
            localStorage.setItem('map_bounds', data.bounds);
            
            // Redirigir a la URL del Mapas
            window.location.href = "/gis-geoserver/landing";
        })
        .catch(error => {
            // LOGIN ERROR
            errorMsg.innerText = error.message;
            errorMsg.style.display = 'block';
            
            // Restaurar UI
            btnText.style.display = 'block';
            spinner.style.display = 'none';
            document.getElementById('btn-login').disabled = false;
        });
    });
</script>
</body>
</html>
GitLab Appliance - Powered by TurnKey Linux