-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from andela/ft-enable-social-login-160617671
#160617671: Enable Social login via Google, Facebook and Twitter
- Loading branch information
Showing
9 changed files
with
136 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from rest_framework import serializers | ||
from authors.apps.authentication.serializers import RegistrationSerializer,LoginSerializer | ||
from authors.apps.authentication.models import User | ||
|
||
class FbRegisterSerializer(RegistrationSerializer): | ||
def create(self, validated_data): | ||
|
||
user = User.objects.create_user(**validated_data) | ||
user.is_verified =True | ||
user.save() | ||
return user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from django.urls import path | ||
from .views import FacebookLogin, TwitterLogin | ||
|
||
|
||
|
||
app_name = 'social_auth' | ||
|
||
urlpatterns = [ | ||
path('facebook/', FacebookLogin.as_view(), name='login'), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter | ||
from allauth.socialaccount.providers.twitter.views import TwitterOAuthAdapter | ||
from allauth.socialaccount.providers.google.views import GoogleOAuth2Adapter | ||
from allauth.socialaccount.providers.oauth2.client import OAuth2Client | ||
from rest_auth.registration.views import SocialLoginView | ||
import requests | ||
from rest_framework import status | ||
from authors.apps.authentication.models import User | ||
|
||
from rest_framework.response import Response | ||
from rest_auth.social_serializers import TwitterLoginSerializer | ||
from authors.apps.authentication.serializers import RegistrationSerializer,LoginSerializer | ||
from .serializers import FbRegisterSerializer | ||
import json | ||
|
||
class FacebookLogin(SocialLoginView): | ||
adapter_class = FacebookOAuth2Adapter | ||
client_class = OAuth2Client | ||
FbRegSerializer_class=FbRegisterSerializer | ||
|
||
def post(self, request): | ||
data=request.data | ||
token=data['access_token'] | ||
url = "https://graph.facebook.com/v3.1/me" | ||
|
||
querystring = {"fields":"id,name,email,first_name,last_name","access_token":token} | ||
response = requests.request("GET", url,params=querystring) | ||
i=json.loads(response.text) | ||
|
||
data={ | ||
"password": 'Ah123456789@', | ||
"username": i['name'], | ||
"email": i['email'], | ||
} | ||
|
||
|
||
if not User.objects.filter(email=i['email']).exists(): | ||
|
||
serializer=self.FbRegSerializer_class(data=data) | ||
serializer.is_valid(raise_exception=True) | ||
serializer.save() | ||
|
||
else: | ||
loginserializer=LoginSerializer(data=data) | ||
loginserializer.is_valid(raise_exception=True) | ||
import pdb; pdb.set_trace() | ||
return Response("logged in") | ||
|
||
return Response(i, status=status.HTTP_200_OK) | ||
|
||
|
||
class GoogleLogin(SocialLoginView): | ||
adapter_class = GoogleOAuth2Adapter | ||
client_class = OAuth2Client | ||
callback_url = 'localhost:8000' | ||
|
||
|
||
class TwitterLogin(SocialLoginView): | ||
serializer_class = TwitterLoginSerializer | ||
adapter_class = TwitterOAuthAdapter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters