Skip to content

Commit 11842a8

Browse files
committed
Use reCaptcha in sign-up. Set password in second step. TODO: check and send email.
1 parent 8553fed commit 11842a8

File tree

6 files changed

+56
-14
lines changed

6 files changed

+56
-14
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,6 @@ venv.bak/
105105

106106
# VIM
107107
*.swp
108+
109+
# Env
110+
.envrc

textsmith/app.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from quart.logging import default_handler
2727
from flask_babel import Babel # type: ignore
2828
from flask_babel import gettext as _ # type: ignore
29-
from flask_wtf import FlaskForm # type: ignore
29+
from flask_wtf import FlaskForm, RecaptchaField # type: ignore
3030
from wtforms import validators # type: ignore
3131
from wtforms.fields import PasswordField, BooleanField # type: ignore
3232
from wtforms.fields.html5 import EmailField # type: ignore
@@ -58,6 +58,17 @@
5858
)
5959
# i18n support.
6060
babel = Babel(app)
61+
# ReCaptcha support.
62+
app.config.update(
63+
{
64+
"RECAPTCHA_PUBLIC_KEY": os.environ.get(
65+
"RECAPTCHA_PUBLIC_KEY", "CHANGEME"
66+
),
67+
"RECAPTCHA_PRIVATE_KEY": os.environ.get(
68+
"RECAPTCHA_PRIVATE_KEY", "CHANGEME"
69+
),
70+
}
71+
)
6172

6273

6374
# ---------- WEB FORM DEFINITIONS
@@ -76,6 +87,18 @@ class SignUp(FlaskForm):
7687
],
7788
render_kw={"autofocus": True},
7889
)
90+
accept = BooleanField(
91+
_("I accept the code of conduct"),
92+
[validators.InputRequired(_("Please agree to our code of conduct."))],
93+
)
94+
recaptcha = RecaptchaField()
95+
96+
97+
class SetPassword(FlaskForm):
98+
"""
99+
Allows a user to set and confirm a new password.
100+
"""
101+
79102
password1 = PasswordField(
80103
_("Password"),
81104
[
@@ -91,10 +114,6 @@ class SignUp(FlaskForm):
91114
password2 = PasswordField(
92115
_("Confirm Password"), [validators.InputRequired(_("Required."))]
93116
)
94-
accept = BooleanField(
95-
_("I accept the code of conduct"),
96-
[validators.InputRequired(_("Please agree to our code of conduct."))],
97-
)
98117

99118

100119
class LogIn(FlaskForm):

textsmith/datastore.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,9 @@ async def set_container(self, object_id: int, container_id: int):
254254
referenced as container_id.
255255
"""
256256

257-
async def get_contents(self, object_id: int) -> Dict[
257+
async def get_contents(
258+
self, object_id: int
259+
) -> Dict[
258260
int,
259261
Dict[
260262
str,

textsmith/logic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ async def set_last_login(self, user_id):
2929
"""
3030
Set the last_login timestamp to time.now() for the referenced user.
3131
"""
32-
await self.datastore.set_last_seen(user_id)
32+
await self.datastore.set_last_seen(user_id)
3333

3434
async def check_email(self, email: str) -> bool:
3535
"""
@@ -43,4 +43,4 @@ async def create_user(self, email, password):
4343
Create a user with the referenced email and password. Email a
4444
confirmation link with instructions to the new user.
4545
"""
46-
return
46+
return

textsmith/templates/set_password.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{% extends 'base.html' %}
2+
{% block content %}
3+
<section>
4+
<p>{% trans %}Please enter and confirm your new password in the form
5+
below.{% endtrans %}</p>
6+
7+
{% if form.errors %}
8+
<p><strong>There are problems with the values you entered.</strong></p>
9+
{% endif %}
10+
<form action="{{ url_for('password') }}" method="post">
11+
{{ form.csrf_token }}
12+
<p>{{ form.password1.label }}:<br/>{{ form.password1}}
13+
{% if form.password1.errors %}<strong>{% for error in form.password1.errors %}{{ error }} {% endfor %}</strong>{% endif %}</p>
14+
<p>{{ form.password2.label }}:<br/>{{ form.password2}}</p>
15+
<p><input type="submit" value="{% trans %}Submit{% endtrans %}"></p>
16+
</form>
17+
</section>
18+
{% endblock %}

textsmith/templates/signup.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22
{% block content %}
33
<section>
44
<p>{% trans %}Please read our <a href="/conduct">code of conduct</a> and
5-
then fill in the form below to sign up.{% endtrans %}</p>
5+
then fill in the form below to sign up. We'll email you instructions for
6+
setting your password and logging in for the first time.{% endtrans %}</p>
67

78
{% if form.errors %}
89
<p><strong>There are problems with the values you entered.</strong></p>
910
{% endif %}
1011
<form action="{{ url_for('signup') }}" method="post">
1112
{{ form.csrf_token }}
12-
<p>{{ form.email.label }}:<br/>{{ form.email }}
13-
{% if form.email.errors %}<strong>{% for error in form.email.errors %}{{ error }} {% endfor %}</strong>{% endif %}</p>
14-
<p>{{ form.password1.label }}:<br/>{{ form.password1}}
15-
{% if form.password1.errors %}<strong>{% for error in form.password1.errors %}{{ error }} {% endfor %}</strong>{% endif %}</p>
16-
<p>{{ form.password2.label }}:<br/>{{ form.password2}}</p>
13+
<p>{{ form.email.label }}:<br/>{{ form.email }}</p>
14+
{% if form.email.errors %}<p><strong>({% for error in form.email.errors %}{{ error }} {% endfor %}</strong>)</p>{% endif %}
1715
<p>{{ form.accept.label }}: {{ form.accept }}</p>
16+
{{ form.recaptcha }}
17+
{% if form.recaptcha.errors %}<p>(<strong>{% for error in form.recaptcha.errors %}{{ error }} {% endfor %})</strong></p>{% endif %}
1818
<p><input type="submit" value="{% trans %}Submit{% endtrans %}"></p>
1919
</form>
2020
</section>

0 commit comments

Comments
 (0)