From 13060f0685c28e5645d65a00c0be661ad39dd3fa Mon Sep 17 00:00:00 2001 From: kdiaz Date: Wed, 14 Mar 2018 20:46:53 -0500 Subject: [PATCH] corrections for used in angular 5 --- rest_auth/registration/serializers.py | 12 +++++++++++- rest_auth/registration/urls.py | 13 +++++++++++-- rest_auth/serializers.py | 12 ++++++++++++ rest_auth/urls.py | 9 +++++++++ 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/rest_auth/registration/serializers.py b/rest_auth/registration/serializers.py index 4f99c18..7ff9041 100644 --- a/rest_auth/registration/serializers.py +++ b/rest_auth/registration/serializers.py @@ -1,4 +1,6 @@ +from django.conf import settings from django.http import HttpRequest + from django.utils.translation import ugettext_lazy as _ from django.contrib.auth import get_user_model @@ -17,7 +19,11 @@ except ImportError: from rest_framework import serializers from requests.exceptions import HTTPError - +if not hasattr(settings, 'ACCOUNT_FIRST_NAME_REQUIRED'): + settings.ACCOUNT_FIRST_NAME_REQUIRED = False +if not hasattr(settings, 'ACCOUNT_LAST_NAME_REQUIRED'): + settings.ACCOUNT_LAST_NAME_REQUIRED = False + class SocialAccountSerializer(serializers.ModelSerializer): """ serialize allauth SocialAccounts for use with a REST API @@ -169,6 +175,8 @@ class RegisterSerializer(serializers.Serializer): min_length=allauth_settings.USERNAME_MIN_LENGTH, required=allauth_settings.USERNAME_REQUIRED ) + first_name = serializers.CharField(required=settings.ACCOUNT_FIRST_NAME_REQUIRED, max_length=30) + last_name = serializers.CharField(required=settings.ACCOUNT_LAST_NAME_REQUIRED, max_length=30) email = serializers.EmailField(required=allauth_settings.EMAIL_REQUIRED) password1 = serializers.CharField(write_only=True) password2 = serializers.CharField(write_only=True) @@ -198,6 +206,8 @@ class RegisterSerializer(serializers.Serializer): def get_cleaned_data(self): return { + 'first_name': self.validated_data.get('first_name', ''), + 'last_name': self.validated_data.get('last_name', ''), 'username': self.validated_data.get('username', ''), 'password1': self.validated_data.get('password1', ''), 'email': self.validated_data.get('email', '') diff --git a/rest_auth/registration/urls.py b/rest_auth/registration/urls.py index 1004695..8a86624 100644 --- a/rest_auth/registration/urls.py +++ b/rest_auth/registration/urls.py @@ -3,6 +3,16 @@ from django.conf.urls import url from .views import RegisterView, VerifyEmailView + +from django.contrib.sites.shortcuts import get_current_site +from django.views.generic.base import RedirectView +class ConfirmEmailRedirectView(RedirectView): + permanent = False + def get_redirect_url(self, *args, **kwargs): + current_site = get_current_site(self.request) + self.url = '%s://%s/auth/confirm-email/%s' % (self.request.scheme,current_site.domain, kwargs["key"]) + return super().get_redirect_url(*args, **kwargs) + urlpatterns = [ url(r'^$', RegisterView.as_view(), name='rest_register'), url(r'^verify-email/$', VerifyEmailView.as_view(), name='rest_verify_email'), @@ -18,6 +28,5 @@ urlpatterns = [ # If you don't want to use API on that step, then just use ConfirmEmailView # view from: # django-allauth https://github.com/pennersr/django-allauth/blob/master/allauth/account/views.py - url(r'^account-confirm-email/(?P[-:\w]+)/$', TemplateView.as_view(), - name='account_confirm_email'), + url(r'^account-confirm-email/(?P[-:\w]+)/$', ConfirmEmailRedirectView.as_view(),name='account_confirm_email'), ] diff --git a/rest_auth/serializers.py b/rest_auth/serializers.py index 45729b2..16eb5a9 100644 --- a/rest_auth/serializers.py +++ b/rest_auth/serializers.py @@ -26,6 +26,12 @@ class LoginSerializer(serializers.Serializer): if email and password: user = authenticate(email=email, password=password) + try: + username = UserModel.objects.get(email__iexact=email).get_username() + user = authenticate(username=username, password=password) + except UserModel.DoesNotExist: + msg = _('Unable to log in with provided credentials.') + raise exceptions.ValidationError(msg) else: msg = _('Must include "email" and "password".') raise exceptions.ValidationError(msg) @@ -48,6 +54,12 @@ class LoginSerializer(serializers.Serializer): if email and password: user = authenticate(email=email, password=password) + try: + username = UserModel.objects.get(email__iexact=email).get_username() + user = authenticate(username=username, password=password) + except UserModel.DoesNotExist: + msg = _('Unable to log in with provided credentials.') + raise exceptions.ValidationError(msg) elif username and password: user = authenticate(username=username, password=password) else: diff --git a/rest_auth/urls.py b/rest_auth/urls.py index 7a35e9b..e5e22e2 100644 --- a/rest_auth/urls.py +++ b/rest_auth/urls.py @@ -5,6 +5,14 @@ from rest_auth.views import ( PasswordResetView, PasswordResetConfirmView ) +from rest_framework import status +from rest_framework.decorators import api_view +from rest_framework.response import Response + +@api_view() +def null_view(request): + return Response(status=status.HTTP_400_BAD_REQUEST) + urlpatterns = [ # URLs that do not require a session or valid token url(r'^password/reset/$', PasswordResetView.as_view(), @@ -17,4 +25,5 @@ urlpatterns = [ url(r'^user/$', UserDetailsView.as_view(), name='rest_user_details'), url(r'^password/change/$', PasswordChangeView.as_view(), name='rest_password_change'), + url(r'^new-password/(?P[0-9A-Za-z_\-]+)/(?P[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', null_view ,name='password_reset_confirm'), ]