AnonymousUtils.js 3.8 KB

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