ParseSession.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  7. var _isRevocableSession = _interopRequireDefault(require("./isRevocableSession"));
  8. var _ParseObject = _interopRequireDefault(require("./ParseObject"));
  9. var _ParseUser = _interopRequireDefault(require("./ParseUser"));
  10. function _interopRequireDefault(obj) {
  11. return obj && obj.__esModule ? obj : {
  12. default: obj
  13. };
  14. }
  15. /**
  16. * Copyright (c) 2015-present, Parse, LLC.
  17. * All rights reserved.
  18. *
  19. * This source code is licensed under the BSD-style license found in the
  20. * LICENSE file in the root directory of this source tree. An additional grant
  21. * of patent rights can be found in the PATENTS file in the same directory.
  22. *
  23. * @flow
  24. */
  25. /**
  26. * <p>A Parse.Session object is a local representation of a revocable session.
  27. * This class is a subclass of a Parse.Object, and retains the same
  28. * functionality of a Parse.Object.</p>
  29. *
  30. * @alias Parse.Session
  31. * @augments Parse.Object
  32. */
  33. class ParseSession extends _ParseObject.default {
  34. /**
  35. * @param {object} attributes The initial set of data to store in the user.
  36. */
  37. constructor(attributes
  38. /*: ?AttributeMap*/
  39. ) {
  40. super('_Session');
  41. if (attributes && typeof attributes === 'object') {
  42. if (!this.set(attributes || {})) {
  43. throw new Error("Can't create an invalid Session");
  44. }
  45. }
  46. }
  47. /**
  48. * Returns the session token string.
  49. *
  50. * @returns {string}
  51. */
  52. getSessionToken()
  53. /*: string*/
  54. {
  55. const token = this.get('sessionToken');
  56. if (typeof token === 'string') {
  57. return token;
  58. }
  59. return '';
  60. }
  61. static readOnlyAttributes() {
  62. return ['createdWith', 'expiresAt', 'installationId', 'restricted', 'sessionToken', 'user'];
  63. }
  64. /**
  65. * Retrieves the Session object for the currently logged in session.
  66. *
  67. * @param {object} options useMasterKey
  68. * @static
  69. * @returns {Promise} A promise that is resolved with the Parse.Session
  70. * object after it has been fetched. If there is no current user, the
  71. * promise will be rejected.
  72. */
  73. static current(options
  74. /*: FullOptions*/
  75. ) {
  76. options = options || {};
  77. const controller = _CoreManager.default.getSessionController();
  78. const sessionOptions = {};
  79. if (options.hasOwnProperty('useMasterKey')) {
  80. sessionOptions.useMasterKey = options.useMasterKey;
  81. }
  82. return _ParseUser.default.currentAsync().then(user => {
  83. if (!user) {
  84. return Promise.reject('There is no current user.');
  85. }
  86. sessionOptions.sessionToken = user.getSessionToken();
  87. return controller.getSession(sessionOptions);
  88. });
  89. }
  90. /**
  91. * Determines whether the current session token is revocable.
  92. * This method is useful for migrating Express.js or Node.js web apps to
  93. * use revocable sessions. If you are migrating an app that uses the Parse
  94. * SDK in the browser only, please use Parse.User.enableRevocableSession()
  95. * instead, so that sessions can be automatically upgraded.
  96. *
  97. * @static
  98. * @returns {boolean}
  99. */
  100. static isCurrentSessionRevocable()
  101. /*: boolean*/
  102. {
  103. const currentUser = _ParseUser.default.current();
  104. if (currentUser) {
  105. return (0, _isRevocableSession.default)(currentUser.getSessionToken() || '');
  106. }
  107. return false;
  108. }
  109. }
  110. _ParseObject.default.registerSubclass('_Session', ParseSession);
  111. const DefaultController = {
  112. getSession(options
  113. /*: RequestOptions*/
  114. )
  115. /*: Promise<ParseSession>*/
  116. {
  117. const RESTController = _CoreManager.default.getRESTController();
  118. const session = new ParseSession();
  119. return RESTController.request('GET', 'sessions/me', {}, options).then(sessionData => {
  120. session._finishFetch(sessionData);
  121. session._setExisted(true);
  122. return session;
  123. });
  124. }
  125. };
  126. _CoreManager.default.setSessionController(DefaultController);
  127. var _default = ParseSession;
  128. exports.default = _default;