diff --git a/docs/configuration.rst b/docs/configuration.rst index 59b301f..aef29e1 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -51,3 +51,5 @@ Configuration - **OLD_PASSWORD_FIELD_ENABLED** - set it to True if you want to have old password verification on password change enpoint (default: False) - **LOGOUT_ON_PASSWORD_CHANGE** - set to False if you want to keep the current user logged in after a password change + +- **REST_AUTH_ROLL_BACK_REGISTER_ON_ERROR** - set to True to prevent a user being created if an error occurs after writing the user (such as during the sending of the verification email). diff --git a/rest_auth/registration/app_settings.py b/rest_auth/registration/app_settings.py index c8cd25a..ea7f956 100644 --- a/rest_auth/registration/app_settings.py +++ b/rest_auth/registration/app_settings.py @@ -17,3 +17,7 @@ def register_permission_classes(): for klass in getattr(settings, 'REST_AUTH_REGISTER_PERMISSION_CLASSES', tuple()): permission_classes.append(import_callable(klass)) return tuple(permission_classes) + +roll_back_register_on_error = getattr(settings, + 'REST_AUTH_ROLL_BACK_REGISTER_ON_ERROR', + False) diff --git a/rest_auth/registration/views.py b/rest_auth/registration/views.py index 0e0ab0d..69234e3 100644 --- a/rest_auth/registration/views.py +++ b/rest_auth/registration/views.py @@ -1,4 +1,5 @@ from django.conf import settings +from django.db import transaction from django.utils.decorators import method_decorator from django.utils.translation import ugettext_lazy as _ from django.views.decorators.debug import sensitive_post_parameters @@ -29,7 +30,9 @@ from rest_auth.registration.serializers import (VerifyEmailSerializer, SocialConnectSerializer) from rest_auth.utils import jwt_encode from rest_auth.views import LoginView -from .app_settings import RegisterSerializer, register_permission_classes +from .app_settings import (RegisterSerializer, + register_permission_classes, + roll_back_register_on_error) sensitive_post_parameters_m = method_decorator( sensitive_post_parameters('password1', 'password2') @@ -82,6 +85,12 @@ class RegisterView(CreateAPIView): return user +if roll_back_register_on_error: + RegisterView.perform_create = transaction.atomic( + RegisterView.perform_create + ) + + class VerifyEmailView(APIView, ConfirmEmailView): permission_classes = (AllowAny,) allowed_methods = ('POST', 'OPTIONS', 'HEAD')