1
votes

A default behavior of django-allauth is redirect to Singup form when the email retrieved from a social profile matches already existing user's emailid.

Instead, I would like to redirect the user back to the login page with the following message in case of the matching emailid on social login:

An account already exists with this "[email protected]" e-mail address. Please sign in to that account first, then connect your "PROVIDER" account. You can sign in using "LIST OF LINKED TO THAT EMAIL PROVIDERS".

Has anybody made something similar? Any help is appreciated.

1
Hey, were you able to achieve the above functionality?PythonEnthusiast
Have similar issue. Have you found a solution? Does it allow you to input another email and then produces 'Integrity error, Duplicate entry...'? @PythonEnthusiast maybe a solution from you? :)horbor

1 Answers

0
votes

To redirect user before sign-up you need to override allauth.socialaccount.views.SignupView. Simply alter you main urls.py settings:

# urls.py
from users.views import CustomSignupView

urlpatterns = [
        # ...
        # Your custom SignupView:
        url(r'^accounts/social/signup/$', CustomSignupView.as_view(), name='socialaccount_signup'),
        url(r'^accounts/', include('allauth.urls')),
        # ...
    ]

CustomSignupView may look like this:

# your_app.views.py
from allauth.socialaccount.views import SignupView
from django.contrib import messages
from django.shortcuts import redirect
from django.urls import reverse
from django.utils.translation import ugettext as _


class CustomSignupView(SignupView):
    http_method_names = ['get']

    def dispatch(self, request, *args, **kwargs):
        return super().dispatch(request, *args, **kwargs)

    def get(self, request, *args, **kwargs):
        social_login_email: str = self.sociallogin.user.email
        provider: str = self.sociallogin.account.provider

        messages.warning(self.request, _(f"An account already exists with this "
                                         f"\"{social_login_email}\" e-mail address. "
                                         f"Please sign in to that account first, "
                                         f"then connect your \"{provider.capitalize()}\" account."))

        return redirect(reverse("account_login"), permanent=False)

This custom view simply forbids all HTTP methods except the GET method (which redirects the user).

This solution works only when SOCIALACCOUNT_AUTO_SIGNUP is set to True (True is set by default, see https://django-allauth.readthedocs.io/en/latest/configuration.html). With this setting, the sign-up form is displayed only when there is e-mail address conflict -> then redirect the user when SignupView is loaded.

Some background: I had a similar problem as mentioned by @horbor in the comment. After the user enters another email (if an email from an external provider is already in use), I get IntegrityError on SignupView. I still do not know if there is a problem with allauth or with my application. However, bypassing signup is the best solution for me.