FacebookUtils.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. *
  79. * @class Parse.FacebookUtils
  80. * @static
  81. * @hideconstructor
  82. */
  83. const FacebookUtils = {
  84. /**
  85. * Initializes Parse Facebook integration. Call this function after you
  86. * have loaded the Facebook Javascript SDK with the same parameters
  87. * as you would pass to<code>
  88. * <a href=
  89. * "https://developers.facebook.com/docs/reference/javascript/FB.init/">
  90. * FB.init()</a></code>. Parse.FacebookUtils will invoke FB.init() for you
  91. * with these arguments.
  92. *
  93. * @function init
  94. * @name Parse.FacebookUtils.init
  95. * @param {object} options Facebook options argument as described here:
  96. * <a href=
  97. * "https://developers.facebook.com/docs/reference/javascript/FB.init/">
  98. * FB.init()</a>. The status flag will be coerced to 'false' because it
  99. * interferes with Parse Facebook integration. Call FB.getLoginStatus()
  100. * explicitly if this behavior is required by your application.
  101. */
  102. init(options) {
  103. if (typeof FB === 'undefined') {
  104. throw new Error('The Facebook JavaScript SDK must be loaded before calling init.');
  105. }
  106. initOptions = {};
  107. if (options) {
  108. for (const key in options) {
  109. initOptions[key] = options[key];
  110. }
  111. }
  112. if (initOptions.status && typeof console !== 'undefined') {
  113. const warn = console.warn || console.log || function () {}; // eslint-disable-line no-console
  114. 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.');
  115. }
  116. initOptions.status = false;
  117. FB.init(initOptions);
  118. _ParseUser.default._registerAuthenticationProvider(provider);
  119. initialized = true;
  120. },
  121. /**
  122. * Gets whether the user has their account linked to Facebook.
  123. *
  124. * @function isLinked
  125. * @name Parse.FacebookUtils.isLinked
  126. * @param {Parse.User} user User to check for a facebook link.
  127. * The user must be logged in on this device.
  128. * @returns {boolean} <code>true</code> if the user has their account
  129. * linked to Facebook.
  130. */
  131. isLinked(user) {
  132. return user._isLinked('facebook');
  133. },
  134. /**
  135. * Logs in a user using Facebook. This method delegates to the Facebook
  136. * SDK to authenticate the user, and then automatically logs in (or
  137. * creates, in the case where it is a new user) a Parse.User.
  138. *
  139. * Standard API:
  140. *
  141. * <code>logIn(permission: string, authData: Object);</code>
  142. *
  143. * Advanced API: Used for handling your own oAuth tokens
  144. * {@link https://docs.parseplatform.org/rest/guide/#linking-users}
  145. *
  146. * <code>logIn(authData: Object, options?: Object);</code>
  147. *
  148. * @function logIn
  149. * @name Parse.FacebookUtils.logIn
  150. * @param {(string | object)} permissions The permissions required for Facebook
  151. * log in. This is a comma-separated string of permissions.
  152. * Alternatively, supply a Facebook authData object as described in our
  153. * REST API docs if you want to handle getting facebook auth tokens
  154. * yourself.
  155. * @param {object} options MasterKey / SessionToken. Alternatively can be used for authData if permissions is a string
  156. * @returns {Promise}
  157. */
  158. logIn(permissions, options) {
  159. if (!permissions || typeof permissions === 'string') {
  160. if (!initialized) {
  161. throw new Error('You must initialize FacebookUtils before calling logIn.');
  162. }
  163. requestedPermissions = permissions;
  164. return _ParseUser.default.logInWith('facebook', options);
  165. }
  166. return _ParseUser.default.logInWith('facebook', {
  167. authData: permissions
  168. }, options);
  169. },
  170. /**
  171. * Links Facebook to an existing PFUser. This method delegates to the
  172. * Facebook SDK to authenticate the user, and then automatically links
  173. * the account to the Parse.User.
  174. *
  175. * Standard API:
  176. *
  177. * <code>link(user: Parse.User, permission: string, authData?: Object);</code>
  178. *
  179. * Advanced API: Used for handling your own oAuth tokens
  180. * {@link https://docs.parseplatform.org/rest/guide/#linking-users}
  181. *
  182. * <code>link(user: Parse.User, authData: Object, options?: FullOptions);</code>
  183. *
  184. * @function link
  185. * @name Parse.FacebookUtils.link
  186. * @param {Parse.User} user User to link to Facebook. This must be the
  187. * current user.
  188. * @param {(string | object)} permissions The permissions required for Facebook
  189. * log in. This is a comma-separated string of permissions.
  190. * Alternatively, supply a Facebook authData object as described in our
  191. * REST API docs if you want to handle getting facebook auth tokens
  192. * yourself.
  193. * @param {object} options MasterKey / SessionToken. Alternatively can be used for authData if permissions is a string
  194. * @returns {Promise}
  195. */
  196. link(user, permissions, options) {
  197. if (!permissions || typeof permissions === 'string') {
  198. if (!initialized) {
  199. throw new Error('You must initialize FacebookUtils before calling link.');
  200. }
  201. requestedPermissions = permissions;
  202. return user.linkWith('facebook', options);
  203. }
  204. return user.linkWith('facebook', {
  205. authData: permissions
  206. }, options);
  207. },
  208. /**
  209. * Unlinks the Parse.User from a Facebook account.
  210. *
  211. * @function unlink
  212. * @name Parse.FacebookUtils.unlink
  213. * @param {Parse.User} user User to unlink from Facebook. This must be the
  214. * current user.
  215. * @param {object} options Standard options object with success and error
  216. * callbacks.
  217. * @returns {Promise}
  218. */
  219. unlink: function (user, options) {
  220. if (!initialized) {
  221. throw new Error('You must initialize FacebookUtils before calling unlink.');
  222. }
  223. return user._unlinkFrom('facebook', options);
  224. },
  225. // Used for testing purposes
  226. _getAuthProvider() {
  227. return provider;
  228. }
  229. };
  230. var _default = FacebookUtils;
  231. exports.default = _default;