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.