Skip to content

Commit

Permalink
Add support for login with email/password on the auth/login view
Browse files Browse the repository at this point in the history
  • Loading branch information
DavisRayM committed Aug 21, 2020
1 parent a046b86 commit 92506fa
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
18 changes: 11 additions & 7 deletions onadata/apps/main/backends.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
from django.contrib.auth.models import User
from django.contrib.auth.backends import ModelBackend as DjangoModelBackend
from django.db.models import Q


class ModelBackend(DjangoModelBackend):
def authenticate(self, request=None, username=None, password=None):
"""Username is case insensitive."""
try:
user = User.objects.get(username__iexact=username)
if user.check_password(password):
return user
except User.DoesNotExist:
return None
"""
Username is case insensitive. Supports using email in place of username
"""
user = User.objects.filter(
Q(username__iexact=username) | Q(email=username)).first()

if user and user.check_password(password):
return user

return None
7 changes: 7 additions & 0 deletions onadata/apps/main/tests/test_http_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ def test_http_auth(self):
**self._set_auth_headers('bob', 'bob'))
self.assertEqual(response.status_code, 200)

# Set user email
self.user.email = "bob@testing_pros.com"
self.user.save()
# headers with valid email/pass
response = self.client.get(
self.api_url, **self._set_auth_headers(self.user.email, 'bob'))

def test_http_auth_shared_data(self):
self.xform.shared_data = True
self.xform.save()
Expand Down

0 comments on commit 92506fa

Please sign in to comment.