Djangoでdjango-allauthとCustomUserを使った認証機能を作成

##はじめに
django-allauthとCustomUserを使った認証をしたかったのですが、日本語記事が少なく、いい英語記事を探すのが大変だったので、sampleとハマったところの解説、プログラムを貼っておきます。

#目次
1.[ sample](#1sample)
2.[ ハマったところ](#2ハマったところ)
3.[ ファイル構成](#3ファイル構成)
4.[ url.py](#4urlpy)
5.[ settings.py](#5settingspy)
6.[ models.py](#6modelspy)
7.[ forms.py](#7formspy)
8.[ adapter.py](#8adapterpy)
9.[ templates](#9templates)
10.[ 参考](#10参考)

##1.sample
[**samplecode(Github)**](https://github.com/Kei-Segawa/testsite)

実際に動かしてみるとこうなります。![1573995017.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/297982/c7c50d53-30cd-555a-a71f-2e7de0dc9eeb.jpeg)

#2.ハマったところ
- **ハマったところ**<br>
Userとsignupformを拡張して、年齢と体重の項目を追加したが、CustomUserで追加したage,weightに**値が入らない**。
- **理由**<br>
django-allauthでは、Customuserでuserを、CustomSignupformでsignupformを拡張してもsignupformから入力された値はユーザーフィールドに`default`で**保存されない**。
- **解決法**<br>
`adapter.py`を作り、`AccountAdapterクラス`と`save_user関数`を記述すると、拡張したフィールドに入力された値がCustomUserに**保存できるようになる**。

#3.ファイル構成
`testsite/`
&emsp;├`templates/`
&emsp;│ &emsp;&emsp; ├`accounts/`
&emsp;│ &emsp;&emsp; │ &emsp;&emsp;└`registration/`
&emsp;│ &emsp;&emsp; │ &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;└`login.html/`
&emsp;│ &emsp;&emsp; │ &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;└`signup.html/`
&emsp;│  &emsp;&emsp;├`testapp/`
&emsp;│  &emsp;&emsp;│ &emsp;&emsp;└`home.html`
&emsp;│  &emsp;&emsp;└`base.html`
&emsp;├`testapp/`
&emsp;│  &emsp;&emsp;├`__pycache__`
&emsp;│  &emsp;&emsp;├`migrations`
&emsp;│  &emsp;&emsp;├`adapter.py`
&emsp;│  &emsp;&emsp;├`admin.py`
&emsp;│  &emsp;&emsp;├`apps.py`
&emsp;│  &emsp;&emsp;├`forms.py`
&emsp;│  &emsp;&emsp;├`models.py`
&emsp;│  &emsp;&emsp;├`tests.py`
&emsp;│  &emsp;&emsp;├`urls.py`
&emsp;│  &emsp;&emsp;└`views.py`
&emsp;├`testsite/`
&emsp;│  &emsp;&emsp;├`__pycache__`
&emsp;│  &emsp;&emsp;├`__init__`
&emsp;│  &emsp;&emsp;├`settings.py`
&emsp;│  &emsp;&emsp;├`urls.py`
&emsp;│  &emsp;&emsp;└`wsgi.py`
&emsp;└`manage.py`

#4.url.py
```python:url.py
from django.contrib import admin
from django.urls import include, path
from django.views.generic import TemplateView

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',include('testapp.urls')),
    path('accounts/', include('allauth.urls')),
    path('accounts/login/', TemplateView.as_view(template_name = 'login.html'), name='login'),
    path('accounts/logout/', TemplateView.as_view(template_name = 'logout.html'), name='logout'),
    path('accounts/signup/', TemplateView.as_view(template_name = 'signup.html'), name='signup'),
]
```
#5.settings.py
customuser,signupform,adapterの設定は必須。

```python:settings.py
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

SECRET_KEY = ''

DEBUG = True

ALLOWED_HOSTS = ['*']

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'testapp.apps.TestappConfig',#testapp
    'django.contrib.sites', #for djnago-allauth
    'allauth', #for djnago-allauth
    'allauth.account', #for djnago-allauth
    'allauth.socialaccount',#for djnago-allauth
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'testsite.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'testsite.wsgi.application'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

LANGUAGE_CODE = 'ja'

TIME_ZONE = 'Asia/Tokyo'

USE_I18N = True

USE_L10N = True

USE_TZ = True

STATIC_URL = '/static/'

#追加
AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
    'allauth.account.auth_backends.AuthenticationBackend',
)

# 認証方式を「メールアドレスとパスワード」に変更
ACCOUNT_AUTHENTICATION_METHOD = 'email'
# ユーザー名を使用する
ACCOUNT_USERNAME_REQUIRED = True

# ユーザー登録確認メールは送信しない
ACCOUNT_EMAIL_VERIFICATION = 'none'
# メールアドレスを必須項目にする
ACCOUNT_EMAIL_REQUIRED = True

#ユーザーモデルの拡張(customuser)
AUTH_USER_MODEL = 'testapp.CustomUser'

SITE_ID = 1 #django-allauthがsitesフレームワークを使っているため

LOGIN_REDIRECT_URL = 'home'
ACCOUNT_LOGOUT_REDIRECT_URL = '/accounts/login/'
ACCOUNT_LOGOUT_ON_GET = True

#signupformを指定
ACCOUNT_FORMS = {
    'signup' : 'testapp.forms.CustomSignupForm',
}
#signupformからの情報をcustomusermodelに保存するのに必要
ACCOUNT_ADAPTER = 'testapp.adapter.AccountAdapter'
#passwordの入力を一回に
ACCOUNT_SIGNUP_PASSWORD_ENTER_TWICE = False
```


#6.models.py
```python:models.py
from django.db import models
from django.contrib.auth.models import AbstractUser

# Create your models here.
class CustomUser(AbstractUser):
    """拡張ユーザーモデル"""
    class Meta(AbstractUser.Meta):
        db_table = 'custom_user'

    age = models.IntegerField('年齢', blank=True, default=0)
    weight = models.IntegerField('体重',blank=True, default=0)
```
#7.forms.py
```python:forms.py
from allauth.account.forms import SignupForm
from django import forms
from .models import CustomUser
from allauth.account.adapter import DefaultAccountAdapter

class CustomSignupForm(SignupForm):
    age = forms.IntegerField()
    weight = forms.IntegerField()
    
    class Meta:
        model = CustomUser

    def signup(self, request,user):
        user.age = self.cleaned_data['age']
        user.weight = self.cleaned_data['weight']
        user.save()
        return user
```
#8.adapter.py
```python:adapter.py
from allauth.account.adapter import DefaultAccountAdapter

class AccountAdapter(DefaultAccountAdapter):

    def save_user(self, request, user, form, commit=True):
        """
        This is called when saving user via allauth registration.
        We override this to set additional data on user object.
        """
        # Do not persist the user yet so we pass commit=False
        # (last argument)
        user = super(AccountAdapter, self).save_user(request, user, form, commit=False)
        user.age = form.cleaned_data.get('age')
        user.weight = form.cleaned_data.get('weight')
        user.save()
```
#9.templates
```python:base.html
<!doctype html>
<html lang="ja">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>{% block title %}{% endblock %}</title>

    {# --- css --- #}
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
        integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
    <style type="text/css">
        body {
            padding-top: 5rem;
        }
    </style>
    {% block extra_css %}{% endblock %}
</head>

<body>
    <main class="container">
        {% block content %}{% endblock %}
    </main>

    {# --- js --- #}
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
        integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous">
    </script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"
        integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous">
    </script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"
        integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous">
    </script>
    {% block extra_js %}{% endblock %}
</body>

</html>
```
```python:account/registration/login.html
<!doctype html>
{% extends "base.html" %}

{% block title %}ログイン{% endblock %}

{% block content %}
<h2>ログイン</h2>
<form method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">ログイン</button>
</form>
{% endblock %}
```
```python:account/registration/signup.html
<!doctype html>
{% extends "base.html" %}

{% block title %}ユーザー登録{% endblock %}

{% block content %}
<h2>ユーザー登録</h2>
<form method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <p><a href="{% url 'login' %}"><button type="submit">ユーザー登録</button></a></p>
</form>
{% endblock %}
```
```python:testapp/home.html
<!doctype html>
{% extends "base.html" %}

{% block title %}ホーム{% endblock %}

{% block content %}
<h2>ホーム</h2>
{% if user.is_authenticated %}
ようこそ {{ user.get_username }} さん
あなたの年齢は{{ user.age }}です
あなたの体重は{{ user.weight }}kgです
<p><a href="{% url 'logout' %}">ログアウト</a></p>
{% else %}
<p><a href="{% url 'login' %}">ログイン</a></p>
<p><a href="{% url 'signup' %}">signup</a></p>
{% endif %}
{% endblock %}
```
#10.参考
>[横瀬 明仁「現場で使えるdjangoの教科書」](https://www.amazon.co.jp/%E7%8F%BE%E5%A0%B4%E3%81%A7%E4%BD%BF%E3%81%88%E3%82%8B-Django-%E3%81%AE%E6%95%99%E7%A7%91%E6%9B%B8%E3%80%8A%E5%AE%9F%E8%B7%B5%E7%B7%A8%E3%80%8B-%E6%A8%AA%E7%80%AC-%E6%98%8E%E4%BB%81-ebook/dp/B07L3DRGBT?tag=maftracking264432-22&linkCode=ure&creative=6339)
>[django-allauthの公式1](https://django-allauth.readthedocs.io/en/latest/advanced.html#creating-and-populating-user-instances)
>[django-allauthの公式2](https://django-allauth.readthedocs.io/en/latest/configuration.html)
> [the-complete-django-allauth-guide](https://dev.to/gajesh/the-complete-django-allauth-guide-la3)
>[extending-and-customizing-django-allauth](https://medium.com/@gajeshbhat/extending-and-customizing-django-allauth-eed206623a1a)