From e58c970028a4cbb0ba7c45e6b0b4eaabf1758d81 Mon Sep 17 00:00:00 2001 From: Clemens Wolff Date: Fri, 27 Oct 2023 19:39:41 +0200 Subject: [PATCH] Fix integrity error on database constraints (#553) --- dj_rest_auth/registration/serializers.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/dj_rest_auth/registration/serializers.py b/dj_rest_auth/registration/serializers.py index 0f14b570..db095cfc 100644 --- a/dj_rest_auth/registration/serializers.py +++ b/dj_rest_auth/registration/serializers.py @@ -1,6 +1,7 @@ from allauth.socialaccount.providers.oauth2.client import OAuth2Error from django.contrib.auth import get_user_model from django.core.exceptions import ValidationError as DjangoValidationError +from django.db import IntegrityError from django.http import HttpRequest, HttpResponseBadRequest from django.urls.exceptions import NoReverseMatch from django.utils.translation import gettext_lazy as _ @@ -180,7 +181,12 @@ def validate(self, attrs): ) login.lookup() - login.save(request, connect=True) + try: + login.save(request, connect=True) + except IntegrityError as ex: + raise serializers.ValidationError( + _('User is already registered with this e-mail address.'), + ) from ex self.post_signup(login, attrs) attrs['user'] = login.account.user