FacebookUtils.js 8.0 KB

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