mirror of
https://github.com/Tivix/django-rest-auth.git
synced 2024-11-22 09:06:40 +03:00
create base view and serializer for social authentication
This commit is contained in:
parent
ad1d189367
commit
85688940df
43
rest_auth/registration/serializers.py
Normal file
43
rest_auth/registration/serializers.py
Normal file
|
@ -0,0 +1,43 @@
|
|||
from rest_framework import serializers
|
||||
from requests.exceptions import HTTPError
|
||||
from allauth.socialaccount.helpers import complete_social_login
|
||||
|
||||
|
||||
class SocialLoginSerializer(serializers.Serializer):
|
||||
|
||||
access_token = serializers.CharField(required=True)
|
||||
|
||||
def validate_access_token(self, attrs, source):
|
||||
access_token = attrs[source]
|
||||
|
||||
view = self.context.get('view')
|
||||
request = self.context.get('request')
|
||||
|
||||
if not view:
|
||||
raise serializers.ValidationError('View is not defined, pass it as\
|
||||
a context variable')
|
||||
self.adapter_class = getattr(view, 'adapter_class', None)
|
||||
|
||||
if not self.adapter_class:
|
||||
raise serializers.ValidationError('Define adapter_class in view')
|
||||
|
||||
self.adapter = self.adapter_class()
|
||||
app = self.adapter.get_provider().get_app(request)
|
||||
token = self.adapter.parse_token({'access_token': access_token})
|
||||
token.app = app
|
||||
|
||||
try:
|
||||
login = self.adapter.complete_login(request, app, token,
|
||||
response=access_token)
|
||||
token.account = login.account
|
||||
login.token = token
|
||||
complete_social_login(request, login)
|
||||
except HTTPError:
|
||||
raise serializers.ValidationError('Incorrect value')
|
||||
|
||||
if not login.is_existing:
|
||||
login.lookup()
|
||||
login.save(request, connect=True)
|
||||
self.object = {'user': login.account.user}
|
||||
|
||||
return attrs
|
|
@ -8,6 +8,8 @@ from allauth.account.utils import complete_signup
|
|||
from allauth.account import app_settings
|
||||
|
||||
from rest_auth.serializers import UserDetailsSerializer
|
||||
from rest_auth.registration.serializers import SocialLoginSerializer
|
||||
from rest_auth.views import Login
|
||||
|
||||
|
||||
class Register(APIView, SignupView):
|
||||
|
@ -49,3 +51,16 @@ class VerifyEmail(APIView, ConfirmEmailView):
|
|||
confirmation = self.get_object()
|
||||
confirmation.confirm(self.request)
|
||||
return Response({'message': 'ok'}, status=status.HTTP_200_OK)
|
||||
|
||||
|
||||
class SocialLogin(Login):
|
||||
"""
|
||||
class used for social authentications
|
||||
example usage for facebook
|
||||
|
||||
from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter
|
||||
class FacebookLogin(SocialLogin):
|
||||
adapter_class = FacebookOAuth2Adapter
|
||||
"""
|
||||
|
||||
serializer_class = SocialLoginSerializer
|
||||
|
|
|
@ -48,13 +48,13 @@ class Login(LoggedOutRESTAPIView, GenericAPIView):
|
|||
response_serializer = TokenSerializer
|
||||
|
||||
def get_serializer(self):
|
||||
return self.serializer_class(data=self.request.DATA)
|
||||
return self.serializer_class(data=self.request.DATA,
|
||||
context={'request': self.request, 'view': self})
|
||||
|
||||
def login(self):
|
||||
self.user = self.serializer.object['user']
|
||||
self.token, created = self.token_model.objects.get_or_create(
|
||||
user=self.user)
|
||||
|
||||
if getattr(settings, 'REST_SESSION_LOGIN', True):
|
||||
login(self.request, self.user)
|
||||
|
||||
|
@ -244,3 +244,5 @@ class PasswordChange(LoggedInRESTAPIView, GenericAPIView):
|
|||
else:
|
||||
return Response(serializer.errors,
|
||||
status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user