password recuperation scripts
Browse files- app/main.py +43 -5
app/main.py
CHANGED
|
@@ -18,6 +18,9 @@ import telebot
|
|
| 18 |
from telebot import types
|
| 19 |
import markdown
|
| 20 |
from dotenv import load_dotenv
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
# Cargar variables de entorno desde .env
|
| 23 |
load_dotenv()
|
|
@@ -415,6 +418,34 @@ def logout():
|
|
| 415 |
flash("Has cerrado sesión", "blue")
|
| 416 |
return redirect(url_for('index'))
|
| 417 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 418 |
@app.route('/forgot-password', methods=['GET', 'POST'])
|
| 419 |
def forgot_password():
|
| 420 |
if current_user.is_authenticated:
|
|
@@ -426,13 +457,20 @@ def forgot_password():
|
|
| 426 |
|
| 427 |
if token:
|
| 428 |
reset_url = url_for('reset_password', token=token, _external=True)
|
| 429 |
-
# Simulación de envío de correo
|
| 430 |
-
print(f"\n[EMAIL SIMULATION] Para: {email}")
|
| 431 |
-
print(f"[EMAIL SIMULATION] Enlace de recuperación: {reset_url}\n")
|
| 432 |
|
| 433 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 434 |
else:
|
| 435 |
-
# Por seguridad, no
|
| 436 |
flash("Si el correo está registrado, recibirás un enlace de recuperación.", "blue")
|
| 437 |
|
| 438 |
return redirect(url_for('login'))
|
|
|
|
| 18 |
from telebot import types
|
| 19 |
import markdown
|
| 20 |
from dotenv import load_dotenv
|
| 21 |
+
import smtplib
|
| 22 |
+
from email.mime.text import MIMEText
|
| 23 |
+
from email.mime.multipart import MIMEMultipart
|
| 24 |
|
| 25 |
# Cargar variables de entorno desde .env
|
| 26 |
load_dotenv()
|
|
|
|
| 418 |
flash("Has cerrado sesión", "blue")
|
| 419 |
return redirect(url_for('index'))
|
| 420 |
|
| 421 |
+
def send_email(to_email, subject, body):
|
| 422 |
+
"""Envía un correo electrónico usando SMTP."""
|
| 423 |
+
smtp_server = os.getenv("SMTP_SERVER")
|
| 424 |
+
smtp_port = os.getenv("SMTP_PORT")
|
| 425 |
+
smtp_user = os.getenv("SMTP_USER")
|
| 426 |
+
smtp_password = os.getenv("SMTP_PASSWORD")
|
| 427 |
+
|
| 428 |
+
if not all([smtp_server, smtp_port, smtp_user, smtp_password]):
|
| 429 |
+
print("[EMAIL ERROR] Falta configuración SMTP en variables de entorno.")
|
| 430 |
+
return False
|
| 431 |
+
|
| 432 |
+
try:
|
| 433 |
+
msg = MIMEMultipart()
|
| 434 |
+
msg['From'] = smtp_user
|
| 435 |
+
msg['To'] = to_email
|
| 436 |
+
msg['Subject'] = subject
|
| 437 |
+
msg.attach(MIMEText(body, 'plain'))
|
| 438 |
+
|
| 439 |
+
server = smtplib.SMTP(smtp_server, int(smtp_port))
|
| 440 |
+
server.starttls()
|
| 441 |
+
server.login(smtp_user, smtp_password)
|
| 442 |
+
server.send_message(msg)
|
| 443 |
+
server.quit()
|
| 444 |
+
return True
|
| 445 |
+
except Exception as e:
|
| 446 |
+
print(f"[EMAIL ERROR] Error enviando correo: {e}")
|
| 447 |
+
return False
|
| 448 |
+
|
| 449 |
@app.route('/forgot-password', methods=['GET', 'POST'])
|
| 450 |
def forgot_password():
|
| 451 |
if current_user.is_authenticated:
|
|
|
|
| 457 |
|
| 458 |
if token:
|
| 459 |
reset_url = url_for('reset_password', token=token, _external=True)
|
|
|
|
|
|
|
|
|
|
| 460 |
|
| 461 |
+
# Intentar enviar correo real
|
| 462 |
+
subject = "Recuperación de Contraseña - Maker Space"
|
| 463 |
+
body = f"Hola,\n\nHemos recibido una solicitud para restablecer tu contraseña. Haz clic en el siguiente enlace para continuar:\n\n{reset_url}\n\nSi no solicitaste este cambio, puedes ignorar este correo.\n\nEste enlace caducará en 1 hora."
|
| 464 |
+
|
| 465 |
+
if send_email(email, subject, body):
|
| 466 |
+
flash("Se ha enviado un enlace de recuperación a tu correo.", "blue")
|
| 467 |
+
else:
|
| 468 |
+
# Fallback a logs si falla el envío
|
| 469 |
+
print(f"\n[EMAIL FALLBACK] Para: {email}")
|
| 470 |
+
print(f"[EMAIL FALLBACK] Enlace: {reset_url}\n")
|
| 471 |
+
flash("Hubo un problema enviando el correo. Contacta al administrador o revisa los logs.", "orange")
|
| 472 |
else:
|
| 473 |
+
# Por seguridad, no decimos si el email existe
|
| 474 |
flash("Si el correo está registrado, recibirás un enlace de recuperación.", "blue")
|
| 475 |
|
| 476 |
return redirect(url_for('login'))
|