Skip to content

Commit

Permalink
fix: Fix bugs in CAPTCHA and sign up
Browse files Browse the repository at this point in the history
  • Loading branch information
drorganvidez committed Sep 12, 2024
1 parent 992aa12 commit c2ca4c5
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
18 changes: 14 additions & 4 deletions app/modules/auth/routes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from flask import flash, render_template, redirect, url_for, request
from flask_login import current_user, login_user, logout_user
from pymysql import IntegrityError

from app.modules.auth import auth_bp
from app.modules.auth.decorators import guest_required
Expand All @@ -8,6 +9,8 @@
from app.modules.profile.services import UserProfileService
from app.modules.captcha.services import CaptchaService

from app import db

authentication_service = AuthenticationService()
user_profile_service = UserProfileService()
captcha_service = CaptchaService()
Expand All @@ -20,22 +23,29 @@ def show_signup_form():

form = SignupForm()
if form.validate_on_submit():

user_input = request.form['captcha']
if not captcha_service.validate_captcha(user_input):
flash('Please complete the reCAPTCHA', 'danger')
return render_template('auth/signup_form.html', form=form)

email = form.email.data
if not authentication_service.is_email_available(email):
return render_template("auth/signup_form.html", form=form, error=f'Email {email} in use')
flash(f'Email {email} is already in use', 'danger')
return render_template("auth/signup_form.html", form=form)

try:
# Intentamos crear el usuario
user = authentication_service.create_with_profile(**form.data)
authentication_service.send_confirmation_email(user.email)
flash("Please confirm your email", "info")
except Exception as exc:
return render_template("auth/signup_form.html", form=form, error=f'Error creating user: {exc}')
except IntegrityError as exc:
# Manejar el caso de duplicado en la base de datos
db.session.rollback() # Hacer rollback para limpiar la sesión
if 'Duplicate entry' in str(exc):
flash(f'Email {email} is already in use', 'danger')
else:
flash(f'Error creating user: {exc}', 'danger')
return render_template("auth/signup_form.html", form=form)

return redirect(url_for("public.index"))

Expand Down
9 changes: 7 additions & 2 deletions app/modules/auth/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,16 @@ def get_token_from_email(self, email):

def send_confirmation_email(self, user_email):
token = self.get_token_from_email(user_email)
url = url_for("auth.confirm_user", token=token)
url = url_for("auth.confirm_user", token=token, _external=True)

# Usamos UTF-8 para el contenido HTML
html_body = f"<a href='{url}'>Please confirm your email</a>"

mail_service.send_email(
"Please confirm your email",
recipients=[user_email],
body=f"<a href='{url}'>Please confirm your email</a>",
body="Please confirm your email by clicking the link below.",
html_body=html_body
)

def confirm_user_with_token(self, token):
Expand Down
1 change: 1 addition & 0 deletions app/modules/auth/templates/auth/signup_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,5 @@ <h5 class="card-title">Anti-bot filter</h5>

{% block scripts %}
<script src="{{ url_for('auth.scripts') }}"></script>
<script src="{{ url_for('captcha.scripts') }}"></script>
{% endblock %}
4 changes: 2 additions & 2 deletions app/modules/captcha/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def __init__(self):
self.image_captcha = ImageCaptcha()

def generate_captcha_text(self, length=6) -> str:
letters = string.ascii_uppercase + string.digits
return ''.join(random.choice(letters) for _ in range(length))
allowed_characters = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789'
return ''.join(random.choice(allowed_characters) for _ in range(length))

def generate_captcha(self):
captcha_text = self.generate_captcha_text()
Expand Down
8 changes: 6 additions & 2 deletions app/modules/mail/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ def init_app(self, app):
app.config['MAIL_USE_TLS'] = os.getenv('MAIL_USE_TLS', 'True') == 'True'
app.config['MAIL_USE_SSL'] = os.getenv('MAIL_USE_SSL', 'False') == 'True'
app.config['MAIL_USERNAME'] = os.getenv('MAIL_USERNAME', 'tu_correo@tudominio.com')
app.config['MAIL_PASSWORD'] = os.getenv('MAIL_PASSWORD', 'tu_contraseña')
app.config['MAIL_PASSWORD'] = os.getenv('MAIL_PASSWORD', 'tu_password')
app.config['MAIL_DEFAULT_SENDER'] = os.getenv('MAIL_USERNAME')

self.mail = Mail(app)
self.sender = app.config['MAIL_USERNAME']

def send_email(self, subject, recipients, body):
def send_email(self, subject, recipients, body, html_body=None):
msg = Message(subject, sender=self.sender, recipients=recipients)

msg.body = body
if html_body:
msg.html = html_body

self.mail.send(msg)

0 comments on commit c2ca4c5

Please sign in to comment.