summaryrefslogtreecommitdiff
path: root/WebInterface/src/js/modules/networking/commands/login/login.js
blob: 44a6c944f9f33ab0dc347406ad862130415b7793 (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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import Command from '../_command';
import LoginModal from '../../../ui/components/modal/login-modal';

/**
 * Handles login to server
 */
export default class Login extends Command {
  /**
   * Registers interface for communication with other objects
   * @param {Interface} iface
   */
  constructor(iface) {
    super(iface);
    this.registerPublic('login', 'sendLogin', 'showLogin');
    this.refreshing = false;
  }

  /**
   * Shows a login modal
   * @param {String} name
   */
  showLogin(name) {
    new LoginModal(this.iface, name);
  }

  /**
   * Registers login response method
   */
  registerLoginResponse() {
    this.iface.callMethod('networker', 'registerHandler', 'LoginResponse',
        (result) => {
          if (result == 0) {
            this.iface.callMethod('modal', 'close');
            this.iface.callMethod('router', 'routePlay');
            this.iface.callMethod('networker', 'removeHandler',
                'LoginResponse');
          } else {
            this.iface.callMethod('modal', 'loginFailed', result);
          }
        });
  }

  /**
   * Sends a login request
   * @param {string} group Group name to join
   * @param {string} password Password to send as SHA-256 Base64 String
   * @param {string} username Display name to use
   */
  sendLogin(group, password, username) {
    this.registerLoginResponse();
    this.iface.callMethod('networker', 'sendRequest', 'Login',
        (err) => console.error(err), group, username, password);
  }
}