DEV Community

Cover image for Integrating hCaptcha with Django-allauth
Rachit Khurana
Rachit Khurana

Posted on

Integrating hCaptcha with Django-allauth

hCaptcha logo

We may face issues when someone uses bots to abuse our system and send automated signup/password reset to random people. So hCaptcha is a really good way to avoid bots.

Setting Up hCaptcha account

Installing hCaptcha

We will be using django-hCaptcha package from pypi.

  • Install it using the following command
pip install django-hCaptcha
Enter fullscreen mode Exit fullscreen mode
  • Add β€œhcaptcha” to your INSTALLED_APPS setting like this:
# project/settings.py

INSTALLED_APPS = [
    ...
    'hcaptcha',
]
Enter fullscreen mode Exit fullscreen mode
  • Addsitekey and secret key which we kept earlier to your settings.py file
# project/settings.py

...
HCAPTCHA_SITEKEY = '<your sitekey>'
HCAPTCHA_SECRET = '<your secret key>'
...
Enter fullscreen mode Exit fullscreen mode

Add hCaptcha to forms

# app/forms.py

from allauth.account.forms import SignupForm, ResetPasswordForm
from hcaptcha.fields import hCaptchaField


class CustomSignupForm(SignupForm):
    hcaptcha = hCaptchaField(theme='dark')
    # if the order of fields isn't as you expected ,then you can use field_order
    #field_order = ['username', 'email', 'password1', 'password2', 'hcaptcha']
    #customize this according to your form                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

class CustomForgetPassword(ResetPasswordForm):
    hcaptcha = hCaptchaField(theme='dark')
Enter fullscreen mode Exit fullscreen mode
  • Make these as the default forms by declaring them in settings.py file
# project/settings.py
...
ACCOUNT_FORMS = {
    'signup': '<app>.forms.MyCustomSignupForm',
    'reset_password': '<app>.forms.CustomForgetPassword',}
...
Enter fullscreen mode Exit fullscreen mode

All Done πŸŽ‰

Congrats

Top comments (0)