Created
September 26, 2020 09:01
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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