ashrafZolkopli
Posted on June 15, 2021
Up until now we haven't created any registration/login form. We could or should roll our own for a very special business requirement or we could use a ready made and famous authentication packages call django-allauth. Django allauth will handle all your login/registration need, and include plugins for login using 3rd party Oauth such as google, facebook, twitter, github to name a few. For more 3rd party authentication that Allauth support please refer here.
Lets start will installation of Allauth
Installing Django-Allauth
Installing django Allauth with the following command in your terminal
pipenv install django-allauth
pipenv lock -r > requirement.txt
after the installation is done, We need to register django-allauth.
Settings Django-Allauth
Django-Allauth require a significant change to your settings.py file, Lets first check each part one by one ...
open your settings.py file, and first check your INSTALLED_APP, and include the following
INSTALLED_APPS = [
#...
# Make sure the following 3 is installed
'django.contrib.auth',
'django.contrib.messages',
'django.contrib.sites',
# Django-allauth
'allauth',
'allauth.account',
'allauth.socialaccount',
#...
]
then add the following in your settings.py file
AUTHENTICATION_BACKENDS = [
# Needed to login by username in Django admin, regardless of `allauth`
'django.contrib.auth.backends.ModelBackend',
# Django-allauth
# `allauth` specific authentication methods, such as login by e-mail
# https://django-allauth.readthedocs.io/en/latest/installation.html
'allauth.account.auth_backends.AuthenticationBackend',
]
SITE_ID = 1
once done, we should now update the urls.py file
Configuring urls.py
lets now open urls.py file and add the following
urlpatterns = [
#...
path('account/', include('allauth.urls')),
#...
]
Optional Configuration
The following would be optional configuration that you could add to change the behavior of django_allauth, first open your settings.py file and add the following
# Common Django Settings
LOGIN_URL = "account/login"
LOGIN_REDIRECT_URL = "/"
LOGOUT_REDIRECT_URL = "/"
# Django-allauth
# https://django-allauth.readthedocs.io/en/latest/configuration.html
ACCOUNT_AUTHENTICATION_METHOD = "username_email"
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_EMAIL_VERIFICATION = "mandatory"
ACCOUNT_SIGNUP_EMAIL_ENTER_TWICE = True
ACCOUNT_SIGNUP_PASSWORD_ENTER_TWICE = True
since we also added email verification mandatory, we need djnago to have an email backend
Configuring Django Email Backend
for testing purposes, its often better to use the console backend like so
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
but for me, I prefer to use the file backend as it will create a proper html file for our emails
EMAIL_BACKEND = 'django.core.mail.backends.filebased.EmailBackend'
EMAIL_FILE_PATH = BASE_DIR.joinpath('test').joinpath('email')
and create a folder directory like so
test/email/
Migrate your database
Django allauth require that you migrate its files with the command
python manage.py migrate
you could now go to
End
Congratulation, now your are able to register, and login your user using django-allauth
Posted on June 15, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.