Add hook for social account registration #448
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.
What does this pull request do?
This pull request adds a hook to the social registration serializer to inject optional custom the behavior when the user signs up with a social provider.
Why is this useful?
I have a registration flow that requires additional information to be sent by the client so that the backend can take certain actions after the user signed up. I implemented this by subclassing the registration serializers and adding my required additional fields.
However, there currently is no clean way to run custom logic after the social account has been registered if this logic needs access to the request data. The django-allauth package does provide the
save_user
hook in theDefaultSocialAccountAdapter
, but this hook doesn't have access to the serializer and re-parsing the request body fails in the ASGI context as the request stream has already been consumed by the serializer.As a work-around, I currently copy the entire
validate
method into my subclass to inject my custom logic but given the amount of code that's present in the method, this isn't a long-term maintainable solution as it's very brittle to changes in the dj-rest-auth package. Adding a new lifecycle hook that fires after the registration is complete enables my use-case in a maintainable way and I can easily see this enabling many other use-cases in the future.