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 using a social account who already has registered using email. I found this link.
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')
But I'm getting an error when I try to login through social account.
RelatedObjectDoesNotExist at /accounts/facebook/login/callback/
SocialAccount has no user.
Any help will be appreciated.
Also I am aware of the security issue in this. But I still want to try this.
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
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
It's implemented out of the box with the latest allauth
. You can use the following in your templates:
{% load socialaccount %}
<a href="{% provider_login_url "spotify" process="connect" %}">connect spotify account</a>
The URL is the following:
/accounts/spotify/login/?process=connect
No internal modifications needed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With