33from rest_framework .response import Response
44from rest_framework .permissions import AllowAny
55from rest_framework import status
6+ from rest_framework .authtoken .models import Token
67
78from allauth .account .views import SignupView , ConfirmEmailView
89from allauth .account .utils import complete_signup
910from allauth .account import app_settings
1011
11- from rest_auth .app_settings import UserDetailsSerializer
12+ from rest_auth .app_settings import UserDetailsSerializer , TokenSerializer
1213from rest_auth .registration .serializers import SocialLoginSerializer
1314from rest_auth .views import Login
1415
1516
1617class Register (APIView , SignupView ):
18+ """
19+ Accepts the credentials and creates a new user
20+ if user does not exist already
21+ Return the REST Token if the credentials are valid and authenticated.
22+ Calls allauth complete_signup method
23+
24+ Accept the following POST parameters: username, email, password
25+ Return the REST Framework Token Object's key.
26+ """
1727
1828 permission_classes = (AllowAny ,)
19- user_serializer_class = UserDetailsSerializer
29+ # user_serializer_class = UserDetailsSerializer
2030 allowed_methods = ('POST' , 'OPTIONS' , 'HEAD' )
31+ token_model = Token
32+ serializer_class = TokenSerializer
2133
2234 def get (self , * args , ** kwargs ):
2335 return Response ({}, status = status .HTTP_405_METHOD_NOT_ALLOWED )
@@ -27,6 +39,9 @@ def put(self, *args, **kwargs):
2739
2840 def form_valid (self , form ):
2941 self .user = form .save (self .request )
42+ self .token , created = self .token_model .objects .get_or_create (
43+ user = self .user
44+ )
3045 if isinstance (self .request , HttpRequest ):
3146 request = self .request
3247 else :
@@ -47,7 +62,8 @@ def post(self, request, *args, **kwargs):
4762 return self .get_response_with_errors ()
4863
4964 def get_response (self ):
50- serializer = self .user_serializer_class (instance = self .user )
65+ # serializer = self.user_serializer_class(instance=self.user)
66+ serializer = self .serializer_class (instance = self .token )
5167 return Response (serializer .data , status = status .HTTP_201_CREATED )
5268
5369 def get_response_with_errors (self ):
@@ -72,11 +88,25 @@ def post(self, request, *args, **kwargs):
7288class SocialLogin (Login ):
7389 """
7490 class used for social authentications
75- example usage for facebook
91+ example usage for facebook with access_token
92+ -------------
93+ from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter
7694
95+ class FacebookLogin(SocialLogin):
96+ adapter_class = FacebookOAuth2Adapter
97+ -------------
98+
99+ example usage for facebook with code
100+
101+ -------------
77102 from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter
103+ from allauth.socialaccount.providers.oauth2.client import OAuth2Client
104+
78105 class FacebookLogin(SocialLogin):
79106 adapter_class = FacebookOAuth2Adapter
107+ client_class = OAuth2Client
108+ callback_url = 'localhost:8000'
109+ -------------
80110 """
81111
82112 serializer_class = SocialLoginSerializer
0 commit comments