modulo de autenticacion
This commit is contained in:
@@ -0,0 +1,167 @@
|
||||
<?php
|
||||
session_start();
|
||||
include("db.php");
|
||||
|
||||
$mensaje = "";
|
||||
$tipo_mensaje = "";
|
||||
$paso = 1;
|
||||
|
||||
if (isset($_SESSION['codigo_verificado'])) {
|
||||
$paso = 3;
|
||||
} elseif (isset($_SESSION['email_recuperacion'])) {
|
||||
$paso = 2;
|
||||
}
|
||||
|
||||
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
||||
|
||||
if (isset($_POST['btn_paso1'])) {
|
||||
$email = $_POST['email'];
|
||||
$stmt = $conexion->prepare("SELECT id_usuario FROM usuarios WHERE email = ?");
|
||||
$stmt->bind_param("s", $email);
|
||||
$stmt->execute();
|
||||
|
||||
if ($stmt->get_result()->num_rows > 0) {
|
||||
$codigo = rand(100000, 999999);
|
||||
$update = $conexion->prepare("UPDATE usuarios SET reset_codigo = ?, reset_expiracion = DATE_ADD(NOW(), INTERVAL 15 MINUTE) WHERE email = ?");
|
||||
$update->bind_param("is", $codigo, $email);
|
||||
|
||||
if ($update->execute()) {
|
||||
$apiKey = RESEND_API_KEY;
|
||||
$datos_mail = [
|
||||
"from" => "La Pecosa <onboarding@resend.dev>",
|
||||
"to" => [$email],
|
||||
"subject" => "Código de Recuperación - La Pecosa",
|
||||
"html" => "
|
||||
<div style='font-family: sans-serif; border: 1px solid #eee; padding: 20px; border-radius: 10px;'>
|
||||
<h2 style='color: #e02828;'>Hola!</h2>
|
||||
<p>Solicitaste restablecer tu contraseña en <strong>La Pecosa</strong>.</p>
|
||||
<p style='font-size: 24px; font-weight: bold; color: #333; letter-spacing: 2px;'>$codigo</p>
|
||||
<p style='color: #888; font-size: 12px;'>Este código vence en 15 minutos.</p>
|
||||
</div>"
|
||||
];
|
||||
|
||||
$ch = curl_init('https://api.resend.com/emails');
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $apiKey, 'Content-Type: application/json']);
|
||||
curl_setopt($ch, CURLOPT_POST, 1);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($datos_mail));
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
|
||||
$respuesta = curl_exec($ch);
|
||||
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
if ($http_code == 200 || $http_code == 201) {
|
||||
$_SESSION['email_recuperacion'] = $email;
|
||||
$tipo_mensaje = "exito";
|
||||
$mensaje = "¡Código enviado! Revisá tu casilla de correo.";
|
||||
$paso = 2;
|
||||
escribir_log("Recuperación: Código enviado correctamente a $email");
|
||||
} else {
|
||||
$tipo_mensaje = "error";
|
||||
$mensaje = "Error al enviar el mail.";
|
||||
escribir_log("Error mail: Falló Resend para $email (HTTP $http_code)");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
escribir_log("Alerta: Intento de recuperación para email NO registrado: $email");
|
||||
$tipo_mensaje = "exito";
|
||||
$mensaje = "Si el correo es correcto, recibirás un código.";
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($_POST['btn_paso2'])) {
|
||||
$codigo_ingresado = $_POST['codigo'];
|
||||
$email = $_SESSION['email_recuperacion'];
|
||||
$stmt = $conexion->prepare("SELECT id_usuario FROM usuarios WHERE email = ? AND reset_codigo = ? AND reset_expiracion > NOW()");
|
||||
$stmt->bind_param("si", $email, $codigo_ingresado);
|
||||
$stmt->execute();
|
||||
|
||||
if ($stmt->get_result()->num_rows > 0) {
|
||||
$_SESSION['codigo_verificado'] = true;
|
||||
$paso = 3;
|
||||
escribir_log("Recuperación: Código verificado con éxito para $email");
|
||||
} else {
|
||||
$tipo_mensaje = "error";
|
||||
$mensaje = "Código incorrecto o expirado.";
|
||||
escribir_log("Alerta: Código INCORRECTO ingresado para $email");
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($_POST['btn_paso3'])) {
|
||||
$pass = $_POST['password'];
|
||||
$confirm = $_POST['confirm_password'];
|
||||
$email = $_SESSION['email_recuperacion'];
|
||||
|
||||
if ($pass === $confirm) {
|
||||
$password_hash = password_hash($pass, PASSWORD_BCRYPT);
|
||||
$update = $conexion->prepare("UPDATE usuarios SET password = ?, reset_codigo = NULL, reset_expiracion = NULL WHERE email = ?");
|
||||
$update->bind_param("ss", $password_hash, $email);
|
||||
|
||||
if ($update->execute()) {
|
||||
escribir_log("Recuperación exitosa: Contraseña actualizada para $email");
|
||||
session_destroy();
|
||||
header("Location: login.php?mensaje=actualizada");
|
||||
exit();
|
||||
}
|
||||
} else {
|
||||
$tipo_mensaje = "error";
|
||||
$mensaje = "Las contraseñas no coinciden.";
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html lang="es">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Recuperar Contraseña - 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; background-color: #fffef8; display: flex; justify-content: center; align-items: center; height: 100vh; }
|
||||
.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.5rem; margin-bottom: 20px; text-transform: uppercase; letter-spacing: 1px; }
|
||||
input { display: block; margin: 15px auto; padding: 15px; width: 100%; border: none; background-color: #f8f9fa; border-radius: 10px; font-size: 1rem; }
|
||||
button { background: #e02828; color: white; border: none; padding: 15px; width: 100%; cursor: pointer; border-radius: 50px; font-weight: 700; margin-top: 10px; transition: 0.3s; }
|
||||
.msg { padding: 12px; border-radius: 10px; margin-bottom: 20px; font-size: 14px; font-weight: 600; }
|
||||
.msg-error { background-color: #ffeaea; color: #e02828; }
|
||||
.msg-exito { background-color: #eaffea; color: #28a745; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<div class="card">
|
||||
<?php if ($mensaje): ?>
|
||||
<div class="msg msg-<?php echo $tipo_mensaje; ?>"><?php echo $mensaje; ?></div>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if ($paso == 1): ?>
|
||||
<h2>Olvidé mi clave</h2>
|
||||
<form method="POST">
|
||||
<input type="email" name="email" placeholder="Tu email" required>
|
||||
<button type="submit" name="btn_paso1">Enviar código</button>
|
||||
</form>
|
||||
|
||||
<?php elseif ($paso == 2): ?>
|
||||
<h2>Verificar Código</h2>
|
||||
<p style="font-size: 14px; color: #666; margin-bottom: 15px;">Ingresá el código enviado a tu mail.</p>
|
||||
<form method="POST">
|
||||
<input type="number" name="codigo" placeholder="000000" required>
|
||||
<button type="submit" name="btn_paso2">Verificar</button>
|
||||
</form>
|
||||
|
||||
<?php elseif ($paso == 3): ?>
|
||||
<h2>Nueva Contraseña</h2>
|
||||
<form method="POST">
|
||||
<input type="password" name="password" placeholder="Nueva contraseña" required>
|
||||
<input type="password" name="confirm_password" placeholder="Confirmar contraseña" required>
|
||||
<button type="submit" name="btn_paso3">Cambiar Contraseña</button>
|
||||
</form>
|
||||
<?php endif; ?>
|
||||
|
||||
<a href="login.php" style="display:block; margin-top:20px; font-size:14px; color:#e02828; text-decoration:none;">Volver al inicio</a>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user