framework/js/lib/Session.js

50 lines
1017 B
JavaScript
Raw Normal View History

/**
* The `Session` class defines the current user session. It stores a reference
* to the current authenticated user, and provides methods to log in/out.
*/
export default class Session {
constructor(user, csrfToken) {
/**
* The current authenticated user.
*
* @type {User|null}
* @public
*/
this.user = user;
/**
* The CSRF token.
*
* @type {String|null}
* @public
*/
this.csrfToken = csrfToken;
2015-04-25 20:58:39 +08:00
}
/**
* Attempt to log in a user.
*
* @param {String} identification The username/email.
* @param {String} password
* @param {Object} [options]
* @return {Promise}
* @public
*/
login(data, options = {}) {
return app.request(Object.assign({
2015-04-25 20:58:39 +08:00
method: 'POST',
url: app.forum.attribute('baseUrl') + '/login',
data
}, options));
2015-04-25 20:58:39 +08:00
}
/**
* Log the user out.
*
* @public
*/
2015-04-25 20:58:39 +08:00
logout() {
window.location = app.forum.attribute('baseUrl') + '/logout?token=' + this.csrfToken;
2015-04-25 20:58:39 +08:00
}
}