From d7761060e7420f353b92fb7a5f2fa8ece8d2d6e1 Mon Sep 17 00:00:00 2001 From: IRBorisov <8611739+IRBorisov@users.noreply.github.com> Date: Tue, 28 May 2024 15:29:35 +0300 Subject: [PATCH] Enable email login --- rsconcept/backend/apps/users/messages.py | 2 +- rsconcept/backend/apps/users/serializers.py | 20 ++++++++++++------- rsconcept/backend/apps/users/tests/t_views.py | 4 ++++ rsconcept/frontend/src/pages/LoginPage.tsx | 2 +- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/rsconcept/backend/apps/users/messages.py b/rsconcept/backend/apps/users/messages.py index a7ea4f94..f142f37a 100644 --- a/rsconcept/backend/apps/users/messages.py +++ b/rsconcept/backend/apps/users/messages.py @@ -3,7 +3,7 @@ def passwordAuthFailed(): - return 'Неизвестное сочетание имени пользователя и пароля' + return 'Неизвестное сочетание имени пользователя (email) и пароля' def passwordsNotMatch(): diff --git a/rsconcept/backend/apps/users/serializers.py b/rsconcept/backend/apps/users/serializers.py index 519f74d6..2843f832 100644 --- a/rsconcept/backend/apps/users/serializers.py +++ b/rsconcept/backend/apps/users/serializers.py @@ -1,6 +1,4 @@ ''' Serializers: User profile and Authorization. ''' -from urllib import request - from django.contrib.auth import authenticate from django.contrib.auth.password_validation import validate_password from rest_framework import serializers @@ -32,19 +30,27 @@ class LoginSerializer(serializers.Serializer): ) def validate(self, attrs): - username = attrs.get('username') - password = attrs.get('password') - user = authenticate( + username = attrs['username'] + if '@' in username: + user = models.User.objects.filter(email=username) + if not user.exists() or user.count() > 1: + raise serializers.ValidationError( + msg.passwordAuthFailed(), + code='authorization' + ) + username = user.first().username + password = attrs['password'] + authenticated = authenticate( request=self.context.get('request'), username=username, password=password ) - if not user: + if not authenticated: raise serializers.ValidationError( msg.passwordAuthFailed(), code='authorization' ) - attrs['user'] = user + attrs['user'] = authenticated return attrs diff --git a/rsconcept/backend/apps/users/tests/t_views.py b/rsconcept/backend/apps/users/tests/t_views.py index cc7f51b2..6b7d40de 100644 --- a/rsconcept/backend/apps/users/tests/t_views.py +++ b/rsconcept/backend/apps/users/tests/t_views.py @@ -22,6 +22,10 @@ class TestUserAPIViews(EndpointTester): self.executeAccepted(data) self.executeAccepted(data) + self.logout() + data = {'username': self.user.email, 'password': 'password'} + self.executeAccepted(data) + @decl_endpoint('/users/api/logout', method='post') def test_logout(self): diff --git a/rsconcept/frontend/src/pages/LoginPage.tsx b/rsconcept/frontend/src/pages/LoginPage.tsx index e422373c..ece4bd3c 100644 --- a/rsconcept/frontend/src/pages/LoginPage.tsx +++ b/rsconcept/frontend/src/pages/LoginPage.tsx @@ -69,7 +69,7 @@ function LoginPage() { Концепт Портал