AnonymousUtils.js 4.0 KB

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