FacebookUtils.js 7.8 KB

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