ParseSession.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. * @flow
  17. */
  18. /*:: import type { AttributeMap } from './ObjectStateMutations';*/
  19. /*:: import type { RequestOptions, FullOptions } from './RESTController';*/
  20. /**
  21. * <p>A Parse.Session object is a local representation of a revocable session.
  22. * This class is a subclass of a Parse.Object, and retains the same
  23. * functionality of a Parse.Object.</p>
  24. *
  25. * @alias Parse.Session
  26. * @augments Parse.Object
  27. */
  28. class ParseSession extends _ParseObject.default {
  29. /**
  30. * @param {object} attributes The initial set of data to store in the user.
  31. */
  32. constructor(attributes /*: ?AttributeMap*/) {
  33. super('_Session');
  34. if (attributes && typeof attributes === 'object') {
  35. if (!this.set(attributes || {})) {
  36. throw new Error("Can't create an invalid Session");
  37. }
  38. }
  39. }
  40. /**
  41. * Returns the session token string.
  42. *
  43. * @returns {string}
  44. */
  45. getSessionToken() /*: string*/{
  46. const token = this.get('sessionToken');
  47. if (typeof token === 'string') {
  48. return token;
  49. }
  50. return '';
  51. }
  52. static readOnlyAttributes() {
  53. return ['createdWith', 'expiresAt', 'installationId', 'restricted', 'sessionToken', 'user'];
  54. }
  55. /**
  56. * Retrieves the Session object for the currently logged in session.
  57. *
  58. * @param {object} options useMasterKey
  59. * @static
  60. * @returns {Promise} A promise that is resolved with the Parse.Session
  61. * object after it has been fetched. If there is no current user, the
  62. * promise will be rejected.
  63. */
  64. static current(options /*: FullOptions*/) {
  65. options = options || {};
  66. const controller = _CoreManager.default.getSessionController();
  67. const sessionOptions = {};
  68. if (options.hasOwnProperty('useMasterKey')) {
  69. sessionOptions.useMasterKey = options.useMasterKey;
  70. }
  71. return _ParseUser.default.currentAsync().then(user => {
  72. if (!user) {
  73. return Promise.reject('There is no current user.');
  74. }
  75. sessionOptions.sessionToken = user.getSessionToken();
  76. return controller.getSession(sessionOptions);
  77. });
  78. }
  79. /**
  80. * Determines whether the current session token is revocable.
  81. * This method is useful for migrating Express.js or Node.js web apps to
  82. * use revocable sessions. If you are migrating an app that uses the Parse
  83. * SDK in the browser only, please use Parse.User.enableRevocableSession()
  84. * instead, so that sessions can be automatically upgraded.
  85. *
  86. * @static
  87. * @returns {boolean}
  88. */
  89. static isCurrentSessionRevocable() /*: boolean*/{
  90. const currentUser = _ParseUser.default.current();
  91. if (currentUser) {
  92. return (0, _isRevocableSession.default)(currentUser.getSessionToken() || '');
  93. }
  94. return false;
  95. }
  96. }
  97. _ParseObject.default.registerSubclass('_Session', ParseSession);
  98. const DefaultController = {
  99. getSession(options /*: RequestOptions*/) /*: Promise<ParseSession>*/{
  100. const RESTController = _CoreManager.default.getRESTController();
  101. const session = new ParseSession();
  102. return RESTController.request('GET', 'sessions/me', {}, options).then(sessionData => {
  103. session._finishFetch(sessionData);
  104. session._setExisted(true);
  105. return session;
  106. });
  107. }
  108. };
  109. _CoreManager.default.setSessionController(DefaultController);
  110. var _default = ParseSession;
  111. exports.default = _default;