FacebookUtils.js 8.1 KB

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