mirror of
https://github.com/Tivix/django-rest-auth.git
synced 2025-07-23 14:09:46 +03:00
login through email possible, checking allauth settings
This commit is contained in:
parent
4b188023ff
commit
a18c45045e
|
@ -37,7 +37,7 @@ class Register(APIView, SignupView):
|
||||||
authentication_classes = (EverybodyCanAuthentication,)
|
authentication_classes = (EverybodyCanAuthentication,)
|
||||||
token_model = Token
|
token_model = Token
|
||||||
token_serializer = TokenSerializer
|
token_serializer = TokenSerializer
|
||||||
user_serializer = UserDetailsSerializer
|
user_serializer_class = UserDetailsSerializer
|
||||||
allowed_methods = ('POST', 'OPTIONS', 'HEAD')
|
allowed_methods = ('POST', 'OPTIONS', 'HEAD')
|
||||||
|
|
||||||
def get(self, *args, **kwargs):
|
def get(self, *args, **kwargs):
|
||||||
|
@ -71,7 +71,7 @@ class Register(APIView, SignupView):
|
||||||
|
|
||||||
def get_response(self):
|
def get_response(self):
|
||||||
response = self.token_serializer(self.token).data
|
response = self.token_serializer(self.token).data
|
||||||
user = self.user_serializer(instance=self.user).data
|
user = self.user_serializer_class(instance=self.user).data
|
||||||
response['user'] = user
|
response['user'] = user
|
||||||
return Response(response, status=status.HTTP_201_CREATED)
|
return Response(response, status=status.HTTP_201_CREATED)
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
|
from django.contrib.auth import authenticate
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
from django.utils.translation import ugettext_lazy as _
|
||||||
from django.contrib.auth.forms import PasswordResetForm, SetPasswordForm
|
from django.contrib.auth.forms import PasswordResetForm, SetPasswordForm
|
||||||
try:
|
try:
|
||||||
from django.utils.http import urlsafe_base64_decode as uid_decoder
|
from django.utils.http import urlsafe_base64_decode as uid_decoder
|
||||||
|
@ -8,16 +10,76 @@ except:
|
||||||
from django.utils.http import base36_to_int as uid_decoder
|
from django.utils.http import base36_to_int as uid_decoder
|
||||||
from django.contrib.auth.tokens import default_token_generator
|
from django.contrib.auth.tokens import default_token_generator
|
||||||
|
|
||||||
from rest_framework import serializers
|
from rest_framework import (
|
||||||
|
exceptions,
|
||||||
|
serializers,
|
||||||
|
)
|
||||||
from rest_framework.authtoken.models import Token
|
from rest_framework.authtoken.models import Token
|
||||||
from rest_framework.authtoken.serializers import AuthTokenSerializer
|
# from rest_framework.authtoken.serializers import AuthTokenSerializer
|
||||||
from rest_framework.exceptions import ValidationError
|
from rest_framework.exceptions import ValidationError
|
||||||
|
|
||||||
|
|
||||||
class LoginSerializer(AuthTokenSerializer):
|
class LoginSerializer(serializers.Serializer):
|
||||||
|
username = serializers.CharField(required=False)
|
||||||
|
email = serializers.EmailField(required=False)
|
||||||
|
password = serializers.CharField(style={'input_type': 'password'})
|
||||||
|
|
||||||
def validate(self, attrs):
|
def validate(self, attrs):
|
||||||
attrs = super(LoginSerializer, self).validate(attrs)
|
username = attrs.get('username')
|
||||||
|
email = attrs.get('email')
|
||||||
|
password = attrs.get('password')
|
||||||
|
|
||||||
|
if 'allauth' in settings.INSTALLED_APPS:
|
||||||
|
from allauth.account import app_settings
|
||||||
|
|
||||||
|
# Authentication through email
|
||||||
|
if app_settings.AUTHENTICATION_METHOD == app_settings.AuthenticationMethod.EMAIL:
|
||||||
|
if email and password:
|
||||||
|
user = authenticate(email=email, password=password)
|
||||||
|
else:
|
||||||
|
msg = _('Must include "email" and "password".')
|
||||||
|
raise exceptions.ValidationError(msg)
|
||||||
|
# Authentication through username
|
||||||
|
elif app_settings.AUTHENTICATION_METHOD == app_settings.AuthenticationMethod.USERNAME:
|
||||||
|
if username and password:
|
||||||
|
user = authenticate(username=username, password=password)
|
||||||
|
else:
|
||||||
|
msg = _('Must include "username" and "password".')
|
||||||
|
raise exceptions.ValidationError(msg)
|
||||||
|
# Authentication through either username or email
|
||||||
|
else:
|
||||||
|
if email and password:
|
||||||
|
user = authenticate(email=email, password=password)
|
||||||
|
elif username and password:
|
||||||
|
user = authenticate(username=username, password=password)
|
||||||
|
else:
|
||||||
|
msg = _('Must include either "username" or "email" and "password".')
|
||||||
|
raise exceptions.ValidationError(msg)
|
||||||
|
|
||||||
|
if user:
|
||||||
|
if not user.is_active:
|
||||||
|
msg = _('User account is disabled.')
|
||||||
|
raise exceptions.ValidationError(msg)
|
||||||
|
else:
|
||||||
|
msg = _('Unable to log in with provided credentials.')
|
||||||
|
raise exceptions.ValidationError(msg)
|
||||||
|
|
||||||
|
elif username and password:
|
||||||
|
user = authenticate(username=username, password=password)
|
||||||
|
|
||||||
|
if user:
|
||||||
|
if not user.is_active:
|
||||||
|
msg = _('User account is disabled.')
|
||||||
|
raise exceptions.ValidationError(msg)
|
||||||
|
else:
|
||||||
|
msg = _('Unable to log in with provided credentials.')
|
||||||
|
raise exceptions.ValidationError(msg)
|
||||||
|
else:
|
||||||
|
msg = _('Must include "username" and "password".')
|
||||||
|
raise exceptions.ValidationError(msg)
|
||||||
|
|
||||||
|
attrs['user'] = user
|
||||||
|
|
||||||
if 'rest_auth.registration' in settings.INSTALLED_APPS:
|
if 'rest_auth.registration' in settings.INSTALLED_APPS:
|
||||||
from allauth.account import app_settings
|
from allauth.account import app_settings
|
||||||
if app_settings.EMAIL_VERIFICATION == app_settings.EmailVerificationMethod.MANDATORY:
|
if app_settings.EMAIL_VERIFICATION == app_settings.EmailVerificationMethod.MANDATORY:
|
||||||
|
@ -25,9 +87,24 @@ class LoginSerializer(AuthTokenSerializer):
|
||||||
email_address = user.emailaddress_set.get(email=user.email)
|
email_address = user.emailaddress_set.get(email=user.email)
|
||||||
if not email_address.verified:
|
if not email_address.verified:
|
||||||
raise serializers.ValidationError('E-mail is not verified.')
|
raise serializers.ValidationError('E-mail is not verified.')
|
||||||
|
|
||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
|
|
||||||
|
# class LoginSerializer(AuthTokenSerializer):
|
||||||
|
|
||||||
|
# def validate(self, attrs):
|
||||||
|
# attrs = super(LoginSerializer, self).validate(attrs)
|
||||||
|
# if 'rest_auth.registration' in settings.INSTALLED_APPS:
|
||||||
|
# from allauth.account import app_settings
|
||||||
|
# if app_settings.EMAIL_VERIFICATION == app_settings.EmailVerificationMethod.MANDATORY:
|
||||||
|
# user = attrs['user']
|
||||||
|
# email_address = user.emailaddress_set.get(email=user.email)
|
||||||
|
# if not email_address.verified:
|
||||||
|
# raise serializers.ValidationError('E-mail is not verified.')
|
||||||
|
# return attrs
|
||||||
|
|
||||||
|
|
||||||
class TokenSerializer(serializers.ModelSerializer):
|
class TokenSerializer(serializers.ModelSerializer):
|
||||||
"""
|
"""
|
||||||
Serializer for Token model.
|
Serializer for Token model.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user