Django: Allauth
Tags: django, github, security, tutorial
Published: 2021-06-15 10:29:23

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](https://django-allauth.readthedocs.io/en/latest/installation.html). 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](https://django-allauth.readthedocs.io/en/latest/providers.html).

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

```python 
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 

```python
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 

```python
# 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 

```python
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

```python
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 

[register page](http://127.0.0.1:8000/account/signup/)

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/onwllm857nxuw44kuvib.png)

# End

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