Skip to content

Commit c7a2630

Browse files
committed
Add captcha to user login page
Signed-off-by: Keshav Priyadarshi <git@keshav.space>
1 parent 720c48d commit c7a2630

3 files changed

Lines changed: 21 additions & 2 deletions

File tree

fedcode/forms.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#
99

1010
from django import forms
11+
from django.contrib.auth.forms import AuthenticationForm
1112
from django.contrib.auth.forms import UserCreationForm
1213
from django.contrib.auth.models import User
1314
from django_recaptcha.fields import ReCaptchaField
@@ -146,3 +147,12 @@ class SearchRepositoryForm(forms.Form):
146147
},
147148
),
148149
)
150+
151+
152+
class UserLoginForm(AuthenticationForm):
153+
captcha = ReCaptchaField(
154+
error_messages={
155+
"required": ("Captcha is required"),
156+
},
157+
widget=ReCaptchaV2Checkbox,
158+
)

fedcode/templates/login.html

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
{% if form.errors %}
1111
<div class="notification is-danger">
1212
<button class="delete"></button>
13-
<strong>Error!</strong> Your username and password didn't match. Please try again.
13+
<strong>Error!</strong>
14+
{% for error in form.errors.values %}
15+
{{ error }}
16+
{% endfor %}
1417
</div>
1518
{% endif %}
1619

@@ -32,7 +35,11 @@ <h2 class="title">User Login</h2>
3235
autocomplete="current-password" required id="id_password">
3336
</div>
3437
</div>
35-
38+
<div class="field">
39+
<div class="control">
40+
{{ form.captcha }}
41+
</div>
42+
</div>
3643
<div class="field">
3744
<div class="control">
3845
<input class="button is-fullwidth is-info" type="submit" value="Log in">

fedcode/views.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
from fedcode.forms import SearchRepositoryForm
5858
from fedcode.forms import SearchReviewForm
5959
from fedcode.forms import SubscribePackageForm
60+
from fedcode.forms import UserLoginForm
6061
from fedcode.models import Follow
6162
from fedcode.models import Note
6263
from fedcode.models import Package
@@ -277,6 +278,7 @@ def post(self, request, repository_id):
277278
class UserLogin(LoginView):
278279
template_name = "login.html"
279280
next_page = "/"
281+
form_class = UserLoginForm
280282

281283
def dispatch(self, request, *args, **kwargs):
282284
# If user is already logged in, redirect to the next_page.

0 commit comments

Comments
 (0)