django-allauth social account connect to existing account on login

后端 未结 3 1558
无人及你
无人及你 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:39

    I found the following solution here that also checks that the email addresses are verified.

    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)
    

    if you prefer to skip the verification step, I think this solution is still a bit better:

    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
    

提交回复
热议问题