Commit 6247d53c authored by Luis García's avatar Luis García
Browse files

Login with Facebook and Twitter

parent 08355a9c
......@@ -29,7 +29,7 @@ from api.urls import api_patterns
from django.views.i18n import JavaScriptCatalog
from registration.views import CustomActivationView
from core.views.social_auth import FacebookLogin, TwitterLogin
from core.views.social_auth import FacebookLogin, TwitterLogin, FacebookConnect, TwitterConnect
"""urlpatterns = [
path('page/', include(pages_patterns)),
......@@ -55,8 +55,9 @@ urlpatterns += [
url(r'^oauth/', include('social_django.urls', namespace='social')),
url(r'^rest-auth/', include('rest_auth.urls')),
url(r'^rest-auth/facebook/$', FacebookLogin.as_view(), name='fb_login'),
url(r'^rest-auth/twitter/$', TwitterLogin.as_view(), name='twitter_login')
#url(r'^auth/', include('rest_framework_social_oauth2.urls')),
url(r'^rest-auth/twitter/$', TwitterLogin.as_view(), name='twitter_login'),
url(r'^rest-auth/facebook/connect/$', FacebookConnect.as_view(), name='fb_connect'),
url(r'^rest-auth/twitter/connect/$', TwitterConnect.as_view(), name='twitter_connect'),
]
"""urlpatterns += i18n_patterns(
......
from django.http import HttpResponseForbidden
from django.core.exceptions import PermissionDenied
from core.models import Profile
class StafRequiredMixin(object):
def dispatch(self, request, *args, **kwargs):
......@@ -15,4 +16,17 @@ class SuperuserRequiredMixin(object):
if user:
if user.is_superuser:
return super(SuperuserRequiredMixin, self).dispatch(request, *args, **kwargs)
return HttpResponseForbidden("You don't have access to this page!")
\ No newline at end of file
return HttpResponseForbidden("You don't have access to this page!")
class SocialLoginMixin(object):
def post(self, request, *args, **kwargs):
self.request = request
self.serializer = self.get_serializer(data=self.request.data, context={'request': request})
self.serializer.is_valid(raise_exception=True)
self.login()
#Validate if the user have a profile
profile = Profile.objects.filter(user = self.user)
if len(profile) == 0:
p = Profile(age=0, gender='x', user = self.user)
p.save()
return self.get_response()
\ No newline at end of file
from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter
from allauth.socialaccount.providers.twitter.views import TwitterOAuthAdapter
from rest_auth.registration.views import SocialLoginView
from rest_auth.registration.views import SocialLoginView, SocialConnectView
from rest_auth.social_serializers import TwitterLoginSerializer
from rest_auth.social_serializers import TwitterConnectSerializer
from core.mixins import SocialLoginMixin
class FacebookLogin(SocialLoginView):
class FacebookLogin(SocialLoginMixin, SocialLoginView):
adapter_class = FacebookOAuth2Adapter
from rest_auth.registration.views import SocialLoginView
class TwitterLogin(SocialLoginView):
class TwitterLogin(SocialLoginMixin, SocialLoginView):
serializer_class = TwitterLoginSerializer
adapter_class = TwitterOAuthAdapter
class FacebookConnect(SocialConnectView):
adapter_class = FacebookOAuth2Adapter
class TwitterConnect(SocialConnectView):
serializer_class = TwitterConnectSerializer
adapter_class = TwitterOAuthAdapter
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment