django-allauth social account connect to existing account on login

后端 未结 3 1554
无人及你
无人及你 2020-12-13 16:31

I have a custom user model and I am using django-allauth for social registration and login. I am trying to connect existing user to new social account when a user login usi

3条回答
  •  春和景丽
    2020-12-13 16:53

    I managed to get this working by changing the code for adapter a little bit.

    adapter.py

    from allauth.socialaccount.adapter import DefaultSocialAccountAdapter
    
    class MySocialAccountAdapter(DefaultSocialAccountAdapter):
        def pre_social_login(self, request, sociallogin): 
            user = sociallogin.user
            if user.id:  
                return          
            try:
                customer = Customer.objects.get(email=user.email)  # if user exists, connect the account to the existing account and login
                sociallogin.state['process'] = 'connect'                
                perform_login(request, customer, 'none')
            except Customer.DoesNotExist:
                pass
    

    If subclassing DefaultSocialAccountAdapter, we have to specify SOCIALACCOUNT_ADAPTER = 'myapp.my_adapter.MySocialAccountAdapter' in settings.py file

提交回复
热议问题