FacebookUtils.js 8.1 KB

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