mirror of
1
2
Fork 0
ucaptcha/src/shared/models/UserSession.js

63 lines
1.1 KiB
JavaScript
Raw Normal View History

/**
* User session model
* @type {Object} Session Object.
* @property {string} _sessionId The session identifier
2020-03-19 08:35:50 +01:00
* @property {string} _websiteKey The website id for session
2020-03-16 10:31:57 +01:00
*/
export class UserSession {
/**
* Set session ID
2020-03-15 14:09:39 +01:00
* @param {string} id
*/
2020-03-16 10:31:57 +01:00
set sessionId(id) {
2020-03-15 14:09:39 +01:00
this._sessionId = id;
2020-03-15 09:05:18 +01:00
}
2020-03-15 14:09:39 +01:00
/**
2020-03-16 10:31:57 +01:00
* Get the session ID
* @type {string}
2020-03-15 14:09:39 +01:00
*/
2020-03-16 10:31:57 +01:00
get sessionId() {
2020-03-15 14:09:39 +01:00
return this._sessionId;
2020-03-15 09:05:18 +01:00
}
2020-03-19 08:35:50 +01:00
/**
* Set session ID
* @param {string} id
*/
set websiteKey(id) {
this._websiteKey = id;
}
/**
* Get the session ID
* @type {string}
*/
get websiteKey() {
return this._websiteKey;
}
2020-03-15 14:09:39 +01:00
/**
* Serialize the session into a JSON object
* @return {Array<any>}
*/
serialize() {
2020-03-19 08:35:50 +01:00
const {sessionId, websiteKey} = this;
if (!sessionId || !websiteKey) {
throw Error();
2020-03-15 09:05:18 +01:00
}
2020-03-19 08:35:50 +01:00
return [sessionId, websiteKey];
2020-03-15 09:05:18 +01:00
}
2020-03-15 14:09:39 +01:00
/**
* Deserialize session data from JSON object
* @param {Array<any>} payload
*/
2020-03-19 08:35:50 +01:00
deserialize([sessionId, websiteKey]) {
Object.assign(this, {sessionId, websiteKey});
2020-03-15 09:05:18 +01:00
}
};
2020-03-16 11:17:11 +01:00
export default UserSession
2020-03-16 11:17:11 +01:00
;