I have a very simple desire: to authenticate (log in) user over REST. I need to use email and password.
settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.sites',
'django.contrib.staticfiles',
'rest_framework',
'modeltranslation',
'corsheaders',
'django_s3_storage',
'rest_framework.authtoken',
'rest_auth',
'allauth',
'allauth.account'
]
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.SessionAuthentication',
),
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False
AUTHENTICATION_BACKENDS = (
"django.contrib.auth.backends.ModelBackend",
"allauth.account.auth_backends.AuthenticationBackend",
)
urls.py:
re_path(r'^rest-auth/', include('rest_auth.urls')),
When I do:
curl \
-X POST \
-H "Content-Type: application/json" \
-d '{"email": "[email protected]", "password": "zyzzyx' \
http://localhost:8000/rest-auth/login/
The error message is: Unable to log in with provided credentials.
EDIT:
I'm using custom user model:
class CustomUser(AbstractUser):
created = models.DateTimeField(auto_now_add=True)
username = models.CharField(max_length=100, unique=True)
email = models.EmailField(max_length=200, unique=True)
company = models.ForeignKey(Company, on_delete=models.CASCADE, related_name='%(class)s_company')
role = models.CharField(
max_length=100,
choices=ROLE_CHOICES,
default='admin',
)
class Meta:
ordering = ('created',)
db_table = "custom_user"
def __str__(self):
return self.username
User = get_user_model()
In settings.py:
AUTH_USER_MODEL = 'myapps.CustomUser'
curlrequest looks fine. Probably either the credential is wrong or thelogin controlleris having the problem.