AnonymousUtils.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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/v4');
  23. /*:: import type { RequestOptions } from './RESTController';*/
  24. let registered = false;
  25. /**
  26. * Provides utility functions for working with Anonymously logged-in users. <br />
  27. * Anonymous users have some unique characteristics:
  28. * <ul>
  29. * <li>Anonymous users don't need a user name or password.</li>
  30. * <ul>
  31. * <li>Once logged out, an anonymous user cannot be recovered.</li>
  32. * </ul>
  33. * <li>signUp converts an anonymous user to a standard user with the given username and password.</li>
  34. * <ul>
  35. * <li>Data associated with the anonymous user is retained.</li>
  36. * </ul>
  37. * <li>logIn switches users without converting the anonymous user.</li>
  38. * <ul>
  39. * <li>Data associated with the anonymous user will be lost.</li>
  40. * </ul>
  41. * <li>Service logIn (e.g. Facebook, Twitter) will attempt to convert
  42. * the anonymous user into a standard user by linking it to the service.</li>
  43. * <ul>
  44. * <li>If a user already exists that is linked to the service, it will instead switch to the existing user.</li>
  45. * </ul>
  46. * <li>Service linking (e.g. Facebook, Twitter) will convert the anonymous user
  47. * into a standard user by linking it to the service.</li>
  48. * </ul>
  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. * @method 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. * @return {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. * @method logIn
  74. * @name Parse.AnonymousUtils.logIn
  75. * @param {Object} options MasterKey / SessionToken.
  76. * @returns {Promise}
  77. * @static
  78. */
  79. logIn(options
  80. /*:: ?: RequestOptions*/
  81. ) {
  82. const 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. * @method 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}
  93. * @static
  94. */
  95. link(user
  96. /*: ParseUser*/
  97. , options
  98. /*:: ?: RequestOptions*/
  99. ) {
  100. const provider = this._getAuthProvider();
  101. return user._linkWith(provider.getAuthType(), provider.getAuthData(), options);
  102. },
  103. _getAuthProvider() {
  104. const provider = {
  105. restoreAuthentication() {
  106. return true;
  107. },
  108. getAuthType() {
  109. return 'anonymous';
  110. },
  111. getAuthData() {
  112. return {
  113. authData: {
  114. id: uuidv4()
  115. }
  116. };
  117. }
  118. };
  119. if (!registered) {
  120. _ParseUser.default._registerAuthenticationProvider(provider);
  121. registered = true;
  122. }
  123. return provider;
  124. }
  125. };
  126. var _default = AnonymousUtils;
  127. exports.default = _default;