AnonymousUtils.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. "use strict";
  2. var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");
  3. var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
  4. _Object$defineProperty(exports, "__esModule", {
  5. value: true
  6. });
  7. exports.default = void 0;
  8. var _ParseUser = _interopRequireDefault(require("./ParseUser"));
  9. /**
  10. * @flow-weak
  11. */
  12. /*:: import type { RequestOptions } from './RESTController';*/
  13. var uuidv4 = require('./uuid');
  14. var registered = false;
  15. /**
  16. * Provides utility functions for working with Anonymously logged-in users. <br />
  17. * Anonymous users have some unique characteristics:
  18. * <ul>
  19. * <li>Anonymous users don't need a user name or password.</li>
  20. * <ul>
  21. * <li>Once logged out, an anonymous user cannot be recovered.</li>
  22. * </ul>
  23. * <li>signUp converts an anonymous user to a standard user with the given username and password.</li>
  24. * <ul>
  25. * <li>Data associated with the anonymous user is retained.</li>
  26. * </ul>
  27. * <li>logIn switches users without converting the anonymous user.</li>
  28. * <ul>
  29. * <li>Data associated with the anonymous user will be lost.</li>
  30. * </ul>
  31. * <li>Service logIn (e.g. Facebook, Twitter) will attempt to convert
  32. * the anonymous user into a standard user by linking it to the service.</li>
  33. * <ul>
  34. * <li>If a user already exists that is linked to the service, it will instead switch to the existing user.</li>
  35. * </ul>
  36. * <li>Service linking (e.g. Facebook, Twitter) will convert the anonymous user
  37. * into a standard user by linking it to the service.</li>
  38. * </ul>
  39. *
  40. * @class Parse.AnonymousUtils
  41. * @static
  42. */
  43. var AnonymousUtils = {
  44. /**
  45. * Gets whether the user has their account linked to anonymous user.
  46. *
  47. * @function isLinked
  48. * @name Parse.AnonymousUtils.isLinked
  49. * @param {Parse.User} user User to check for.
  50. * The user must be logged in on this device.
  51. * @returns {boolean} <code>true</code> if the user has their account
  52. * linked to an anonymous user.
  53. * @static
  54. */
  55. isLinked: function (user /*: ParseUser*/) {
  56. var provider = this._getAuthProvider();
  57. return user._isLinked(provider.getAuthType());
  58. },
  59. /**
  60. * Logs in a user Anonymously.
  61. *
  62. * @function logIn
  63. * @name Parse.AnonymousUtils.logIn
  64. * @param {object} options MasterKey / SessionToken.
  65. * @returns {Promise} Logged in user
  66. * @static
  67. */
  68. logIn: function (options /*:: ?: RequestOptions*/) /*: Promise<ParseUser>*/{
  69. var provider = this._getAuthProvider();
  70. return _ParseUser.default.logInWith(provider.getAuthType(), provider.getAuthData(), options);
  71. },
  72. /**
  73. * Links Anonymous User to an existing PFUser.
  74. *
  75. * @function link
  76. * @name Parse.AnonymousUtils.link
  77. * @param {Parse.User} user User to link. This must be the current user.
  78. * @param {object} options MasterKey / SessionToken.
  79. * @returns {Promise} Linked with User
  80. * @static
  81. */
  82. link: function (user /*: ParseUser*/, options /*:: ?: RequestOptions*/) /*: Promise<ParseUser>*/{
  83. var provider = this._getAuthProvider();
  84. return user.linkWith(provider.getAuthType(), provider.getAuthData(), options);
  85. },
  86. /**
  87. * Returns true if Authentication Provider has been registered for use.
  88. *
  89. * @function isRegistered
  90. * @name Parse.AnonymousUtils.isRegistered
  91. * @returns {boolean}
  92. * @static
  93. */
  94. isRegistered: function () /*: boolean*/{
  95. return registered;
  96. },
  97. _getAuthProvider: function () {
  98. var provider = {
  99. restoreAuthentication: function () {
  100. return true;
  101. },
  102. getAuthType: function () {
  103. return 'anonymous';
  104. },
  105. getAuthData: function () {
  106. return {
  107. authData: {
  108. id: uuidv4()
  109. }
  110. };
  111. }
  112. };
  113. if (!registered) {
  114. _ParseUser.default._registerAuthenticationProvider(provider);
  115. registered = true;
  116. }
  117. return provider;
  118. }
  119. };
  120. var _default = AnonymousUtils;
  121. exports.default = _default;