FacebookUtils.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _ParseUser = _interopRequireDefault(require("./ParseUser"));
  7. var initialized = false;
  8. var requestedPermissions;
  9. var initOptions;
  10. var provider = {
  11. authenticate: function (options) {
  12. var _this = this;
  13. if (typeof FB === 'undefined') {
  14. options.error(this, 'Facebook SDK not found.');
  15. }
  16. FB.login(function (response) {
  17. if (response.authResponse) {
  18. if (options.success) {
  19. options.success(_this, {
  20. id: response.authResponse.userID,
  21. access_token: response.authResponse.accessToken,
  22. expiration_date: new Date(response.authResponse.expiresIn * 1000 + new Date().getTime()).toJSON()
  23. });
  24. }
  25. } else {
  26. if (options.error) {
  27. options.error(_this, response);
  28. }
  29. }
  30. }, {
  31. scope: requestedPermissions
  32. });
  33. },
  34. restoreAuthentication: function (authData) {
  35. if (authData) {
  36. var newOptions = {};
  37. if (initOptions) {
  38. for (var key in initOptions) {
  39. newOptions[key] = initOptions[key];
  40. }
  41. }
  42. newOptions.status = false;
  43. var existingResponse = FB.getAuthResponse();
  44. if (existingResponse && existingResponse.userID !== authData.id) {
  45. FB.logout();
  46. }
  47. FB.init(newOptions);
  48. }
  49. return true;
  50. },
  51. getAuthType: function () {
  52. return 'facebook';
  53. },
  54. deauthenticate: function () {
  55. this.restoreAuthentication(null);
  56. }
  57. };
  58. var FacebookUtils = {
  59. init: function (options) {
  60. if (typeof FB === 'undefined') {
  61. throw new Error('The Facebook JavaScript SDK must be loaded before calling init.');
  62. }
  63. initOptions = {};
  64. if (options) {
  65. for (var key in options) {
  66. initOptions[key] = options[key];
  67. }
  68. }
  69. if (initOptions.status && typeof console !== 'undefined') {
  70. var warn = console.warn || console.log || function () {};
  71. 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.');
  72. }
  73. initOptions.status = false;
  74. FB.init(initOptions);
  75. _ParseUser.default._registerAuthenticationProvider(provider);
  76. initialized = true;
  77. },
  78. isLinked: function (user) {
  79. return user._isLinked('facebook');
  80. },
  81. logIn: function (permissions, options) {
  82. if (!permissions || typeof permissions === 'string') {
  83. if (!initialized) {
  84. throw new Error('You must initialize FacebookUtils before calling logIn.');
  85. }
  86. requestedPermissions = permissions;
  87. return _ParseUser.default.logInWith('facebook', options);
  88. }
  89. return _ParseUser.default.logInWith('facebook', {
  90. authData: permissions
  91. }, options);
  92. },
  93. link: function (user, permissions, options) {
  94. if (!permissions || typeof permissions === 'string') {
  95. if (!initialized) {
  96. throw new Error('You must initialize FacebookUtils before calling link.');
  97. }
  98. requestedPermissions = permissions;
  99. return user.linkWith('facebook', options);
  100. }
  101. return user.linkWith('facebook', {
  102. authData: permissions
  103. }, options);
  104. },
  105. unlink: function (user, options) {
  106. if (!initialized) {
  107. throw new Error('You must initialize FacebookUtils before calling unlink.');
  108. }
  109. return user._unlinkFrom('facebook', options);
  110. },
  111. _getAuthProvider: function () {
  112. return provider;
  113. }
  114. };
  115. var _default = FacebookUtils;
  116. exports.default = _default;