2021-08-15 16:21:00 +05:30
|
|
|
/*
|
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-02-14 22:23:59 +05:30
|
|
|
import {ViewModel} from "../ViewModel";
|
2021-12-22 21:39:52 +05:30
|
|
|
import {LoginFailure} from "../../matrix/Client.js";
|
2021-08-15 16:21:00 +05:30
|
|
|
|
|
|
|
export class PasswordLoginViewModel extends ViewModel {
|
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
2021-08-20 23:02:06 +05:30
|
|
|
const {loginOptions, attemptLogin} = options;
|
2021-08-15 16:21:00 +05:30
|
|
|
this._loginOptions = loginOptions;
|
2021-08-20 15:19:42 +05:30
|
|
|
this._attemptLogin = attemptLogin;
|
|
|
|
this._isBusy = false;
|
2021-08-20 21:22:12 +05:30
|
|
|
this._errorMessage = "";
|
2021-08-15 16:21:00 +05:30
|
|
|
}
|
|
|
|
|
2021-08-20 15:19:42 +05:30
|
|
|
get isBusy() { return this._isBusy; }
|
2021-08-20 21:22:12 +05:30
|
|
|
get errorMessage() { return this._errorMessage; }
|
2021-08-15 16:21:00 +05:30
|
|
|
|
2021-08-23 16:09:40 +05:30
|
|
|
setBusy(status) {
|
|
|
|
this._isBusy = status;
|
2021-08-20 15:19:42 +05:30
|
|
|
this.emitChange("isBusy");
|
2021-08-15 16:21:00 +05:30
|
|
|
}
|
|
|
|
|
2021-08-20 21:22:12 +05:30
|
|
|
_showError(message) {
|
|
|
|
this._errorMessage = message;
|
|
|
|
this.emitChange("errorMessage");
|
|
|
|
}
|
|
|
|
|
2021-08-18 13:05:05 +05:30
|
|
|
async login(username, password) {
|
2021-08-23 15:28:44 +05:30
|
|
|
this._errorMessage = "";
|
|
|
|
this.emitChange("errorMessage");
|
2021-08-23 22:56:39 +05:30
|
|
|
const status = await this._attemptLogin(this._loginOptions.password(username, password));
|
2021-08-20 15:19:42 +05:30
|
|
|
let error = "";
|
|
|
|
switch (status) {
|
|
|
|
case LoginFailure.Credentials:
|
2021-08-20 20:36:33 +05:30
|
|
|
error = this.i18n`Your username and/or password don't seem to be correct.`;
|
2021-08-20 15:19:42 +05:30
|
|
|
break;
|
|
|
|
case LoginFailure.Connection:
|
2021-08-23 22:56:39 +05:30
|
|
|
error = this.i18n`Can't connect to ${this._loginOptions.homeserver}.`;
|
2021-08-20 15:19:42 +05:30
|
|
|
break;
|
|
|
|
case LoginFailure.Unknown:
|
2021-08-20 20:36:33 +05:30
|
|
|
error = this.i18n`Something went wrong while checking your login and password.`;
|
2021-08-20 15:19:42 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (error) {
|
|
|
|
this._showError(error);
|
2021-08-15 16:21:00 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|