FacebookUtils.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. "use strict";
  2. var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");
  3. var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
  4. _Object$defineProperty(exports, "__esModule", {
  5. value: true
  6. });
  7. exports.default = void 0;
  8. var _ParseUser = _interopRequireDefault(require("./ParseUser"));
  9. /**
  10. * Copyright (c) 2015-present, Parse, LLC.
  11. * All rights reserved.
  12. *
  13. * This source code is licensed under the BSD-style license found in the
  14. * LICENSE file in the root directory of this source tree. An additional grant
  15. * of patent rights can be found in the PATENTS file in the same directory.
  16. *
  17. * @flow-weak
  18. */
  19. /* global FB */
  20. var initialized = false;
  21. var requestedPermissions;
  22. var initOptions;
  23. var provider = {
  24. authenticate: function (options) {
  25. var _this = this;
  26. if (typeof FB === 'undefined') {
  27. options.error(this, 'Facebook SDK not found.');
  28. }
  29. FB.login(function (response) {
  30. if (response.authResponse) {
  31. if (options.success) {
  32. options.success(_this, {
  33. id: response.authResponse.userID,
  34. access_token: response.authResponse.accessToken,
  35. expiration_date: new Date(response.authResponse.expiresIn * 1000 + new Date().getTime()).toJSON()
  36. });
  37. }
  38. } else {
  39. if (options.error) {
  40. options.error(_this, response);
  41. }
  42. }
  43. }, {
  44. scope: requestedPermissions
  45. });
  46. },
  47. restoreAuthentication: function (authData) {
  48. if (authData) {
  49. var newOptions = {};
  50. if (initOptions) {
  51. for (var key in initOptions) {
  52. newOptions[key] = initOptions[key];
  53. }
  54. } // Suppress checks for login status from the browser.
  55. newOptions.status = false; // If the user doesn't match the one known by the FB SDK, log out.
  56. // Most of the time, the users will match -- it's only in cases where
  57. // the FB SDK knows of a different user than the one being restored
  58. // from a Parse User that logged in with username/password.
  59. var existingResponse = FB.getAuthResponse();
  60. if (existingResponse && existingResponse.userID !== authData.id) {
  61. FB.logout();
  62. }
  63. FB.init(newOptions);
  64. }
  65. return true;
  66. },
  67. getAuthType: function () {
  68. return 'facebook';
  69. },
  70. deauthenticate: function () {
  71. this.restoreAuthentication(null);
  72. }
  73. };
  74. /**
  75. * Provides a set of utilities for using Parse with Facebook.
  76. *
  77. * @class Parse.FacebookUtils
  78. * @static
  79. * @hideconstructor
  80. */
  81. var FacebookUtils = {
  82. /**
  83. * Initializes Parse Facebook integration. Call this function after you
  84. * have loaded the Facebook Javascript SDK with the same parameters
  85. * as you would pass to<code>
  86. * <a href=
  87. * "https://developers.facebook.com/docs/reference/javascript/FB.init/">
  88. * FB.init()</a></code>. Parse.FacebookUtils will invoke FB.init() for you
  89. * with these arguments.
  90. *
  91. * @function init
  92. * @name Parse.FacebookUtils.init
  93. * @param {object} options Facebook options argument as described here:
  94. * <a href=
  95. * "https://developers.facebook.com/docs/reference/javascript/FB.init/">
  96. * FB.init()</a>. The status flag will be coerced to 'false' because it
  97. * interferes with Parse Facebook integration. Call FB.getLoginStatus()
  98. * explicitly if this behavior is required by your application.
  99. */
  100. init: function (options) {
  101. if (typeof FB === 'undefined') {
  102. throw new Error('The Facebook JavaScript SDK must be loaded before calling init.');
  103. }
  104. initOptions = {};
  105. if (options) {
  106. for (var key in options) {
  107. initOptions[key] = options[key];
  108. }
  109. }
  110. if (initOptions.status && typeof console !== 'undefined') {
  111. var warn = console.warn || console.log || function () {}; // eslint-disable-line no-console
  112. warn.call(console, 'The "status" flag passed into' + ' FB.init, when set to true, can interfere with Parse Facebook' + ' integration, so it has been suppressed. Please call' + ' FB.getLoginStatus() explicitly if you require this behavior.');
  113. }
  114. initOptions.status = false;
  115. FB.init(initOptions);
  116. _ParseUser.default._registerAuthenticationProvider(provider);
  117. initialized = true;
  118. },
  119. /**
  120. * Gets whether the user has their account linked to Facebook.
  121. *
  122. * @function isLinked
  123. * @name Parse.FacebookUtils.isLinked
  124. * @param {Parse.User} user User to check for a facebook link.
  125. * The user must be logged in on this device.
  126. * @returns {boolean} <code>true</code> if the user has their account
  127. * linked to Facebook.
  128. */
  129. isLinked: function (user) {
  130. return user._isLinked('facebook');
  131. },
  132. /**
  133. * Logs in a user using Facebook. This method delegates to the Facebook
  134. * SDK to authenticate the user, and then automatically logs in (or
  135. * creates, in the case where it is a new user) a Parse.User.
  136. *
  137. * Standard API:
  138. *
  139. * <code>logIn(permission: string, authData: Object);</code>
  140. *
  141. * Advanced API: Used for handling your own oAuth tokens
  142. * {@link https://docs.parseplatform.org/rest/guide/#linking-users}
  143. *
  144. * <code>logIn(authData: Object, options?: Object);</code>
  145. *
  146. * @function logIn
  147. * @name Parse.FacebookUtils.logIn
  148. * @param {(string | object)} permissions The permissions required for Facebook
  149. * log in. This is a comma-separated string of permissions.
  150. * Alternatively, supply a Facebook authData object as described in our
  151. * REST API docs if you want to handle getting facebook auth tokens
  152. * yourself.
  153. * @param {object} options MasterKey / SessionToken. Alternatively can be used for authData if permissions is a string
  154. * @returns {Promise}
  155. */
  156. logIn: function (permissions, options) {
  157. if (!permissions || typeof permissions === 'string') {
  158. if (!initialized) {
  159. throw new Error('You must initialize FacebookUtils before calling logIn.');
  160. }
  161. requestedPermissions = permissions;
  162. return _ParseUser.default.logInWith('facebook', options);
  163. }
  164. return _ParseUser.default.logInWith('facebook', {
  165. authData: permissions
  166. }, options);
  167. },
  168. /**
  169. * Links Facebook to an existing PFUser. This method delegates to the
  170. * Facebook SDK to authenticate the user, and then automatically links
  171. * the account to the Parse.User.
  172. *
  173. * Standard API:
  174. *
  175. * <code>link(user: Parse.User, permission: string, authData?: Object);</code>
  176. *
  177. * Advanced API: Used for handling your own oAuth tokens
  178. * {@link https://docs.parseplatform.org/rest/guide/#linking-users}
  179. *
  180. * <code>link(user: Parse.User, authData: Object, options?: FullOptions);</code>
  181. *
  182. * @function link
  183. * @name Parse.FacebookUtils.link
  184. * @param {Parse.User} user User to link to Facebook. This must be the
  185. * current user.
  186. * @param {(string | object)} permissions The permissions required for Facebook
  187. * log in. This is a comma-separated string of permissions.
  188. * Alternatively, supply a Facebook authData object as described in our
  189. * REST API docs if you want to handle getting facebook auth tokens
  190. * yourself.
  191. * @param {object} options MasterKey / SessionToken. Alternatively can be used for authData if permissions is a string
  192. * @returns {Promise}
  193. */
  194. link: function (user, permissions, options) {
  195. if (!permissions || typeof permissions === 'string') {
  196. if (!initialized) {
  197. throw new Error('You must initialize FacebookUtils before calling link.');
  198. }
  199. requestedPermissions = permissions;
  200. return user.linkWith('facebook', options);
  201. }
  202. return user.linkWith('facebook', {
  203. authData: permissions
  204. }, options);
  205. },
  206. /**
  207. * Unlinks the Parse.User from a Facebook account.
  208. *
  209. * @function unlink
  210. * @name Parse.FacebookUtils.unlink
  211. * @param {Parse.User} user User to unlink from Facebook. This must be the
  212. * current user.
  213. * @param {object} options Standard options object with success and error
  214. * callbacks.
  215. * @returns {Promise}
  216. */
  217. unlink: function (user, options) {
  218. if (!initialized) {
  219. throw new Error('You must initialize FacebookUtils before calling unlink.');
  220. }
  221. return user._unlinkFrom('facebook', options);
  222. },
  223. // Used for testing purposes
  224. _getAuthProvider: function () {
  225. return provider;
  226. }
  227. };
  228. var _default = FacebookUtils;
  229. exports.default = _default;