AnonymousUtils.js 3.9 KB

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