ParseSession.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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
  10. */
  11. import CoreManager from './CoreManager';
  12. import isRevocableSession from './isRevocableSession';
  13. import ParseObject from './ParseObject';
  14. import ParseUser from './ParseUser';
  15. /*:: import type { AttributeMap } from './ObjectStateMutations';*/
  16. /*:: import type { RequestOptions, FullOptions } from './RESTController';*/
  17. /**
  18. * <p>A Parse.Session object is a local representation of a revocable session.
  19. * This class is a subclass of a Parse.Object, and retains the same
  20. * functionality of a Parse.Object.</p>
  21. * @alias Parse.Session
  22. * @extends Parse.Object
  23. */
  24. class ParseSession extends ParseObject {
  25. /**
  26. *
  27. * @param {Object} attributes The initial set of data to store in the user.
  28. */
  29. constructor(attributes
  30. /*: ?AttributeMap*/
  31. ) {
  32. super('_Session');
  33. if (attributes && typeof attributes === 'object') {
  34. if (!this.set(attributes || {})) {
  35. throw new Error('Can\'t create an invalid Session');
  36. }
  37. }
  38. }
  39. /**
  40. * Returns the session token string.
  41. * @return {String}
  42. */
  43. getSessionToken()
  44. /*: string*/
  45. {
  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. * @static
  58. * @return {Promise} A promise that is resolved with the Parse.Session
  59. * object after it has been fetched. If there is no current user, the
  60. * promise will be rejected.
  61. */
  62. static current(options
  63. /*: FullOptions*/
  64. ) {
  65. options = options || {};
  66. const controller = CoreManager.getSessionController();
  67. const sessionOptions = {};
  68. if (options.hasOwnProperty('useMasterKey')) {
  69. sessionOptions.useMasterKey = options.useMasterKey;
  70. }
  71. return ParseUser.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. * @static
  86. * @return {Boolean}
  87. */
  88. static isCurrentSessionRevocable()
  89. /*: boolean*/
  90. {
  91. const currentUser = ParseUser.current();
  92. if (currentUser) {
  93. return isRevocableSession(currentUser.getSessionToken() || '');
  94. }
  95. return false;
  96. }
  97. }
  98. ParseObject.registerSubclass('_Session', ParseSession);
  99. const DefaultController = {
  100. getSession(options
  101. /*: RequestOptions*/
  102. )
  103. /*: Promise<ParseSession>*/
  104. {
  105. const RESTController = CoreManager.getRESTController();
  106. const session = new ParseSession();
  107. return RESTController.request('GET', 'sessions/me', {}, options).then(sessionData => {
  108. session._finishFetch(sessionData);
  109. session._setExisted(true);
  110. return session;
  111. });
  112. }
  113. };
  114. CoreManager.setSessionController(DefaultController);
  115. export default ParseSession;