master
 1import * as events from '../events';
 2import ApplicationCommand from './application-command';
 3
 4export default class LoginCommand extends ApplicationCommand {
 5  constructor(eventAggregator, api, applicationStorage) {
 6    super();
 7    this.eventAggregator = eventAggregator;
 8    this.api = api;
 9    this.applicationStorage = applicationStorage;
10  }
11
12  subscribeTo(eventAggregator) {
13    eventAggregator.subscribe(events.LOGIN, this);
14  }
15
16  run(event) {
17    let body = { username: event.username, password: event.password };
18    this.api.post('/sessions', body, this.onResponse.bind(this));
19  }
20
21  onResponse(json) {
22    this.applicationStorage.save('authentication_token', json.authentication_token);
23    this.eventAggregator.publish({
24      event: events.LOGGED_IN,
25      ...json
26    });
27  }
28}