question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Connect functionality for social account linking

See original GitHub issue

Hi,

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:closed
  • Created 6 years ago
  • Reactions:37
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
aleksihaklicommented, Nov 19, 2017

I hoped the authors would comment on this but I will try and open a PR for this today 😃

1reaction
Mouldecommented, Nov 28, 2017

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found