-
-
Notifications
You must be signed in to change notification settings - Fork 595
/
Copy pathParseSession.ts
107 lines (95 loc) · 3.28 KB
/
ParseSession.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import CoreManager from './CoreManager';
import isRevocableSession from './isRevocableSession';
import ParseObject, { Attributes } from './ParseObject';
import ParseUser from './ParseUser';
import type { AttributeKey } from './ParseObject';
import type { RequestOptions, FullOptions } from './RESTController';
/**
* <p>A Parse.Session object is a local representation of a revocable session.
* This class is a subclass of a Parse.Object, and retains the same
* functionality of a Parse.Object.</p>
*
* @alias Parse.Session
* @augments Parse.Object
*/
class ParseSession<T extends Attributes = Attributes> extends ParseObject<T> {
/**
* @param {object} attributes The initial set of data to store in the user.
*/
constructor(attributes?: T) {
super('_Session');
if (attributes && typeof attributes === 'object') {
try {
this.set((attributes || {}) as any);
} catch (_) {
throw new Error("Can't create an invalid Session");
}
}
}
/**
* Returns the session token string.
*
* @returns {string}
*/
getSessionToken(): string {
const token = this.get('sessionToken' as AttributeKey<T>);
if (typeof token === 'string') {
return token;
}
return '';
}
static readOnlyAttributes(): string[] {
return ['createdWith', 'expiresAt', 'installationId', 'restricted', 'sessionToken', 'user'];
}
/**
* Retrieves the Session object for the currently logged in session.
*
* @param {object} options useMasterKey
* @static
* @returns {Promise} A promise that is resolved with the Parse.Session
* object after it has been fetched. If there is no current user, the
* promise will be rejected.
*/
static current<T extends ParseSession>(options?: FullOptions): Promise<T> {
const controller = CoreManager.getSessionController();
const sessionOptions = ParseObject._getRequestOptions(options);
return ParseUser.currentAsync().then(user => {
if (!user) {
return Promise.reject('There is no current user.');
}
sessionOptions.sessionToken = user.getSessionToken();
return controller.getSession(sessionOptions) as Promise<T>;
});
}
/**
* Determines whether the current session token is revocable.
* This method is useful for migrating Express.js or Node.js web apps to
* use revocable sessions. If you are migrating an app that uses the Parse
* SDK in the browser only, please use Parse.User.enableRevocableSession()
* instead, so that sessions can be automatically upgraded.
*
* @static
* @returns {boolean}
*/
static isCurrentSessionRevocable(): boolean {
const currentUser = ParseUser.current();
if (currentUser) {
return isRevocableSession(currentUser.getSessionToken() || '');
}
return false;
}
}
ParseObject.registerSubclass('_Session', ParseSession);
const DefaultController = {
getSession(options?: RequestOptions): Promise<ParseSession> {
const RESTController = CoreManager.getRESTController();
const session = new ParseSession();
return RESTController.request('GET', 'sessions/me', {}, options).then(sessionData => {
session._finishFetch(sessionData);
session._setExisted(true);
return session;
});
},
};
CoreManager.setSessionController(DefaultController);
export default ParseSession;