AnonymousUtils.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * Copyright (c) 2015-present, Parse, LLC.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. * @flow-weak
  10. */
  11. import ParseUser from './ParseUser';
  12. const uuidv4 = require('uuid/v4');
  13. /*:: import type { RequestOptions } from './RESTController';*/
  14. let registered = false;
  15. /**
  16. * Provides utility functions for working with Anonymously logged-in users. <br />
  17. * Anonymous users have some unique characteristics:
  18. * <ul>
  19. * <li>Anonymous users don't need a user name or password.</li>
  20. * <ul>
  21. * <li>Once logged out, an anonymous user cannot be recovered.</li>
  22. * </ul>
  23. * <li>signUp converts an anonymous user to a standard user with the given username and password.</li>
  24. * <ul>
  25. * <li>Data associated with the anonymous user is retained.</li>
  26. * </ul>
  27. * <li>logIn switches users without converting the anonymous user.</li>
  28. * <ul>
  29. * <li>Data associated with the anonymous user will be lost.</li>
  30. * </ul>
  31. * <li>Service logIn (e.g. Facebook, Twitter) will attempt to convert
  32. * the anonymous user into a standard user by linking it to the service.</li>
  33. * <ul>
  34. * <li>If a user already exists that is linked to the service, it will instead switch to the existing user.</li>
  35. * </ul>
  36. * <li>Service linking (e.g. Facebook, Twitter) will convert the anonymous user
  37. * into a standard user by linking it to the service.</li>
  38. * </ul>
  39. * @class Parse.AnonymousUtils
  40. * @static
  41. */
  42. const AnonymousUtils = {
  43. /**
  44. * Gets whether the user has their account linked to anonymous user.
  45. *
  46. * @method isLinked
  47. * @name Parse.AnonymousUtils.isLinked
  48. * @param {Parse.User} user User to check for.
  49. * The user must be logged in on this device.
  50. * @return {Boolean} <code>true</code> if the user has their account
  51. * linked to an anonymous user.
  52. * @static
  53. */
  54. isLinked(user
  55. /*: ParseUser*/
  56. ) {
  57. const provider = this._getAuthProvider();
  58. return user._isLinked(provider.getAuthType());
  59. },
  60. /**
  61. * Logs in a user Anonymously.
  62. *
  63. * @method logIn
  64. * @name Parse.AnonymousUtils.logIn
  65. * @param {Object} options MasterKey / SessionToken.
  66. * @returns {Promise}
  67. * @static
  68. */
  69. logIn(options
  70. /*:: ?: RequestOptions*/
  71. ) {
  72. const provider = this._getAuthProvider();
  73. return ParseUser._logInWith(provider.getAuthType(), provider.getAuthData(), options);
  74. },
  75. /**
  76. * Links Anonymous User to an existing PFUser.
  77. *
  78. * @method link
  79. * @name Parse.AnonymousUtils.link
  80. * @param {Parse.User} user User to link. This must be the current user.
  81. * @param {Object} options MasterKey / SessionToken.
  82. * @returns {Promise}
  83. * @static
  84. */
  85. link(user
  86. /*: ParseUser*/
  87. , options
  88. /*:: ?: RequestOptions*/
  89. ) {
  90. const provider = this._getAuthProvider();
  91. return user._linkWith(provider.getAuthType(), provider.getAuthData(), options);
  92. },
  93. _getAuthProvider() {
  94. const provider = {
  95. restoreAuthentication() {
  96. return true;
  97. },
  98. getAuthType() {
  99. return 'anonymous';
  100. },
  101. getAuthData() {
  102. return {
  103. authData: {
  104. id: uuidv4()
  105. }
  106. };
  107. }
  108. };
  109. if (!registered) {
  110. ParseUser._registerAuthenticationProvider(provider);
  111. registered = true;
  112. }
  113. return provider;
  114. }
  115. };
  116. export default AnonymousUtils;