summaryrefslogtreecommitdiff
path: root/app/services/commands/login-command.js
blob: 9931f7a16f63e694ffcfe132cf5f77b9153667d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import * as events from '../events';
import ApplicationCommand from './application-command';

export default class LoginCommand extends ApplicationCommand {
  constructor(eventAggregator, api, applicationStorage) {
    super();
    this.eventAggregator = eventAggregator;
    this.api = api;
    this.applicationStorage = applicationStorage;
  }

  subscribeTo(eventAggregator) {
    eventAggregator.subscribe(events.LOGIN, this);
  }

  run(event) {
    let body = { username: event.username, password: event.password };
    this.api.post('/sessions', body, this.onResponse.bind(this));
  }

  onResponse(json) {
    this.applicationStorage.save('authentication_token', json.authentication_token);
    this.eventAggregator.publish({
      event: events.LOGGED_IN,
      ...json
    });
  }
}