From cd20ad484659ab06b26494ea0f6ab765432fd377 Mon Sep 17 00:00:00 2001 From: Philippe Luickx Date: Wed, 15 Apr 2015 17:25:32 +0300 Subject: [PATCH] login through email possible, checking allauth settings --- rest_auth/registration/views.py | 4 +- rest_auth/serializers.py | 85 +++++++++++++++++++++++++++++++-- 2 files changed, 83 insertions(+), 6 deletions(-) diff --git a/rest_auth/registration/views.py b/rest_auth/registration/views.py index 2642e8f..3dada33 100644 --- a/rest_auth/registration/views.py +++ b/rest_auth/registration/views.py @@ -37,7 +37,7 @@ class Register(APIView, SignupView): authentication_classes = (EverybodyCanAuthentication,) token_model = Token token_serializer = TokenSerializer - user_serializer = UserDetailsSerializer + user_serializer_class = UserDetailsSerializer allowed_methods = ('POST', 'OPTIONS', 'HEAD') def get(self, *args, **kwargs): @@ -71,7 +71,7 @@ class Register(APIView, SignupView): def get_response(self): 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 return Response(response, status=status.HTTP_201_CREATED) diff --git a/rest_auth/serializers.py b/rest_auth/serializers.py index e0ab909..d3955f8 100644 --- a/rest_auth/serializers.py +++ b/rest_auth/serializers.py @@ -1,5 +1,7 @@ from django.contrib.auth import get_user_model +from django.contrib.auth import authenticate from django.conf import settings +from django.utils.translation import ugettext_lazy as _ from django.contrib.auth.forms import PasswordResetForm, SetPasswordForm try: 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.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.serializers import AuthTokenSerializer +# from rest_framework.authtoken.serializers import AuthTokenSerializer 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): - 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: from allauth.account import app_settings 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) if not email_address.verified: raise serializers.ValidationError('E-mail is not verified.') + 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): """ Serializer for Token model.