mirror of
https://github.com/flarum/core.git
synced 2025-06-03 13:15:03 +02:00
* Fix global typings for extensions * Deprecate global `app` typings See https://github.com/flarum/core/issues/2857#issuecomment-889841326 * Add `app` export for common namespace * Add missing `app` imports within core * Add missing `app` imports to JS files * Fix incorrect import * Fix admin file importing forum `app` * Add `flarum` global variable * Format * Update JSDoc comment * Update JSDoc comment Co-authored-by: Alexander Skvortsov <sasha.skvortsov109@gmail.com> * Fix frontend JS error * Empty commit Co-authored-by: Alexander Skvortsov <sasha.skvortsov109@gmail.com>
57 lines
1.1 KiB
JavaScript
57 lines
1.1 KiB
JavaScript
import app from '../common/app';
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
|
|
/**
|
|
* Attempt to log in a user.
|
|
*
|
|
* @param {String} identification The username/email.
|
|
* @param {String} password
|
|
* @param {Object} [options]
|
|
* @return {Promise}
|
|
* @public
|
|
*/
|
|
login(body, options = {}) {
|
|
return app.request(
|
|
Object.assign(
|
|
{
|
|
method: 'POST',
|
|
url: `${app.forum.attribute('baseUrl')}/login`,
|
|
body,
|
|
},
|
|
options
|
|
)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Log the user out.
|
|
*
|
|
* @public
|
|
*/
|
|
logout() {
|
|
window.location = `${app.forum.attribute('baseUrl')}/logout?token=${this.csrfToken}`;
|
|
}
|
|
}
|