Newer
Older
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
55
56
57
58
59
import { ViewModel, Client, ObservableValue, LoadStatus } from "hydrogen-view-sdk";
import { IChatterboxConfig } from "../types/IChatterboxConfig";
import { generateRandomString } from "../random";
import "hydrogen-view-sdk/style.css";
export class AccountSetupViewModel extends ViewModel {
private _config: IChatterboxConfig;
private _client: Client;
private _state: ObservableValue<string>;
private _termsStage?: any;
private _username: string;
private _password: string;
constructor(options) {
super(options);
this._client = options.client;
this._config = options.config;
this._state = options.state;
this._startRegistration();
}
private async _startRegistration(): Promise<void> {
this._username = generateRandomString(7);
this._password = generateRandomString(10);
let stage = await this._client.startRegistration(this._homeserver, this._username, this._password, "Chatterbox");
if (stage.type === "m.login.terms") {
this._termsStage = stage;
this.emitChange("termsStage");
}
}
async completeRegistration() {
let stage = this._termsStage;
while (stage !== true) {
stage = await stage.complete();
}
await this.login(this._username, this._password);
}
async login(username: string, password: string): Promise<void> {
const loginOptions = await this._client.queryLogin(this._homeserver).result;
this._client.startWithLogin(loginOptions.password(username, password));
await this._client.loadStatus.waitFor((status: string) => {
return status === LoadStatus.Ready ||
status === LoadStatus.Error ||
status === LoadStatus.LoginFailed;
}).promise;
if (this._client.loginFailure) {
throw new Error("login failed: " + this._client.loginFailure);
} else if (this._client.loadError) {
throw new Error("load failed: " + this._client.loadError.message);
}
this._state.set("timeline");
}