DEV Community

ashrafZolkopli
ashrafZolkopli

Posted on

Django: Allauth

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
Enter fullscreen mode Exit fullscreen mode

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',
    #...
]
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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')),
    #...
]
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

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')
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

you could now go to

register page

image

End

Congratulation, now your are able to register, and login your user using django-allauth

Latest comments (0)