Django rest auth email instead of username

自闭症网瘾萝莉.ら 提交于 2019-12-18 02:46:31

问题


I have a django project in which I am using Django-rest-auth to do authentication. I want to use email with password to authenticate the user and not the username+password.

I have following settings in my settings.py but it didn't do anything for me:

REST_SESSION_LOGIN = True
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_AUTHENTICATION_METHOD = 'EMAIL'
ACCOUNT_EMAIL_VERIFICATION = 'optional'

How can I achieve it?


回答1:


Following setting worked:

#This is required otherwise it asks for email server
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
# ACCOUNT_EMAIL_REQUIRED = True
# AUTHENTICATION_METHOD = 'EMAIL'
# ACCOUNT_EMAIL_VERIFICATION = 'optional'

ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_EMAIL_REQUIRED = True   
ACCOUNT_USERNAME_REQUIRED = False

#Following is added to enable registration with email instead of username
AUTHENTICATION_BACKENDS = (
 # Needed to login by username in Django admin, regardless of `allauth`
 "django.contrib.auth.backends.ModelBackend",

 # `allauth` specific authentication methods, such as login by e-mail
 "allauth.account.auth_backends.AuthenticationBackend",
)



回答2:


I'm using this package too, and by call this config it worked for me:

ACCOUNT_AUTHENTICATION_METHOD = 'email'

Be careful about this config, this config belongs to django-allauth, see this:

class AuthenticationMethod:
    USERNAME = 'username'
    EMAIL = 'email'
    USERNAME_EMAIL = 'username_email'

Above class is a settings which is in allauth, so you write 'EMAIL' in lower case.

I hope it helps



来源:https://stackoverflow.com/questions/36698456/django-rest-auth-email-instead-of-username

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!