본문 바로가기
Python/Django

Django 공식문서 읽기 - Customizing authentication in Django(2)

by 코드뭉치 2023. 5. 2.

Writing an authentication backend

 

 get_user 메서드는 user_id(사용자 이름, 데이터베이스 ID 등일 수 있지만 사용자 개체의 PK여야 함)를

사용하고 사용자 객체 또는 None을 반환한다.

from django.contrib.auth.backends import BaseBackend


class MyBackend(BaseBackend):
    def authenticate(self, request, username=None, password=None):
        # Check the username/password and return a user.
        ...

 

토큰도 인증할 수 있다.

from django.contrib.auth.backends import BaseBackend


class MyBackend(BaseBackend):
    def authenticate(self, request, token=None):
        # Check the token and return a user.
        ...

 

어느 쪽이든 authentication을 확인하고 유효하면 사용자 객체를 반환, 유효하지 않다면 None을 반환

댓글