Current File : /var/www/maausk-app/public/index.php
<?php
session_start();

ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

// traer conexión PDO desde db.php
require_once __DIR__ . '/db.php';

// si ya había sesión, redirigir al dashboard
if (isset($_SESSION['user_email'])) {
    header('Location: /dashboard.php');
    exit;
}

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = trim($_POST['email'] ?? '');
    $pass  = $_POST['password'] ?? '';

    // obtenemos nombre, email y hash de la BD
    $stmt = $pdo->prepare(
        'SELECT nombre, email, password_hash FROM users WHERE email = ?'
    );
    $stmt->execute([$email]);
    $user = $stmt->fetch();

    // verificamos la contraseña
    if ($user && password_verify($pass, $user['password_hash'])) {
        $_SESSION['user_name']  = $user['nombre'];
        $_SESSION['user_email'] = $user['email'];
        header('Location: /dashboard.php');
        exit;
    }

    $error = 'Credenciales incorrectas';
}
?>
<!doctype html>
<html lang="es">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Pharmacius | Bienvenido</title>
  <!-- Bootstrap CSS -->
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <!-- Estilos personalizados para botón y acentos -->
  <style>
    .btn-primary {
      background-color: #033e86;
      border-color: #033e86;
      color: #fff;
    }
    .btn-primary:hover,
    .btn-primary:focus {
      background-color: #022e6a;
      border-color: #022e6a;
      color: #fff;
    }
    .accent {
      color: #033e86;
    }
  </style>
</head>
<body class="d-flex flex-column justify-content-center align-items-center" style="min-height:100vh">
  <div class="container text-center mb-4">
    <h1 class="display-4 fw-bold">Bienvenido a <span class="accent">Pharmacius</span></h1>
    <p class="lead">Gestiona tus productos, inventario y pedidos de forma ágil.</p>
  </div>

  <div class="card shadow-sm" style="width:100%; max-width:400px;">
    <div class="card-header text-center bg-light">
      <h2 class="h5 mb-0">Iniciar sesión</h2>
    </div>
    <div class="card-body">
      <?php if ($error): ?>
        <div class="alert alert-danger py-2"><?= htmlspecialchars($error) ?></div>
      <?php endif; ?>

      <form method="post" autocomplete="off">
        <div class="mb-3 text-start">
          <label class="form-label" for="email">Correo electrónico</label>
          <input class="form-control" type="email" id="email" name="email" required>
        </div>
        <div class="mb-3 text-start">
          <label class="form-label" for="password">Contraseña</label>
          <input class="form-control" type="password" id="password" name="password" required>
        </div>
        <button class="btn btn-primary w-100" type="submit">Entrar</button>
      </form>
    </div>
  </div>

  <!-- Bootstrap Bundle JS -->
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>