Help us understand the problem. What is going on with this article?

Django REST Frameworkで新しいユーザー認証方法を増やす

通常はAbstractBaseUserを継承したクラスのUSERNAME_FIELDで定義されたカラムとpasswordでユーザー認証されますが
新たにlogin_idというカラムを追加し、login_idpasswordでもユーザー認証できるようにしてみます

1. AUTHENTICATION_BACKENDSの追加

ユーザー認証をカスタマイズしたいため、AUTHENTICATION_BACKENDSを追加します
AUTHENTICATION_BACKENDSについてはリファレンスを参照
https://docs.djangoproject.com/en/dev/ref/settings/#authentication-backends

backends.py
from django.contrib.auth.backends import ModelBackend
from project.models.user import User


class LoginIdModelBackend(ModelBackend):
    """
    login_idとpasswordのログイン
    """
    def authenticate(self, request, username=None, password=None, **kwargs):
        try:
            login_id = kwargs.get('login_id')
            if not login_id:
                raise User.DoesNotExist
            user = User.objects.get(login_id=login_id)
        except User.DoesNotExist:
            # Run the default password hasher once to reduce the timing
            # difference between an existing and a nonexistent user (#20760).
            User().set_password(password)
        else:
            if user.check_password(password) and self.user_can_authenticate(user):
                return user
settings.py
AUTHENTICATION_BACKENDS = [
    'django.contrib.auth.backends.ModelBackend',
    'backends.LoginIdModelBackend',
]

Userテーブルにlogin_idカラムがあることが前提です
AUTHENTICATION_BACKENDSは定義した認証に失敗したら次の認証というように順番に呼び出せれます
なので、まず今までの認証を実行し、それに失敗したらログインIDでの認証が実行されます

2. login_idpassword用のユーザー認証APIを追加

views/auth.py
from rest_framework_simplejwt.views import TokenViewBase
from project import serializers


class LoginIdAuthTokenViewSet(TokenViewBase):
    serializer_class = serializers.LoginIdAuthTokenSerializer
serializer/auth.py
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer


class LoginIdAuthTokenSerializer(TokenObtainPairSerializer):
    username_field = 'login_id'

    def create(self, validated_data):
        pass

    def update(self, instance, validated_data):
        pass

urls.pyにも追加してください

今回は別のAPIにしましたが、ロジック次第で一つのAPIで、例えばメールアドレスでもログインIDでもログインできるようにすることも可能だと思います

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした