Skip to content

Commit c2ca4c5

Browse files
committed
fix: Fix bugs in CAPTCHA and sign up
1 parent 992aa12 commit c2ca4c5

File tree

5 files changed

+30
-10
lines changed

5 files changed

+30
-10
lines changed

app/modules/auth/routes.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from flask import flash, render_template, redirect, url_for, request
22
from flask_login import current_user, login_user, logout_user
3+
from pymysql import IntegrityError
34

45
from app.modules.auth import auth_bp
56
from app.modules.auth.decorators import guest_required
@@ -8,6 +9,8 @@
89
from app.modules.profile.services import UserProfileService
910
from app.modules.captcha.services import CaptchaService
1011

12+
from app import db
13+
1114
authentication_service = AuthenticationService()
1215
user_profile_service = UserProfileService()
1316
captcha_service = CaptchaService()
@@ -20,22 +23,29 @@ def show_signup_form():
2023

2124
form = SignupForm()
2225
if form.validate_on_submit():
23-
2426
user_input = request.form['captcha']
2527
if not captcha_service.validate_captcha(user_input):
2628
flash('Please complete the reCAPTCHA', 'danger')
2729
return render_template('auth/signup_form.html', form=form)
2830

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

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

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

app/modules/auth/services.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,16 @@ def get_token_from_email(self, email):
7777

7878
def send_confirmation_email(self, user_email):
7979
token = self.get_token_from_email(user_email)
80-
url = url_for("auth.confirm_user", token=token)
80+
url = url_for("auth.confirm_user", token=token, _external=True)
81+
82+
# Usamos UTF-8 para el contenido HTML
83+
html_body = f"<a href='{url}'>Please confirm your email</a>"
84+
8185
mail_service.send_email(
8286
"Please confirm your email",
8387
recipients=[user_email],
84-
body=f"<a href='{url}'>Please confirm your email</a>",
88+
body="Please confirm your email by clicking the link below.",
89+
html_body=html_body
8590
)
8691

8792
def confirm_user_with_token(self, token):

app/modules/auth/templates/auth/signup_form.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,5 @@ <h5 class="card-title">Anti-bot filter</h5>
160160

161161
{% block scripts %}
162162
<script src="{{ url_for('auth.scripts') }}"></script>
163+
<script src="{{ url_for('captcha.scripts') }}"></script>
163164
{% endblock %}

app/modules/captcha/services.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ def __init__(self):
1313
self.image_captcha = ImageCaptcha()
1414

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

1919
def generate_captcha(self):
2020
captcha_text = self.generate_captcha_text()

app/modules/mail/services.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@ def init_app(self, app):
1818
app.config['MAIL_USE_TLS'] = os.getenv('MAIL_USE_TLS', 'True') == 'True'
1919
app.config['MAIL_USE_SSL'] = os.getenv('MAIL_USE_SSL', 'False') == 'True'
2020
app.config['MAIL_USERNAME'] = os.getenv('MAIL_USERNAME', 'tu_correo@tudominio.com')
21-
app.config['MAIL_PASSWORD'] = os.getenv('MAIL_PASSWORD', 'tu_contraseña')
21+
app.config['MAIL_PASSWORD'] = os.getenv('MAIL_PASSWORD', 'tu_password')
2222
app.config['MAIL_DEFAULT_SENDER'] = os.getenv('MAIL_USERNAME')
2323

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

27-
def send_email(self, subject, recipients, body):
27+
def send_email(self, subject, recipients, body, html_body=None):
2828
msg = Message(subject, sender=self.sender, recipients=recipients)
29+
2930
msg.body = body
31+
if html_body:
32+
msg.html = html_body
33+
3034
self.mail.send(msg)

0 commit comments

Comments
 (0)