FacebookUtils.js 7.7 KB

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