Fix social login under database constraints #553
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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).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.
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 馃檶