Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. class register_user_via_facebook(APIView):
  2. permission_classes = (AllowAny,)
  3.  
  4. def post(self, *args, **kwargs):
  5.  
  6. try:
  7. data = dict(self.request.data)
  8. except Exception as e:
  9. data = None
  10.  
  11. if (not 'accessToken' in data) or (not 'userID' in data):
  12. return responseError(MISSING_PARAMETERS, MESSAGE_MISSING_PARAMETERS)
  13.  
  14. # check if already register
  15. fbAuth = None
  16. try:
  17. fbAuth = FbAuth.objects.get(facebook_id=data['userID'][0])
  18. except Exception as e:
  19. pass
  20.  
  21. if fbAuth: # return existing user token
  22. payload = api_settings.JWT_PAYLOAD_HANDLER(fbAuth.user)
  23. token = api_settings.JWT_ENCODE_HANDLER(payload)
  24. else:
  25. # Step 1: Call facebook and verify details. Make sure data is correct.
  26. profile = facebook.GraphAPI(access_token=data['accessToken']).get_object('me', fields='email,first_name,last_name,gender,picture')
  27.  
  28. try:
  29. email = profile.get('email') or data['email']
  30. username = profile.get('email') or data['email']
  31. except Exception as e:
  32. return responseError(ERROR_FACEBOOK_ACCOUNT_MISSING_EMAIL, MESSAGE_ERROR_FACEBOOK_ACCOUNT_MISSING_EMAIL)
  33.  
  34. try:
  35. first_name = profile.get('first_name') or ''
  36. except Exception as e:
  37. pass
  38.  
  39. try:
  40. last_name = profile.get('last_name') or ''
  41. except Exception as e:
  42. pass
  43.  
  44. password = data['accessToken']
  45. # Step 2: Store details. Register the user.
  46. try:
  47. user = User.objects.create_user(username=username, email=email, password=password, first_name=first_name, last_name=last_name)
  48. except IntegrityError:
  49. user = User.objects.get(username=username, email=email)
  50.  
  51. FbAuth.create_or_update(user, data['userID'], data['accessToken'])
  52.  
  53. user_facebook_avatar_url = profile.get('picture')['data']['url']
  54.  
  55. # extra : update userprofile image
  56. try:
  57. user_profile = UserProfiles.create_or_update(user=user,
  58. firstName=first_name,
  59. lastName=last_name,
  60. email=email,
  61. facebook_avatar=user_facebook_avatar_url)
  62. except Exception as e:
  63. pass
  64.  
  65. # Step 3: Return JWT token in cookie.
  66.  
  67. payload = api_settings.JWT_PAYLOAD_HANDLER(user)
  68. token = api_settings.JWT_ENCODE_HANDLER(payload)
  69.  
  70. # Step 4: Set token as cookie
  71. response = JsonResponse({'token': token}, safe=False, status=status.HTTP_200_OK)
  72.  
  73. return response
  74.  
  75.  
  76. class register_user_via_google(APIView):
  77. permission_classes = (AllowAny,)
  78.  
  79. def post(self, *args, **kwargs):
  80.  
  81. data = dict(self.request.data)
  82.  
  83. if (not 'accessToken' in self.request.POST) or (not 'userID' in self.request.POST):
  84. return responseError(MISSING_PARAMETERS, MESSAGE_MISSING_PARAMETERS)
  85.  
  86. gAuth = None
  87. try:
  88. gAuth = GoogleAuth.objects.get(google_id=data['userID'])
  89. except Exception as e:
  90. pass
  91.  
  92. if gAuth:
  93. payload = api_settings.JWT_PAYLOAD_HANDLER(gAuth.user)
  94. token = api_settings.JWT_ENCODE_HANDLER(payload)
  95. else:
  96. # Step 1: Call facebook and verify details. Make sure data is correct.
  97. google_api_userprofile_endpoint = 'https://www.googleapis.com/userinfo/v2/me?fields=id%2C+name%2C+email&key='+GOOGLE_API_KEY
  98. credentials = AccessTokenCredentials(data['accessToken'][0],'Mozilla/5.0')
  99. http = httplib2.Http()
  100. http = credentials.authorize(http)
  101. response, content = http.request(uri=google_api_userprofile_endpoint)
  102. profile = json.loads(content)
  103.  
  104. if not profile or not profile.get('email'):
  105. raise APIException(code=status.HTTP_400_BAD_REQUEST)
  106.  
  107. username, password, email, first_name = profile.get('email'), data['accessToken'][0], profile.get('email'), profile.get('name')
  108. # Step 2: Store details. Register the user.
  109. try:
  110. user = User.objects.create_user(username=username, email=email, password=password, first_name=first_name)
  111. except IntegrityError:
  112. user = User.objects.get(username=username, email=email)
  113.  
  114. GoogleAuth.create_or_update(user, data['userID'][0], data['accessToken'][0])
  115.  
  116. try:
  117. user_profile = UserProfiles.create_or_update(user=user,
  118. firstName=first_name,
  119. email=email)
  120. except Exception as e:
  121. pass
  122.  
  123. # Step 3: Return JWT token in cookie.
  124.  
  125. payload = api_settings.JWT_PAYLOAD_HANDLER(user)
  126. token = api_settings.JWT_ENCODE_HANDLER(payload)
  127.  
  128. # Step 4: Set token as cookie
  129. response = JsonResponse({'token': token}, safe=False, status=status.HTTP_200_OK)
  130.  
  131. return response
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement