2015-03-06 10 views
7

पर मौजूदा खाते से कनेक्ट मैं एक कस्टम उपयोगकर्ता मॉडल है और मैं सामाजिक पंजीकरण और प्रवेश के लिए Django-allauth उपयोग कर रहा हूँ। मैं मौजूदा उपयोगकर्ता को नए सोशल अकाउंट से कनेक्ट करने की कोशिश कर रहा हूं, जब कोई उपयोगकर्ता किसी ऐसे सामाजिक खाते का उपयोग कर लॉगिन करता है जो पहले ही ईमेल का उपयोग कर पंजीकृत है। मुझे यह link मिला।Django-allauth सामाजिक खाते में प्रवेश

def pre_social_login(self, request, sociallogin): 
    user = sociallogin.account.user 
    if user.id: 
     return 
    try: 
     customer = Customer.objects.get(email=user.email) 
    except Customer.DoesNotExist: 
     pass 
    else: 
     perform_login(request, customer, 'none') 

लेकिन जब मैं सामाजिक खाते के माध्यम से लॉगिन करने का प्रयास करता हूं तो मुझे एक त्रुटि मिल रही है।

RelatedObjectDoesNotExist at /accounts/facebook/login/callback/ 
SocialAccount has no user. 

किसी भी मदद की सराहना की जाएगी।

इसके अलावा, मैं इस में सुरक्षा समस्या के बारे में पता कर रहा हूँ। लेकिन मैं अभी भी यह कोशिश करना चाहता हूं।

+0

आप कस्टम उपयोगकर्ता मॉडल और Django-allauth के बारे में अनुभाग पढ़ें किया? http://django-allauth.readthedocs.org/en/latest/advanced.html#custom-user-models – petkostas

उत्तर

15

मैं एडाप्टर के लिए एक छोटा सा कोड बदल कर इस काम कर पाने में कामयाब रहे।

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 

तो उपवर्गीकरण DefaultSocialAccountAdapter, हम settings.py फ़ाइल

+0

कहाँ ग्राहक वर्ग है ?? –

+1

ग्राहक वर्ग सिर्फ एक उदाहरण है। मैंने django के सार BaseUser के साथ एक कस्टम उपयोगकर्ता मॉडल बनाया। – anupsabraham

+1

ठीक है..क्या आप यह भी बता सकते हैं कि आपने अलाउथ कोड में यह परिवर्तन किया है या आपने एक अलग वर्ग बनाया है? –

0

में SOCIALACCOUNT_ADAPTER = 'myapp.my_adapter.MySocialAccountAdapter' मैं निम्नलिखित समाधान here भी जांच होती है कि कि ईमेल पते सत्यापित कर रहे हैं पाया निर्दिष्ट करना होगा।

from allauth.account.models import EmailAddress 

def pre_social_login(self, request, sociallogin): 

     # social account already exists, so this is just a login 
     if sociallogin.is_existing: 
      return 

     # some social logins don't have an email address 
     if not sociallogin.email_addresses: 
      return 

     # find the first verified email that we get from this sociallogin 
     verified_email = None 
     for email in sociallogin.email_addresses: 
      if email.verified: 
       verified_email = email 
       break 

     # no verified emails found, nothing more to do 
     if not verified_email: 
      return 

     # check if given email address already exists as a verified email on 
     # an existing user's account 
     try: 
      existing_email = EmailAddress.objects.get(email__iexact=email.email, verified=True) 
     except EmailAddress.DoesNotExist: 
      return 

     # if it does, connect this new social login to the existing user 
     sociallogin.connect(request, existing_email.user) 

अगर आप सत्यापन चरण को छोड़ना चाहते हैं, मुझे लगता है कि इस समाधान अभी भी एक सा बेहतर है:

def pre_social_login(self, request, sociallogin): 

    user = sociallogin.user 
    if user.id: 
     return 
    if not user.email: 
     return 

    try: 
     user = User.objects.get(email=user.email) # if user exists, connect the account to the existing account and login 
     sociallogin.connect(request, user) 
    except User.DoesNotExist: 
     pass 
संबंधित मुद्दे