Skip to content

Instantly share code, notes, and snippets.

@dhvcc
Created September 26, 2020 09:01
Show Gist options
  • Save dhvcc/1521478609f1c349b3092bcf8d6b3f9e to your computer and use it in GitHub Desktop.
Save dhvcc/1521478609f1c349b3092bcf8d6b3f9e to your computer and use it in GitHub Desktop.
Allauth pre social login signal handler to avoid sign up (and verify e-mail) if account exists
from allauth.account.utils import perform_login
from allauth.exceptions import ImmediateHttpResponse
from allauth.socialaccount.signals import pre_social_login
from allauth.utils import get_user_model
from django.dispatch import receiver
from django.shortcuts import redirect
from django.conf import settings
@receiver(pre_social_login)
def pre_social_login_handler(request, sociallogin, **kwargs):
"""
Take email out and try to find user with the same email
If the user exists, verify email if needed, login and redirect to home
"""
email_address = sociallogin.account.extra_data["email"]
user_model = get_user_model()
users = user_model.objects.filter(email=email_address)
if users:
user = users[0]
# allauth.account.models.EmailAddress
email_addr_object = user.emailaddress_set.first()
if not email_addr_object.verified:
# Verify if needed
email_addr_object.verified = True
email_addr_object.save()
# allauth.account.app_settings.EmailVerificationMethod
perform_login(request, user, email_verification=settings.ACCOUNT_EMAIL_VERIFICATION)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment