FEAT: Added automated email bot after authentication

This commit is contained in:
white
2025-02-05 10:41:52 +00:00
parent 4b9da0e946
commit a6bbf696f4
2 changed files with 38 additions and 6 deletions

View File

@@ -3,6 +3,7 @@ from werkzeug.security import generate_password_hash, check_password_hash
from flask_cors import cross_origin from flask_cors import cross_origin
from database.database import Database from database.database import Database
from blueprints.utils import login_required, sanitizer from blueprints.utils import login_required, sanitizer
from blueprints.email import send_email
from utils.user_utils import get_user_id from utils.user_utils import get_user_id
from secrets import token_hex from secrets import token_hex
@@ -99,6 +100,7 @@ def signup():
session["username"] = username session["username"] = username
session["user_id"] = get_user_id(username) session["user_id"] = get_user_id(username)
print(f"Logged in as {username}. session: {session.get('username')}. user_id: {session.get('user_id')}", flush=True) print(f"Logged in as {username}. session: {session.get('username')}. user_id: {session.get('user_id')}", flush=True)
# send_email(username)
return jsonify({ return jsonify({
"account_created": True, "account_created": True,
@@ -114,6 +116,7 @@ def signup():
finally: finally:
db.close_connection() db.close_connection()
@auth_bp.route("/login", methods=["POST"]) @auth_bp.route("/login", methods=["POST"])

View File

@@ -1,33 +1,46 @@
from flask import Blueprint from flask import Blueprint, session
from database.database import Database
import smtplib import smtplib
from email.mime.text import MIMEText from email.mime.text import MIMEText
from os import getenv from os import getenv
from random import randrange from random import randrange
from dotenv import load_dotenv from dotenv import load_dotenv
load_dotenv() load_dotenv()
email_bp = Blueprint("email", __name__) email_bp = Blueprint("email", __name__)
def send_email(user_email) -> None: def send_email(username) -> None:
""" """
Send a verification email to the user. Send a verification email to the user.
""" """
# Setup the sender email details # Setup the sender email details
SMTP_SERVER = "smtp.gmail.com" SMTP_SERVER = "smtp.gmail.com"
SMTP_PORT = 587 SMTP_PORT = 587
SMTP_EMAIL = getenv("EMAIL") SMTP_EMAIL = getenv("EMAIL")
SMTP_PASSWORD = getenv("EMAIL_PASSWORD") SMTP_PASSWORD = getenv("EMAIL_PASSWORD")
user_email = get_user_email(username)
# Setup up the receiver details # Setup up the receiver details
login_code = randrange(100000, 1000000) login_code = randrange(100000, 1000000)
body = f"Here is your login code: {login_code}" # Make this better body = f"""
msg = MIMEText(body) <html>
<head></head>
<body>
<b>Thank you for choosing Gander</b>
<p>Your Gander login code is: {login_code}</p>
</body>
</html>
"""
msg = MIMEText(body, "html")
msg["Subject"] = "Reset Gander Login" msg["Subject"] = "Reset Gander Login"
msg["From"] = SMTP_EMAIL msg["From"] = SMTP_EMAIL
msg["To"] = user_email msg["To"] = user_email
# Send the email using smtplib # Send the email using smtplib
with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as smtp: with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as smtp:
try: try:
@@ -40,4 +53,20 @@ def send_email(user_email) -> None:
print("Server timed out") print("Server timed out")
except Exception as e: except Exception as e:
print("Error: ", e) print("Error: ", e)
def get_user_email(username):
"""
Get the users email address.
"""
db = Database()
db.create_connection()
user_email = db.fetchone("""SELECT email
FROM users
WHERE username = ?;""",
(username,))
email = user_email["email"]
db.close_connection()
return email