FacebookUtils.js 7.8 KB

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