AnonymousUtils.js 3.5 KB

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