AnonymousUtils.js 3.6 KB

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