AnonymousUtils.js 4.2 KB

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