From e156ee97d0aa5c0c12bf0a017ba236d7d483624a Mon Sep 17 00:00:00 2001 From: Bruno Barreto Freitas Date: Fri, 26 Apr 2019 15:07:34 -0300 Subject: [PATCH] Resend Verification Email Endpoint --- rest_auth/registration/serializers.py | 3 +++ rest_auth/registration/urls.py | 7 ++++++- rest_auth/registration/views.py | 20 ++++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/rest_auth/registration/serializers.py b/rest_auth/registration/serializers.py index 4f99c18..6b98541 100644 --- a/rest_auth/registration/serializers.py +++ b/rest_auth/registration/serializers.py @@ -215,3 +215,6 @@ class RegisterSerializer(serializers.Serializer): class VerifyEmailSerializer(serializers.Serializer): key = serializers.CharField() + +class ResendEmailVerificationSerializer(serializers.Serializer): + email = serializers.EmailField() \ No newline at end of file diff --git a/rest_auth/registration/urls.py b/rest_auth/registration/urls.py index 1004695..ffc3da7 100644 --- a/rest_auth/registration/urls.py +++ b/rest_auth/registration/urls.py @@ -1,10 +1,15 @@ from django.views.generic import TemplateView from django.conf.urls import url -from .views import RegisterView, VerifyEmailView +from .views import ( + RegisterView, + VerifyEmailView, + ResendEmailVerification +) urlpatterns = [ url(r'^$', RegisterView.as_view(), name='rest_register'), + url(r'^resend-verification-email/$', ResendEmailVerification.as_view(), name='rest_resend_verification_email'), url(r'^verify-email/$', VerifyEmailView.as_view(), name='rest_verify_email'), # This url is used by django-allauth and empty TemplateView is diff --git a/rest_auth/registration/views.py b/rest_auth/registration/views.py index 0e0ab0d..7823198 100644 --- a/rest_auth/registration/views.py +++ b/rest_auth/registration/views.py @@ -14,6 +14,7 @@ from rest_framework import status from allauth.account.adapter import get_adapter from allauth.account.views import ConfirmEmailView from allauth.account.utils import complete_signup +from allauth.account.models import EmailAddress from allauth.account import app_settings as allauth_settings from allauth.socialaccount import signals from allauth.socialaccount.adapter import get_adapter as get_social_adapter @@ -24,6 +25,7 @@ from rest_auth.app_settings import (TokenSerializer, create_token) from rest_auth.models import TokenModel from rest_auth.registration.serializers import (VerifyEmailSerializer, + ResendEmailVerificationSerializer, SocialLoginSerializer, SocialAccountSerializer, SocialConnectSerializer) @@ -98,6 +100,24 @@ class VerifyEmailView(APIView, ConfirmEmailView): return Response({'detail': _('ok')}, status=status.HTTP_200_OK) +class ResendEmailVerification(GenericAPIView): + serializer_class = ResendEmailVerificationSerializer + permission_classes = (AllowAny,) + allowed_methods = ('POST', 'OPTIONS', 'HEAD') + + def post(self, request, *args, **kwargs): + serializer = self.get_serializer(data=request.data) + serializer.is_valid(raise_exception=True) + email = serializer.data['email'] + + try: + email_address = EmailAddress.objects.get(email__exact=email, verified=False) + email_address.send_confirmation(self.request, True) + except EmailAddress.DoesNotExist: + pass + + return Response({'detail': _('Verification e-mail sent.')}) + class SocialLoginView(LoginView): """ class used for social authentications