django allauth facebook redirects to signup when retrieved email matches an existing user's email? django allauth facebook redirects to signup when retrieved email matches an existing user's email? django django

django allauth facebook redirects to signup when retrieved email matches an existing user's email?


I have solved it after digging in deep into google and source code of django and django-allauth

Problem being solved: I just want the ability to interchangeably login using facebook and google with same email id and always redirect to LOGIN_REDIRECT_URL after successful login, but django-allauth doesn't let me do that. Instead, it presents me with a signup page which I don't want.

Solution:: Use @receiver(pre_social_login) to call a function link_to_local_user() which logs in 1st and then raises ImmediateHttpResponse which in turn redirects to LOGIN_REDIRECT_URL

#! myproject.adapter.pyfrom allauth.account.adapter import DefaultAccountAdapterfrom allauth.socialaccount.adapter import DefaultSocialAccountAdapterfrom allauth.exceptions import ImmediateHttpResponsefrom allauth.socialaccount.signals import pre_social_loginfrom allauth.account.utils import perform_loginfrom allauth.utils import get_user_modelfrom django.http import HttpResponsefrom django.dispatch import receiverfrom django.shortcuts import redirectfrom django.conf import settingsimport jsonclass MyLoginAccountAdapter(DefaultAccountAdapter):    '''    Overrides allauth.account.adapter.DefaultAccountAdapter.ajax_response to avoid changing    the HTTP status_code to 400    '''    def get_login_redirect_url(self, request):        """         """        if request.user.is_authenticated():            return settings.LOGIN_REDIRECT_URL.format(                id=request.user.id)        else:            return "/"class MySocialAccountAdapter(DefaultSocialAccountAdapter):    '''    Overrides allauth.socialaccount.adapter.DefaultSocialAccountAdapter.pre_social_login to     perform some actions right after successful login    '''    def pre_social_login(self, request, sociallogin):        pass    # TODOFuture: To perform some actions right after successful login@receiver(pre_social_login)def link_to_local_user(sender, request, sociallogin, **kwargs):    ''' Login and redirect    This is done in order to tackle the situation where user's email retrieved    from one provider is different from already existing email in the database    (e.g facebook and google both use same email-id). Specifically, this is done to    tackle following issues:    * https://github.com/pennersr/django-allauth/issues/215    '''    email_address = sociallogin.account.extra_data['email']    User = get_user_model()    users = User.objects.filter(email=email_address)    if users:        # allauth.account.app_settings.EmailVerificationMethod        perform_login(request, users[0], email_verification='optional')        raise ImmediateHttpResponse(redirect(settings.LOGIN_REDIRECT_URL.format(id=request.user.id)))#! settings.pyACCOUNT_AUTHENTICATION_METHOD = "email" # Defaults to username_emailACCOUNT_USERNAME_REQUIRED = False       # Defaults to TrueACCOUNT_EMAIL_REQUIRED = True           # Defaults to FalseSOCIALACCOUNT_QUERY_EMAIL = ACCOUNT_EMAIL_REQUIREDSOCIALACCOUNT_AUTO_SIGNUP = TrueSOCIALACCOUNT_EMAIL_REQUIRED = FalseACCOUNT_ADAPTER = "myproject.adapter.MyLoginAccountAdapter"SOCIALACCOUNT_ADAPTER = 'myproject.adapter.MySocialAccountAdapter'LOGIN_URL = "/"LOGIN_REDIRECT_URL = "/users/{id}/mytags"


this was happened with me and the reason was after submitting access_token to Facebook again to get the data determined on scope don't return any email or user name.

  1. you need to make sure that the face_book configuration on settings.py has scope at least as follow 'SCOPE': ['email', 'public_profile'],
  2. got to apps setting in facebook developers make email granted for users