AnonymousUtils.js 3.7 KB

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