mirror of
https://github.com/flarum/framework.git
synced 2024-11-25 09:41:49 +08:00
99876e9e36
An initial stab at flarum/core#126. Still WIP. Preliminary implementation of flarum/core#128 and flarum/core#13.
42 lines
1.1 KiB
JavaScript
42 lines
1.1 KiB
JavaScript
import mixin from 'flarum/utils/mixin';
|
|
import evented from 'flarum/utils/evented';
|
|
|
|
export default class Session extends mixin(class {}, evented) {
|
|
constructor() {
|
|
super();
|
|
this.user = m.prop();
|
|
this.token = m.prop();
|
|
}
|
|
|
|
login(identification, password) {
|
|
var deferred = m.deferred();
|
|
var self = this;
|
|
m.request({
|
|
method: 'POST',
|
|
url: app.forum.attribute('baseUrl')+'/login',
|
|
data: {identification, password},
|
|
background: true
|
|
}).then(function(response) {
|
|
self.token(response.token);
|
|
m.startComputation();
|
|
app.store.find('users', response.userId).then(function(user) {
|
|
self.user(user);
|
|
deferred.resolve(user);
|
|
self.trigger('loggedIn', user);
|
|
m.endComputation();
|
|
});
|
|
}, function(response) {
|
|
deferred.reject(response);
|
|
});
|
|
return deferred.promise;
|
|
}
|
|
|
|
logout() {
|
|
window.location = app.forum.attribute('baseUrl')+'/logout?token='+this.token();
|
|
}
|
|
|
|
authorize(xhr) {
|
|
xhr.setRequestHeader('Authorization', 'Token '+this.token());
|
|
}
|
|
}
|