mirror of
https://github.com/IRBorisov/ConceptPortal.git
synced 2025-06-26 13:00:39 +03:00
Enable email login
This commit is contained in:
parent
66918217b4
commit
d7761060e7
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
|
|
||||||
def passwordAuthFailed():
|
def passwordAuthFailed():
|
||||||
return 'Неизвестное сочетание имени пользователя и пароля'
|
return 'Неизвестное сочетание имени пользователя (email) и пароля'
|
||||||
|
|
||||||
|
|
||||||
def passwordsNotMatch():
|
def passwordsNotMatch():
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
''' Serializers: User profile and Authorization. '''
|
''' Serializers: User profile and Authorization. '''
|
||||||
from urllib import request
|
|
||||||
|
|
||||||
from django.contrib.auth import authenticate
|
from django.contrib.auth import authenticate
|
||||||
from django.contrib.auth.password_validation import validate_password
|
from django.contrib.auth.password_validation import validate_password
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
@ -32,19 +30,27 @@ class LoginSerializer(serializers.Serializer):
|
||||||
)
|
)
|
||||||
|
|
||||||
def validate(self, attrs):
|
def validate(self, attrs):
|
||||||
username = attrs.get('username')
|
username = attrs['username']
|
||||||
password = attrs.get('password')
|
if '@' in username:
|
||||||
user = authenticate(
|
user = models.User.objects.filter(email=username)
|
||||||
request=self.context.get('request'),
|
if not user.exists() or user.count() > 1:
|
||||||
username=username,
|
|
||||||
password=password
|
|
||||||
)
|
|
||||||
if not user:
|
|
||||||
raise serializers.ValidationError(
|
raise serializers.ValidationError(
|
||||||
msg.passwordAuthFailed(),
|
msg.passwordAuthFailed(),
|
||||||
code='authorization'
|
code='authorization'
|
||||||
)
|
)
|
||||||
attrs['user'] = user
|
username = user.first().username
|
||||||
|
password = attrs['password']
|
||||||
|
authenticated = authenticate(
|
||||||
|
request=self.context.get('request'),
|
||||||
|
username=username,
|
||||||
|
password=password
|
||||||
|
)
|
||||||
|
if not authenticated:
|
||||||
|
raise serializers.ValidationError(
|
||||||
|
msg.passwordAuthFailed(),
|
||||||
|
code='authorization'
|
||||||
|
)
|
||||||
|
attrs['user'] = authenticated
|
||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,10 @@ class TestUserAPIViews(EndpointTester):
|
||||||
self.executeAccepted(data)
|
self.executeAccepted(data)
|
||||||
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')
|
@decl_endpoint('/users/api/logout', method='post')
|
||||||
def test_logout(self):
|
def test_logout(self):
|
||||||
|
|
|
@ -69,7 +69,7 @@ function LoginPage() {
|
||||||
<img alt='Концепт Портал' src={resources.logo} className='max-h-[2.5rem] min-w-[2.5rem] mb-3' />
|
<img alt='Концепт Портал' src={resources.logo} className='max-h-[2.5rem] min-w-[2.5rem] mb-3' />
|
||||||
<TextInput
|
<TextInput
|
||||||
id='username'
|
id='username'
|
||||||
label='Имя пользователя'
|
label='Логин или email'
|
||||||
autoComplete='username'
|
autoComplete='username'
|
||||||
autoFocus
|
autoFocus
|
||||||
required
|
required
|
||||||
|
|
Loading…
Reference in New Issue
Block a user