modulo de autenticacion
This commit is contained in:
@@ -0,0 +1,178 @@
|
||||
<?php
|
||||
session_start();
|
||||
include("db.php");
|
||||
|
||||
$error_msg = "";
|
||||
|
||||
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
||||
$email = $_POST['email'];
|
||||
$password = $_POST['password'];
|
||||
$captcha_ingresado = $_POST['captcha'];
|
||||
|
||||
// 1. Validamos captcha
|
||||
if (strtolower($captcha_ingresado) != strtolower($_SESSION['captcha'])) {
|
||||
escribir_log("Fallo de captcha: El usuario $email ingresó mal el código."); // <--- LOG
|
||||
header("Location: login.php?error=captcha");
|
||||
exit();
|
||||
}
|
||||
|
||||
// Buscamos usuario
|
||||
$stmt = $conexion->prepare("SELECT id_usuario, nombre, password, rol FROM usuarios WHERE email = ?");
|
||||
$stmt->bind_param("s", $email);
|
||||
$stmt->execute();
|
||||
$result = $stmt->get_result();
|
||||
|
||||
if ($result->num_rows == 1) {
|
||||
$usuario = $result->fetch_assoc();
|
||||
|
||||
// Validamos contraseña hash
|
||||
if (password_verify($password, $usuario['password'])) {
|
||||
$_SESSION['id_usuario'] = $usuario['id_usuario'];
|
||||
$_SESSION['usuario'] = $usuario['nombre'];
|
||||
$_SESSION['rol'] = $usuario['rol'];
|
||||
|
||||
escribir_log("Login exitoso: " . $usuario['nombre'] . " (Rol: " . $usuario['rol'] . ")"); // <--- LOG
|
||||
|
||||
// Redirección inteligente según el Rol
|
||||
if ($_SESSION['rol'] === 'admin') {
|
||||
// Si es el dueño, directo a la cocina y estadísticas
|
||||
header("Location: admin/dashboard.php");
|
||||
} else {
|
||||
// Si es un cliente de Paraná, directo a que compre algo rico
|
||||
header("Location: index.php");
|
||||
}
|
||||
exit();
|
||||
} else {
|
||||
escribir_log("Contraseña incorrecta: Intento fallido para el email $email"); // <--- LOG
|
||||
header("Location: login.php?error=password");
|
||||
exit();
|
||||
}
|
||||
} else {
|
||||
escribir_log("Usuario inexistente: Intento de login con email no registrado: $email"); // <--- LOG
|
||||
header("Location: login.php?error=user");
|
||||
exit();
|
||||
}
|
||||
}
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="es">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Login - La Pecosa</title>
|
||||
<link rel="stylesheet" href="css/all.min.css">
|
||||
<style>
|
||||
* { margin: 0; padding: 0; box-sizing: border-box; }
|
||||
body {
|
||||
font-family: 'Segoe UI', Roboto, sans-serif;
|
||||
display: flex; justify-content: center; align-items: center;
|
||||
height: 100vh; background-color: #fffef8; margin: 0;
|
||||
}
|
||||
.login-card {
|
||||
background: white; padding: 50px 40px; border-radius: 20px;
|
||||
text-align: center; width: 100%; max-width: 400px;
|
||||
box-shadow: 0 10px 30px rgba(0,0,0,0.04);
|
||||
border: 1px solid rgba(224, 40, 40, 0.05);
|
||||
}
|
||||
h2 {
|
||||
color: #333; font-size: 1.8rem; font-weight: 800;
|
||||
text-transform: uppercase; letter-spacing: 1.5px; margin-bottom: 30px;
|
||||
}
|
||||
h2::after {
|
||||
content: ''; display: block; width: 45px; height: 4px;
|
||||
background-color: #e02828; margin: 10px auto 0; border-radius: 2px;
|
||||
}
|
||||
input {
|
||||
display: block; margin: 15px auto; padding: 15px; width: 100%;
|
||||
border: none; background-color: #f8f9fa; border-radius: 10px; font-size: 1rem;
|
||||
}
|
||||
input:focus { outline: none; background-color: #ffffff; box-shadow: 0 0 0 2px rgba(224, 40, 40, 0.1); }
|
||||
|
||||
button[type="submit"] {
|
||||
background: #e02828; color: white; border: none; padding: 15px;
|
||||
width: 100%; cursor: pointer; border-radius: 50px; font-weight: 700;
|
||||
text-transform: uppercase; margin-top: 15px; transition: 0.3s;
|
||||
}
|
||||
.captcha-box { background: #fdfdfd; padding: 20px; border-radius: 15px; border: 1px dashed #eee; margin: 20px 0; }
|
||||
.btn-reload { background: transparent; color: #888; font-size: 13px; border: 1px solid #ddd; padding: 6px 15px; border-radius: 50px; cursor: pointer; margin-bottom: 10px; }
|
||||
|
||||
#msg-error {
|
||||
background-color: #ffeaea; color: #e02828; font-size: 14px;
|
||||
padding: 10px; border-radius: 8px; margin-bottom: 20px;
|
||||
display: none; font-weight: 600;
|
||||
}
|
||||
.register-link { display: block; margin-top: 25px; font-size: 14px; color: #777; text-decoration: none; }
|
||||
.register-link strong { color: #e02828; }
|
||||
|
||||
.olvido-link {
|
||||
font-size: 13px;
|
||||
color: #888;
|
||||
text-decoration: none;
|
||||
transition: 0.3s;
|
||||
}
|
||||
.olvido-link:hover {
|
||||
color: #e02828;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<div class="login-card">
|
||||
<h2>Login</h2>
|
||||
|
||||
<div id="msg-error"></div>
|
||||
|
||||
<form action="login.php" method="POST">
|
||||
<input type="email" name="email" placeholder="Email" required autofocus>
|
||||
<input type="password" name="password" placeholder="Contraseña" required>
|
||||
|
||||
<div style="text-align: right; width: 100%; margin-top: -10px; margin-bottom: 10px;">
|
||||
<a href="recuperar_contraseña.php" class="olvido-link">¿Olvidaste tu contraseña?</a>
|
||||
</div>
|
||||
|
||||
<div class="captcha-box">
|
||||
<img src="captcha.php" id="captchaImg"><br>
|
||||
<button type="button" class="btn-reload" onclick="recargarCaptcha()">
|
||||
<i class="fa-solid fa-rotate"></i> Recargar
|
||||
</button>
|
||||
<input type="text" name="captcha" placeholder="Código" required
|
||||
style="margin-bottom: 0; background: white; border: 1px solid #eee;">
|
||||
</div>
|
||||
|
||||
<button type="submit">Entrar</button>
|
||||
</form>
|
||||
|
||||
<a href="registro.php" class="register-link">
|
||||
¿No tenés cuenta? <strong>Registrate gratis</strong>
|
||||
</a>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
function recargarCaptcha(){
|
||||
document.getElementById("captchaImg").src = "captcha.php?v=" + Date.now();
|
||||
}
|
||||
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
const errorType = urlParams.get('error');
|
||||
const mensajeType = urlParams.get('msj');
|
||||
const errorDiv = document.getElementById('msg-error');
|
||||
|
||||
// Si hay un error de login (captcha, clave, etc)
|
||||
if (errorType) {
|
||||
errorDiv.style.display = 'block';
|
||||
if (errorType === 'captcha') errorDiv.innerText = "¡Código Captcha incorrecto!";
|
||||
if (errorType === 'password') errorDiv.innerText = "Email o contraseña incorrectos.";
|
||||
if (errorType === 'user') errorDiv.innerText = "El usuario no existe.";
|
||||
}
|
||||
|
||||
// Si viene del carrito por no estar logueado
|
||||
if (mensajeType === 'debes_iniciar_sesion') {
|
||||
errorDiv.style.display = 'block';
|
||||
errorDiv.style.backgroundColor = "#fff4e5";
|
||||
errorDiv.style.color = "#856404";
|
||||
errorDiv.style.border = "1px solid #ffeeba";
|
||||
errorDiv.innerText = "⚠️ ¡Hola! Para comprar en La Pecosa primero debés iniciar sesión.";
|
||||
}
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user