# Path: server/apps/authentication/api_views.py
# class APILoginViewSet(APIView):
#     """
#     View to list all users in the system.
# 
#     * Requires token authentication.
#     * Only admin users are able to access this view.
#     """
#     #permission_classes = ()
# 
#     @csrf_exempt
#     def post(self, request, format=None):
#         """
#         Update thumbnail and tiny file field
#         """
#         data = JSONParser().parse(request)
#         serializer = LoginCustomSerializer(data=data)
# 
#         if serializer.is_valid():
#             email = serializer.data.get('email')
#             password = serializer.data.get('password')
# 
#             if not request.user.is_anonymous:
#                 return Response('Already Logged-in', status=status.HTTP_403_FORBIDDEN)
# 
#             account = authenticate(email=email, password=password)
# 
#             if account is not None:
#                 if account.is_active:
#                     login(request, account)
# 
#                     serialized = AccountSerializer(account)
#                     data = serialized.data
# 
#                     # Add the token to the return serialization
#                     try:
#                         token = Token.objects.get(user=account)
#                     except:
#                         token = Token.objects.create(user=account)
# 
#                     data['token'] = token.key
# 
# 
#                     return Response(data)
#                 else:
#                     return Response('This account is not Active.', status=status.HTTP_401_UNAUTHORIZED)
#             else:
#                 return Response('Username/password combination invalid.', status=status.HTTP_401_UNAUTHORIZED)
# 
#         return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
# 
#     def get(self, request, format=None):
#         """
#         Update thumbnail and tiny file field
#         """
# 
#         data_dic = {"Error":"GET not supported for this command"}
# 
#         logout(request)
#         mystatus = status.HTTP_400_BAD_REQUEST
# 
#         return Response(data_dic, status=mystatus)
# 
# class APILogoutViewSet(APIView):
#     permission_classes = (permissions.IsAuthenticated,)
# 
#     def post(self, request, format=None):
#         logout(request)
# 
#         return Response({}, status=status.HTTP_204_NO_CONTENT)
# 
# class APITokenViewSet(APIView):
#     """
#     View to get User's token
#     """
# 
#     def get(self, request, format=None):
#         """
#         Update thumbnail and tiny file field
#         """
#         if request.user.is_anonymous:
#             # User most login before they can get a token
#             # This not only ensures the user has registered, and has an account
#             # but that the account is active
#             return Response('User not recognized.', status=status.HTTP_403_FORBIDDEN)
# 
#         data_dic = {}
# 
#         try:
#             token = Token.objects.get(user=request.user)
#             mystatus = status.HTTP_200_OK
#         except:
#             token = Token.objects.create(user=request.user)
#             mystatus = status.HTTP_201_CREATED
# 
#         data_dic['token'] = token.key
#         return Response(data_dic, status=mystatus)
# 
# class APIUserInfoViewSet(APIView):
#     """
#     View to list all users in the system.
# 
#     * Requires token authentication.
#     * Only admin users are able to access this view.
#     """
#     #permission_classes = ()
# 
#     def get(self, request, format=None):
#         """
#         Update thumbnail and tiny file field
#         """
#         if request.user.is_anonymous:
#             # User most login before they can get a token
#             # This not only ensures the user has registered, and has an account
#             # but that the account is active
#             return Response('User not recognized.', status=status.HTTP_403_FORBIDDEN)
# 
#         account = request.user
# 
#         serialized = AccountSerializer(account)
#         data = serialized.data
# 
#         # Add the token to the return serialization
#         try:
#             token = Token.objects.get(user=account)
#         except:
#             token = Token.objects.create(user=account)
# 
#         data['token'] = token.key
# 
#         return Response(data)
# 
# class FacebookLoginOrSignup(APIView):
# 
#     permission_classes = (AllowAny,)
# 
#     # this is a public api!!!
#     authentication_classes = (EverybodyCanAuthentication,)
# 
#     def dispatch(self, *args, **kwargs):
#         return super(FacebookLoginOrSignup, self).dispatch(*args, **kwargs)
# 
#     def post(self, request):
#         data = JSONParser().parse(request)
#         access_token = data.get('access_token', '')
# 
#         try:
#             app = SocialApp.objects.get(provider="facebook")
#             token = SocialToken(app=app, token=access_token)
# 
#             # check token against facebook
#             login = fb_complete_login(app, token)
#             login.token = token
#             login.state = SocialLogin.state_from_request(request)
# 
#             # add or update the user into users table
#             ret = complete_social_login(request, login)
# 
#             # if we get here we've succeeded
#             return Response(status=200, data={
#                 'success': True,
#                 'username': request.user.username,
#                 'user_id': request.user.pk,
#             })
# 
#         except:
# 
#             return Response(status=401 ,data={
#                 'success': False,
#                 'reason': "Bad Access Token",
#             })


from django.urls import include, path
from django.views.decorators.csrf import csrf_exempt
from rest_framework.authtoken.views import obtain_auth_token
from .api_views import APILoginViewSet, APILogoutViewSet, APITokenViewSet, APIUserInfoViewSet, FacebookLoginOrSignup



urlpatterns = [
    path('login/', APILoginViewSet.as_view(), name='api-login'),
    path('logout/', APILogoutViewSet.as_view(), name='api-logout'),
    path('token/', APITokenViewSet.as_view(), name='api-token'),
    path('user-info/', APIUserInfoViewSet.as_view(), name='api-user-info'),
    path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
    path('api-token-auth/', csrf_exempt(obtain_auth_token), name='api-token-auth'),

# Path: server/apps/authentication/api_views.py
# class APILoginViewSet(APIView):
#     """
#     View to list all users in the system.
# 
#     * Requires token authentication.
#     * Only admin users are able to access this view.
#     """
#     #permission_classes = ()
# 
#     @csrf_exempt
#     def post(self, request, format=None):
#         """
#         Update thumbnail and tiny file field
#         """
#         data = JSONParser().parse(request)
#         serializer = LoginCustomSerializer(data=data)
# 
#         if serializer.is_valid():
#             email = serializer.data.get('email')
#             password = serializer.data.get('password')
# 
#             if not request.user.is_anonymous:
#                 return Response('Already Logged-in', status=status.HTTP_403_FORBIDDEN)
# 
#             account = authenticate(email=email, password=password)
# 
#             if account is not None:
#                 if account.is_active:
#                     login(request, account)
# 
#                     serialized = AccountSerializer(account)
#                     data = serialized.data
# 
#                     # Add the token to the return serialization
#                     try:
#                         token = Token.objects.get(user=account)
#                     except:
#                         token = Token.objects.create(user=account)
# 
#                     data['token'] = token.key
# 
# 
#                     return Response(data)
#                 else:
#                     return Response('This account is not Active.', status=status.HTTP_401_UNAUTHORIZED)
#             else:
#                 return Response('Username/password combination invalid.', status=status.HTTP_401_UNAUTHORIZED)
# 
#         return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
# 
#     def get(self, request, format=None):
#         """
#         Update thumbnail and tiny file field
#         """
# 
#         data_dic = {"Error":"GET not supported for this command"}
# 
#         logout(request)
#         mystatus = status.HTTP_400_BAD_REQUEST
# 
#         return Response(data_dic, status=mystatus)
# 
# class APILogoutViewSet(APIView):
#     permission_classes = (permissions.IsAuthenticated,)
# 
#     def post(self, request, format=None):
#         logout(request)
# 
#         return Response({}, status=status.HTTP_204_NO_CONTENT)
# 
# class APITokenViewSet(APIView):
#     """
#     View to get User's token
#     """
# 
#     def get(self, request, format=None):
#         """
#         Update thumbnail and tiny file field
#         """
#         if request.user.is_anonymous:
#             # User most login before they can get a token
#             # This not only ensures the user has registered, and has an account
#             # but that the account is active
#             return Response('User not recognized.', status=status.HTTP_403_FORBIDDEN)
# 
#         data_dic = {}
# 
#         try:
#             token = Token.objects.get(user=request.user)
#             mystatus = status.HTTP_200_OK
#         except:
#             token = Token.objects.create(user=request.user)
#             mystatus = status.HTTP_201_CREATED
# 
#         data_dic['token'] = token.key
#         return Response(data_dic, status=mystatus)
# 
# class APIUserInfoViewSet(APIView):
#     """
#     View to list all users in the system.
# 
#     * Requires token authentication.
#     * Only admin users are able to access this view.
#     """
#     #permission_classes = ()
# 
#     def get(self, request, format=None):
#         """
#         Update thumbnail and tiny file field
#         """
#         if request.user.is_anonymous:
#             # User most login before they can get a token
#             # This not only ensures the user has registered, and has an account
#             # but that the account is active
#             return Response('User not recognized.', status=status.HTTP_403_FORBIDDEN)
# 
#         account = request.user
# 
#         serialized = AccountSerializer(account)
#         data = serialized.data
# 
#         # Add the token to the return serialization
#         try:
#             token = Token.objects.get(user=account)
#         except:
#             token = Token.objects.create(user=account)
# 
#         data['token'] = token.key
# 
#         return Response(data)
# 
# class FacebookLoginOrSignup(APIView):
# 
#     permission_classes = (AllowAny,)
# 
#     # this is a public api!!!
#     authentication_classes = (EverybodyCanAuthentication,)
# 
#     def dispatch(self, *args, **kwargs):
#         return super(FacebookLoginOrSignup, self).dispatch(*args, **kwargs)
# 
#     def post(self, request):
#         data = JSONParser().parse(request)
#         access_token = data.get('access_token', '')
# 
#         try:
#             app = SocialApp.objects.get(provider="facebook")
#             token = SocialToken(app=app, token=access_token)
# 
#             # check token against facebook
#             login = fb_complete_login(app, token)
#             login.token = token
#             login.state = SocialLogin.state_from_request(request)
# 
#             # add or update the user into users table
#             ret = complete_social_login(request, login)
# 
#             # if we get here we've succeeded
#             return Response(status=200, data={
#                 'success': True,
#                 'username': request.user.username,
#                 'user_id': request.user.pk,
#             })
# 
#         except:
# 
#             return Response(status=401 ,data={
#                 'success': False,
#                 'reason': "Bad Access Token",
#             })


# Path: server/apps/authentication/api_urls.py
from django.urls import include, path
from django.views.decorators.csrf import csrf_exempt
from rest_framework.authtoken.views import obtain_auth_token
from .api_views import APILoginViewSet, APILogoutViewSet, APITokenViewSet, APIUserInfoViewSet, FacebookLoginOrSignup




urlpatterns = [
    path('login/', APILoginViewSet.as_view(), name='api-login'),
    path('logout/', APILogoutViewSet.as_view(), name='api-logout'),
    path('token/', APITokenViewSet.as_view(), name='api-token'),
    path('user-info/', APIUserInfoViewSet.as_view(), name='api-user-info'),
    path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
    path('api-token-auth/', csrf_exempt(obtain_auth_token), name='api-token-auth'),

    path('facebook-signup/?', csrf_exempt(FacebookLoginOrSignup.as_view()), name='facebook-login-signup'),