From 96ed43ac6c2475aa1fdba614b169635123528383 Mon Sep 17 00:00:00 2001 From: Aymane Date: Wed, 30 Aug 2023 22:20:23 +0100 Subject: [PATCH] Capital letters emails fix --- accounts/backends.py | 2 +- accounts/forms.py | 6 ++++++ accounts/views.py | 1 + 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/accounts/backends.py b/accounts/backends.py index ace1968..41595f1 100644 --- a/accounts/backends.py +++ b/accounts/backends.py @@ -7,7 +7,7 @@ class EmailBackend(ModelBackend): def authenticate(self, request, username=None, password=None, **kwargs): UserModel = get_user_model() try: - user = UserModel.objects.get(email=username) + user = UserModel.objects.get(email__iexact=username) except UserModel.DoesNotExist: return None else: diff --git a/accounts/forms.py b/accounts/forms.py index 48b9249..3cb8a93 100644 --- a/accounts/forms.py +++ b/accounts/forms.py @@ -36,6 +36,12 @@ class Meta: ] # We need the user object, so it's an additional parameter + def clean_email(self): + email = self.cleaned_data.get('email').lower() + if CustomUser.objects.filter(email__iexact=email).exists(): + raise forms.ValidationError("Email address already exists.") + return email + def send_activation_email(self, request, user): current_site = get_current_site(request) subject = 'Activate Your Account' diff --git a/accounts/views.py b/accounts/views.py index 0883efc..1ab8c10 100644 --- a/accounts/views.py +++ b/accounts/views.py @@ -72,6 +72,7 @@ def get_form_kwargs(self): return kwargs def form_valid(self, form): + form.instance.email = form.instance.email.lower() to_return = super().form_valid(form) user = form.save() user.is_verified = False # Turns the user email verification to False