AnonymousUtils.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = void 0;
  7. var _ParseUser = _interopRequireDefault(require("./ParseUser"));
  8. /**
  9. * Copyright (c) 2015-present, Parse, LLC.
  10. * All rights reserved.
  11. *
  12. * This source code is licensed under the BSD-style license found in the
  13. * LICENSE file in the root directory of this source tree. An additional grant
  14. * of patent rights can be found in the PATENTS file in the same directory.
  15. *
  16. * @flow-weak
  17. */
  18. var uuidv4 = require('uuid/v4');
  19. /*:: import type { RequestOptions } from './RESTController';*/
  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. * @class Parse.AnonymousUtils
  46. * @static
  47. */
  48. var AnonymousUtils = {
  49. /**
  50. * Gets whether the user has their account linked to anonymous user.
  51. *
  52. * @method isLinked
  53. * @name Parse.AnonymousUtils.isLinked
  54. * @param {Parse.User} user User to check for.
  55. * The user must be logged in on this device.
  56. * @return {Boolean} <code>true</code> if the user has their account
  57. * linked to an anonymous user.
  58. * @static
  59. */
  60. isLinked: function (user
  61. /*: ParseUser*/
  62. ) {
  63. var provider = this._getAuthProvider();
  64. return user._isLinked(provider.getAuthType());
  65. },
  66. /**
  67. * Logs in a user Anonymously.
  68. *
  69. * @method logIn
  70. * @name Parse.AnonymousUtils.logIn
  71. * @param {Object} options MasterKey / SessionToken.
  72. * @returns {Promise}
  73. * @static
  74. */
  75. logIn: function (options
  76. /*:: ?: RequestOptions*/
  77. ) {
  78. var provider = this._getAuthProvider();
  79. return _ParseUser.default._logInWith(provider.getAuthType(), provider.getAuthData(), options);
  80. },
  81. /**
  82. * Links Anonymous User to an existing PFUser.
  83. *
  84. * @method link
  85. * @name Parse.AnonymousUtils.link
  86. * @param {Parse.User} user User to link. This must be the current user.
  87. * @param {Object} options MasterKey / SessionToken.
  88. * @returns {Promise}
  89. * @static
  90. */
  91. link: function (user
  92. /*: ParseUser*/
  93. , options
  94. /*:: ?: RequestOptions*/
  95. ) {
  96. var provider = this._getAuthProvider();
  97. return user._linkWith(provider.getAuthType(), provider.getAuthData(), options);
  98. },
  99. _getAuthProvider: function () {
  100. var provider = {
  101. restoreAuthentication: function () {
  102. return true;
  103. },
  104. getAuthType: function () {
  105. return 'anonymous';
  106. },
  107. getAuthData: function () {
  108. return {
  109. authData: {
  110. id: uuidv4()
  111. }
  112. };
  113. }
  114. };
  115. if (!registered) {
  116. _ParseUser.default._registerAuthenticationProvider(provider);
  117. registered = true;
  118. }
  119. return provider;
  120. }
  121. };
  122. var _default = AnonymousUtils;
  123. exports.default = _default;