在我的项目中,我使用allauth
,并在我的自定义LoginSerializer的validate
方法中:
if 'allauth' in settings.INSTALLED_APPS:
from allauth.account import app_settings
# Authentication through email
if app_settings.AUTHENTICATION_METHOD == app_settings.AuthenticationMethod.EMAIL:
user = self._validate_email(email, password)
# Authentication through username
if app_settings.AUTHENTICATION_METHOD == app_settings.AuthenticationMethod.USERNAME:
user = self._validate_username(username, password)
你看到有app_settings.AuthenticationMethod.EMAIL
和app_settings.AuthenticationMethod.USERNAME
类型。
我阅读了我发现的源代码:
还有另一种类型USERNAME_EMAIL
:
class AppSettings(object):
class AuthenticationMethod:
USERNAME = 'username'
EMAIL = 'email'
USERNAME_EMAIL = 'username_email'
我在哪里设置allauth使用AuthenticationMethod.USERNAME_EMAIL?
在你的ACCOUNT_AUTHENTICATION_METHOD
中配置settings.py
它会起作用:
ACCOUNT_AUTHENTICATION_METHOD = 'username_email'
请小心,不要设置AUTHENTICATION_METHOD
。
你可以在official document找到更多细节。