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():
|
||||
return 'Неизвестное сочетание имени пользователя и пароля'
|
||||
return 'Неизвестное сочетание имени пользователя (email) и пароля'
|
||||
|
||||
|
||||
def passwordsNotMatch():
|
||||
|
|
|
@ -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(
|
||||
request=self.context.get('request'),
|
||||
username=username,
|
||||
password=password
|
||||
)
|
||||
if not user:
|
||||
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'
|
||||
)
|
||||
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
|
||||
|
||||
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -69,7 +69,7 @@ function LoginPage() {
|
|||
<img alt='Концепт Портал' src={resources.logo} className='max-h-[2.5rem] min-w-[2.5rem] mb-3' />
|
||||
<TextInput
|
||||
id='username'
|
||||
label='Имя пользователя'
|
||||
label='Логин или email'
|
||||
autoComplete='username'
|
||||
autoFocus
|
||||
required
|
||||
|
|
Loading…
Reference in New Issue
Block a user