# Duplicate Email Address in Django AllAuth

**URL:** <https://forum.djangoproject.com/t/duplicate-email-address-in-django-allauth/19064>\
**Category:** Forms & APIs\
**Created:** [February 23, 2023, 1:54pm UTC](https://forum.djangoproject.com/t/duplicate-email-address-in-django-allauth/19064 "2023-02-23T13:54:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![firaki12345](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/firaki12345/32/4251_2.png) [@firaki12345](https://forum.djangoproject.com/u/firaki12345)\
**Post date:** [February 23, 2023, 1:54pm UTC](https://forum.djangoproject.com/t/duplicate-email-address-in-django-allauth/19064/1 "2023-02-23T13:54:30Z")

</div>

So i succesfully signed up a user through Github, via **django All-auth**

now the problem is when github grants access to the User, it takes them to a page in my website that tells them to sign up using thier github username and email address.

**the only problem is that the email address already exists in my database,**

so when i click on sign up, it throws a duplicate integrity check error. now how to solve this and make the user automatically login when thier email address already exists in the database.

**Error**

```auto
# IntegrityError at /accounts/social/signup/

duplicate key value violates unique constraint "account_emailaddress_email_key" DETAIL: Key (email)=(abcd.net@gmail.com) already exists.

|Request Method:|POST|
| --- | --- |
|Request URL:|http://0.0.0.0:8000/accounts/social/signup/|
|Django Version:|4.1.7|
|Exception Type:|IntegrityError|

|Exception Value:|duplicate key value violates unique constraint "account_emailaddress_email_key" DETAIL: Key (email)=(abcd@gmail.com) already exists.|

|Exception Location:|/home/firaki/.local/lib/python3.10/site-packages/django/db/backends/utils.py, line 89, in _execute|

|Raised during:|allauth.socialaccount.views.SignupView|

```

**so the error is raised in this views file**

`allauth.socialaccount.views.SignupView`

**i have located it and i changed the form\_valid method, but it still raises the ERROR**

**Changed it from THIS**

```auto
    def form_valid(self, form):
        self.request.session.pop("socialaccount_sociallogin", None)
        form.save(self.request)
        return helpers.complete_social_signup(self.request, self.sociallogin)

```

**TO THIS BELOW**

```auto
     def form_valid(self, form):
        email = form.cleaned_data['email']
        print(email)
        if User.objects.filter(email=email).exists():
            # Email already exists, log in the user and redirect to home page
            user = User.objects.get(email=email)
            get_account_adapter().add_message(
                self.request,
                messages.INFO,
                "You were automatically logged in as your email already exists.",
            )
            auth_login(self.request, user, account_settings.AUTHENTICATION_BACKENDS[0])
            return HttpResponseRedirect(reverse('home'))
        else:
            # Email does not exist, create a new user
            self.request.session.pop('socialaccount_sociallogin', None)
            form.save(self.request)
            return helpers.complete_social_signup(self.request, self.sociallogin)

```

**Thanks**

---

<div class="post-metadata">

**Author:** ![czue](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/czue/32/1387_2.png) [@czue](https://forum.djangoproject.com/u/czue)\
**Post date:** [February 23, 2023, 2:13pm UTC](https://forum.djangoproject.com/t/duplicate-email-address-in-django-allauth/19064/2 "2023-02-23T14:13:36Z")

</div>

It sounds like you might be encountering this issue: [Integrity error during social login: duplicate key violates unique constraint on user table · Issue #3266 · pennersr/django-allauth · GitHub](https://github.com/pennersr/django-allauth/issues/3266).

A workaround is to use one of the suggested fixes in your own signup form. E.g.

```auto
class CustomSocialSignupForm(SocialSignupForm):
    """Custom social signup form to work around this issue:
    https://github.com/pennersr/django-allauth/issues/3266"""
    def __init__ (self, *args, **kwargs):
        super(). __init__ (*args, **kwargs)
        self.prevent_enumeration = False

```

Then in `settings.py`

```auto
SOCIALACCOUNT_FORMS = {
    "signup": "apps.users.forms.CustomSocialSignupForm",
}

```

---

<div class="post-metadata">

**Author:** ![firaki12345](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/firaki12345/32/4251_2.png) [@firaki12345](https://forum.djangoproject.com/u/firaki12345)\
**Post date:** [February 23, 2023, 2:25pm UTC](https://forum.djangoproject.com/t/duplicate-email-address-in-django-allauth/19064/3 "2023-02-23T14:25:05Z")

</div>

i am sorry , i am a bit confused where should i add this form, or create it in the forms.py file ?

as i dont have a custom sign up form, django alluth does it for me

sorry for the noob question

---

<div class="post-metadata">

**Author:** ![czue](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/czue/32/1387_2.png) [@czue](https://forum.djangoproject.com/u/czue)\
**Post date:** [February 23, 2023, 2:42pm UTC](https://forum.djangoproject.com/t/duplicate-email-address-in-django-allauth/19064/4 "2023-02-23T14:42:14Z")

</div>

you can put it anywhere, it just has to be referenced by the same path in settings

---

<div class="post-metadata">

**Author:** ![firaki12345](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/firaki12345/32/4251_2.png) [@firaki12345](https://forum.djangoproject.com/u/firaki12345)\
**Post date:** [February 23, 2023, 3:02pm UTC](https://forum.djangoproject.com/t/duplicate-email-address-in-django-allauth/19064/5 "2023-02-23T15:02:34Z")

</div>

i am sorry but the social signup form does not exist

```auto
    from allauth.socialaccount.forms import SocialSignupForm
ImportError: cannot import name 'SocialSignupForm' from 'allauth.socialaccount.forms

```

maybe it has a differnt name ?

---

<div class="post-metadata">

**Author:** ![firaki12345](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/firaki12345/32/4251_2.png) [@firaki12345](https://forum.djangoproject.com/u/firaki12345)\
**Post date:** [February 23, 2023, 3:09pm UTC](https://forum.djangoproject.com/t/duplicate-email-address-in-django-allauth/19064/6 "2023-02-23T15:09:14Z")

</div>

alright thanks you so much i solved it 🙂

its SignupForm instead of SocialSignupForm

thanks so much

now only one last thing, i found another buggy error is that

when i sign up normally but use the same email address, it still sends an email address to my email , but the email message is that somebody try to sign up using your email address,

**how can i just stop them at the sign up page that the email already exists**
