Reputation: 5366
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?
Upvotes: 20
Views: 14971
Reputation: 86
Django maintains a list of "authentication backends" that checks for authentication. If one authentication method fails, Django tries the other methods.
The default django.contrib.auth.backends.ModelBackend
requires you to provide the username. If you want to add customization to it, you need to add another authentication backend. For your use case, allauth.account.auth_backends.AuthenticationBackend
So, modify the AUTHENTICATION_BACKENDS
setting like following:
AUTHENTICATION_BACKENDS = (
"django.contrib.auth.backends.ModelBackend",
"allauth.account.auth_backends.AuthenticationBackend",
)
You can read more about customizing authentication backends here: https://docs.djangoproject.com/en/5.1/topics/auth/customizing/
Upvotes: 0
Reputation: 2752
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'
The above class is the settings which is in allauth
, so you should write 'EMAIL' in lower case.
Upvotes: 1
Reputation: 5366
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",
)
Upvotes: 39