Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix social login under database constraints #553

Merged
merged 1 commit into from Oct 27, 2023
Merged

Fix social login under database constraints #553

merged 1 commit into from Oct 27, 2023

Conversation

c-w
Copy link
Contributor

@c-w c-w commented Sep 28, 2023

It's a common practice to model the user email as case insensitive by adding a unique constraint on the email field using something similar to the snippet below (as a matter of fact, allauth already models the EmailAddress as case-insensitive by using email__iexact everywhere for lookup).

# example of user model with case insensitive email
class User(AbstractUser):
    class Meta(AbstractUser.Meta):
        constraints = [
            models.UniqueConstraint(
                Upper("email"),
                name="user_email_unique_index",
            ),
        ]

In the case insensitive scenario, dj-rest-auth currently crashes during social signup with an integrity error as the account existence check (see snippet below) doesn't consider a case insensitive scenario.

# current existence check: doesn't consider case sensitivity
account_exists = get_user_model().objects.filter(
    email=login.user.email,
).exists()

More generally, the current approach also has a race condition where we will crash with an integrity error if the existence check is still running while another request logs in with the same email address.

To solve the issues outlined above, this PR proposes to add an additional check around the login save method to catch the integrity error: if such an error occurs, we failed to save the user thus the user must be a duplicate.

More generally, I opened this pull request to draw attention to the described issues. I'm not particularly wedded to this implementation but I'd like to open a discussion about how to best address this scenario and work on the best fix together. For example, an alternative implementation could factor out the email existence check into a method so that subclasses can override the logic to for example inject a case-insensitive check.

Looking forward to your inputs 馃檶

@iMerica iMerica merged commit e58c970 into iMerica:master Oct 27, 2023
2 of 3 checks passed
@c-w c-w deleted the fix-case-insensitive-email branch October 27, 2023 18:12
@c-w
Copy link
Contributor Author

c-w commented Oct 27, 2023

Thanks for the merge 馃檶

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants