diff --git a/django_appwrite/authentication.py b/django_appwrite/authentication.py index 76ac879..d94e27f 100644 --- a/django_appwrite/authentication.py +++ b/django_appwrite/authentication.py @@ -34,13 +34,15 @@ def authenticate(self, request): email = appwrite_settings['prefix_email'] + user_info['email'] password = settings.SECRET_KEY + user_info['$id'] + username_field = getattr(User, 'USERNAME_FIELD', 'username') + # Get or create a corresponding Django user - django_user = User.objects.filter(username=email).first() + django_user = User.objects.filter(**{username_field: email}).first() if not django_user: - User.objects.create_user(username=email, password=password, email=email) + User.objects.create_user(**{username_field: email, 'password': password}) # Ensure the user can be authenticated with Django's system - auth_user = authenticate(request, username=email, password=password) + auth_user = authenticate(request, **{username_field: email, 'password': password}) if not auth_user: raise AuthenticationFailed('The user could not be authenticated with Django.') diff --git a/django_appwrite/middleware.py b/django_appwrite/middleware.py index 2f6078e..f6933c5 100644 --- a/django_appwrite/middleware.py +++ b/django_appwrite/middleware.py @@ -69,18 +69,18 @@ def __call__(self, request, *args, **kwargs): email = self.prefix_email + user_info['email'] password = settings.SECRET_KEY + user_info['$id'] + + username_field = getattr(User, 'USERNAME_FIELD', 'username') + # Get the Django user by its email - user = User.objects.filter(username=email).first() + user = User.objects.filter(**{username_field: email}).first() # If the user doesn't exist, create it if not user: - User.objects.create_user( - username=email, - password=password, - email=email) + User.objects.create_user(**{username_field: email, 'password': password}) # Authenticate the user using the email as the username - user = authenticate(request, username=email, password=password) + user = authenticate(request, **{username_field: email, 'password': password}) # If the authentication was successful, log the user in if user: