ParseSession.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. * @alias Parse.Session
  30. * @extends Parse.Object
  31. */
  32. class ParseSession extends _ParseObject.default {
  33. /**
  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. * @return {String}
  50. */
  51. getSessionToken()
  52. /*: string*/
  53. {
  54. const token = this.get('sessionToken');
  55. if (typeof token === 'string') {
  56. return token;
  57. }
  58. return '';
  59. }
  60. static readOnlyAttributes() {
  61. return ['createdWith', 'expiresAt', 'installationId', 'restricted', 'sessionToken', 'user'];
  62. }
  63. /**
  64. * Retrieves the Session object for the currently logged in session.
  65. * @static
  66. * @return {Promise} A promise that is resolved with the Parse.Session
  67. * object after it has been fetched. If there is no current user, the
  68. * promise will be rejected.
  69. */
  70. static current(options
  71. /*: FullOptions*/
  72. ) {
  73. options = options || {};
  74. const controller = _CoreManager.default.getSessionController();
  75. const sessionOptions = {};
  76. if (options.hasOwnProperty('useMasterKey')) {
  77. sessionOptions.useMasterKey = options.useMasterKey;
  78. }
  79. return _ParseUser.default.currentAsync().then(user => {
  80. if (!user) {
  81. return Promise.reject('There is no current user.');
  82. }
  83. sessionOptions.sessionToken = user.getSessionToken();
  84. return controller.getSession(sessionOptions);
  85. });
  86. }
  87. /**
  88. * Determines whether the current session token is revocable.
  89. * This method is useful for migrating Express.js or Node.js web apps to
  90. * use revocable sessions. If you are migrating an app that uses the Parse
  91. * SDK in the browser only, please use Parse.User.enableRevocableSession()
  92. * instead, so that sessions can be automatically upgraded.
  93. * @static
  94. * @return {Boolean}
  95. */
  96. static isCurrentSessionRevocable()
  97. /*: boolean*/
  98. {
  99. const currentUser = _ParseUser.default.current();
  100. if (currentUser) {
  101. return (0, _isRevocableSession.default)(currentUser.getSessionToken() || '');
  102. }
  103. return false;
  104. }
  105. }
  106. _ParseObject.default.registerSubclass('_Session', ParseSession);
  107. const DefaultController = {
  108. getSession(options
  109. /*: RequestOptions*/
  110. )
  111. /*: Promise<ParseSession>*/
  112. {
  113. const RESTController = _CoreManager.default.getRESTController();
  114. const session = new ParseSession();
  115. return RESTController.request('GET', 'sessions/me', {}, options).then(sessionData => {
  116. session._finishFetch(sessionData);
  117. session._setExisted(true);
  118. return session;
  119. });
  120. }
  121. };
  122. _CoreManager.default.setSessionController(DefaultController);
  123. var _default = ParseSession;
  124. exports.default = _default;