Connect functionality for social account linking
See original GitHub issueHi,
I recently wrote some account connection logic into our application with django-allauth
and django-rest-auth
for use with a mobile application and REST API.
I found out that it would be fairly easy to bundle connect and disconnect views in the application.
Should these serializers and views be included in the django-rest-auth
package?
from allauth.socialaccount.adapter import get_adapter
from allauth.socialaccount.models import SocialAccount
from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter
from rest_auth.registration.serializers import SocialLoginSerializer
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
###
# Social account connect functionality for django-rest-auth
class SocialConnectSerializer(SocialLoginSerializer):
"""Specialize social login process state to `connect`"""
def get_social_login(self, *args, **kwargs):
social_login = super().get_social_login(*args, **kwargs)
social_login.state['process'] = 'connect'
return social_login
class SocialConnectMixin(SocialLoginView):
"""Connect view for logged in users
The django-allauth module internally uses a request attribute
called 'process' to set the required action, we override that
in the SocialConnectSerializer for use with the REST API.
"""
serializer_class = SocialConnectSerializer
permission_classes = (IsAuthenticated, )
###
# Usage example of login and connect functionality for Facebook
class FacebookConfigurationMixin(object):
adapter_class = FacebookOAuth2Adapter
class FacebookLogin(FacebookConfigurationMixin, SocialLoginView):
"""Login user with given Facebook account"""
pass
class FacebookConnect(FacebookConfigurationMixin, SocialConnectMixin, SocialLoginView):
"""Connect given Facebook account for logged in user"""
pass
###
# SocialAccount viewing and disconnection
class SocialAccountSerializer(serializers.ModelSerializer):
class Meta:
model = SocialAccount
fields = (
'id',
'provider',
'uid',
'last_login',
'date_joined',
'extra_data',
)
class SocialAccountViewSet(GenericViewSet):
"""SocialAccount views for logged in users"""
permission_classes = (IsAuthenticated, )
serializer_class = SocialAccountSerializer
queryset = SocialAccount.objects.none()
def get_queryset(self):
return SocialAccount.objects.filter(user=self.request.user)
def list(self, request):
"""List SocialAccounts for currently logged in user"""
return Response(self.get_serializer(self.get_queryset(), many=True).data)
@detail_route(methods=['POST'])
def disconnect(self, request, pk):
"""Disconnect SocialAccount from remote service
This is the same logic as used in the web UI form,
refer to the allauth implementation for more details.
"""
accounts = self.get_queryset()
account = accounts.get(pk=pk)
get_adapter(self.request).validate_disconnect(account, accounts)
account.delete()
signals.social_account_removed.send(
sender=SocialAccount,
request=self.request,
socialaccount=account
)
return Response(self.get_serializer(account).data)
Issue Analytics
- State:
- Created 6 years ago
- Reactions:37
- Comments:5 (2 by maintainers)
Top Results From Across the Web
User Account Linking - Auth0
Auth0 supports the linking of user accounts from various identity providers . This allows a user to authenticate from any of their accounts...
Read more >Connecting social accounts - Squarespace Help Center
In this guide, you'll learn how to connect your Squarespace site to social networks, like Facebook, Instagram, and other online services.
Read more >SAP Customer Data Cloud - Linking Social Accounts
To enable social-to-social linking you need to add SAP Customer Data Cloud's social login widget to your existing link-accounts screen's markup, with the ......
Read more >How to Link Social Media Account(s) - All at Once - Social Pros
Here we are going to tell you how to link social media accounts if you are using one of the platforms, Facebook, Instagram,...
Read more >Authentication - FAQ - Account Linking - LoginRadius
Account Linking allows customers to connect all of the log in providers for a user into one centralized account. How Does it Work?...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I hoped the authors would comment on this but I will try and open a PR for this today 😃
Can anyone point me in the direction of a sample that shows how to use the above? I’m a bit confused about how I use the allauth providers, like github, with the django-rest-auth library.