Merge pull request #845 from ibeckermayer/ibeckermayer/ts-conversion-login

Typescript conversion `domain/login`
This commit is contained in:
Bruno Windels 2022-08-17 16:10:10 +00:00 committed by GitHub
commit 869978517e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 68 additions and 35 deletions

View File

@ -14,11 +14,24 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import {ViewModel} from "../ViewModel"; import {Options as BaseOptions, ViewModel} from "../ViewModel";
import {LoginFailure} from "../../matrix/Client.js"; import {LoginFailure} from "../../matrix/Client.js";
import type {TokenLoginMethod} from "../../matrix/login";
import { Client } from "../../matrix/Client.js";
type Options = {
client: Client;
attemptLogin: (loginMethod: TokenLoginMethod) => Promise<null>;
loginToken: string;
} & BaseOptions
export class CompleteSSOLoginViewModel extends ViewModel { export class CompleteSSOLoginViewModel extends ViewModel {
constructor(options) { private _loginToken: string;
private _client: Client;
private _attemptLogin: (loginMethod: TokenLoginMethod) => Promise<null>;
private _errorMessage = "";
constructor(options: Options) {
super(options); super(options);
const { const {
loginToken, loginToken,
@ -29,22 +42,22 @@ export class CompleteSSOLoginViewModel extends ViewModel {
this._client = client; this._client = client;
this._attemptLogin = attemptLogin; this._attemptLogin = attemptLogin;
this._errorMessage = ""; this._errorMessage = "";
this.performSSOLoginCompletion(); void this.performSSOLoginCompletion();
} }
get errorMessage() { return this._errorMessage; } get errorMessage(): string { return this._errorMessage; }
_showError(message) { _showError(message: string): void {
this._errorMessage = message; this._errorMessage = message;
this.emitChange("errorMessage"); this.emitChange("errorMessage");
} }
async performSSOLoginCompletion() { async performSSOLoginCompletion(): Promise<void> {
if (!this._loginToken) { if (!this._loginToken) {
return; return;
} }
const homeserver = await this.platform.settingsStorage.getString("sso_ongoing_login_homeserver"); const homeserver = await this.platform.settingsStorage.getString("sso_ongoing_login_homeserver");
let loginOptions; let loginOptions: { token?: (loginToken: string) => TokenLoginMethod; };
try { try {
loginOptions = await this._client.queryLogin(homeserver).result; loginOptions = await this._client.queryLogin(homeserver).result;
} }

View File

@ -16,9 +16,9 @@ limitations under the License.
import {Client} from "../../matrix/Client.js"; import {Client} from "../../matrix/Client.js";
import {Options as BaseOptions, ViewModel} from "../ViewModel"; import {Options as BaseOptions, ViewModel} from "../ViewModel";
import {PasswordLoginViewModel} from "./PasswordLoginViewModel.js"; import {PasswordLoginViewModel} from "./PasswordLoginViewModel";
import {StartSSOLoginViewModel} from "./StartSSOLoginViewModel.js"; import {StartSSOLoginViewModel} from "./StartSSOLoginViewModel";
import {CompleteSSOLoginViewModel} from "./CompleteSSOLoginViewModel.js"; import {CompleteSSOLoginViewModel} from "./CompleteSSOLoginViewModel";
import {LoadStatus} from "../../matrix/Client.js"; import {LoadStatus} from "../../matrix/Client.js";
import {SessionLoadViewModel} from "../SessionLoadViewModel.js"; import {SessionLoadViewModel} from "../SessionLoadViewModel.js";
import {SegmentType} from "../navigation/index"; import {SegmentType} from "../navigation/index";
@ -60,15 +60,15 @@ export class LoginViewModel extends ViewModel<SegmentType, Options> {
this._initViewModels(); this._initViewModels();
} }
get passwordLoginViewModel(): PasswordLoginViewModel { get passwordLoginViewModel(): PasswordLoginViewModel | undefined {
return this._passwordLoginViewModel; return this._passwordLoginViewModel;
} }
get startSSOLoginViewModel(): StartSSOLoginViewModel { get startSSOLoginViewModel(): StartSSOLoginViewModel | undefined {
return this._startSSOLoginViewModel; return this._startSSOLoginViewModel;
} }
get completeSSOLoginViewModel(): CompleteSSOLoginViewModel { get completeSSOLoginViewModel(): CompleteSSOLoginViewModel | undefined {
return this._completeSSOLoginViewModel; return this._completeSSOLoginViewModel;
} }
@ -285,7 +285,7 @@ export class LoginViewModel extends ViewModel<SegmentType, Options> {
type ReadyFn = (client: Client) => void; type ReadyFn = (client: Client) => void;
// TODO: move to Client.js when its converted to typescript. // TODO: move to Client.js when its converted to typescript.
type LoginOptions = { export type LoginOptions = {
homeserver: string; homeserver: string;
password?: (username: string, password: string) => PasswordLoginMethod; password?: (username: string, password: string) => PasswordLoginMethod;
sso?: SSOLoginHelper; sso?: SSOLoginHelper;

View File

@ -14,43 +14,53 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import {ViewModel} from "../ViewModel";
import {LoginFailure} from "../../matrix/Client.js"; import {LoginFailure} from "../../matrix/Client.js";
import type {PasswordLoginMethod} from "../../matrix/login";
import {Options as BaseOptions, ViewModel} from "../ViewModel";
import type {LoginOptions} from "./LoginViewModel";
type Options = {
loginOptions: LoginOptions | undefined;
attemptLogin: (loginMethod: PasswordLoginMethod) => Promise<null>;
} & BaseOptions
export class PasswordLoginViewModel extends ViewModel { export class PasswordLoginViewModel extends ViewModel {
constructor(options) { private _loginOptions?: LoginOptions;
private _attemptLogin: (loginMethod: PasswordLoginMethod) => Promise<null>;
private _isBusy = false;
private _errorMessage = "";
constructor(options: Options) {
super(options); super(options);
const {loginOptions, attemptLogin} = options; const {loginOptions, attemptLogin} = options;
this._loginOptions = loginOptions; this._loginOptions = loginOptions;
this._attemptLogin = attemptLogin; this._attemptLogin = attemptLogin;
this._isBusy = false;
this._errorMessage = "";
} }
get isBusy() { return this._isBusy; } get isBusy(): boolean { return this._isBusy; }
get errorMessage() { return this._errorMessage; } get errorMessage(): string { return this._errorMessage; }
setBusy(status) { setBusy(status: boolean): void {
this._isBusy = status; this._isBusy = status;
this.emitChange("isBusy"); this.emitChange("isBusy");
} }
_showError(message) { _showError(message: string): void {
this._errorMessage = message; this._errorMessage = message;
this.emitChange("errorMessage"); this.emitChange("errorMessage");
} }
async login(username, password) { async login(username: string, password: string): Promise<void>{
this._errorMessage = ""; this._errorMessage = "";
this.emitChange("errorMessage"); this.emitChange("errorMessage");
const status = await this._attemptLogin(this._loginOptions.password(username, password)); const status = await this._attemptLogin(this._loginOptions!.password!(username, password));
let error = ""; let error = "";
switch (status) { switch (status) {
case LoginFailure.Credentials: case LoginFailure.Credentials:
error = this.i18n`Your username and/or password don't seem to be correct.`; error = this.i18n`Your username and/or password don't seem to be correct.`;
break; break;
case LoginFailure.Connection: case LoginFailure.Connection:
error = this.i18n`Can't connect to ${this._loginOptions.homeserver}.`; error = this.i18n`Can't connect to ${this._loginOptions!.homeserver}.`;
break; break;
case LoginFailure.Unknown: case LoginFailure.Unknown:
error = this.i18n`Something went wrong while checking your login and password.`; error = this.i18n`Something went wrong while checking your login and password.`;

View File

@ -14,25 +14,35 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import {ViewModel} from "../ViewModel"; import type {SSOLoginHelper} from "../../matrix/login";
import {Options as BaseOptions, ViewModel} from "../ViewModel";
import type {LoginOptions} from "./LoginViewModel";
type Options = {
loginOptions: LoginOptions | undefined;
} & BaseOptions;
export class StartSSOLoginViewModel extends ViewModel{ export class StartSSOLoginViewModel extends ViewModel{
constructor(options) { private _sso?: SSOLoginHelper;
private _isBusy = false;
constructor(options: Options) {
super(options); super(options);
this._sso = options.loginOptions.sso; this._sso = options.loginOptions!.sso;
this._isBusy = false; this._isBusy = false;
} }
get isBusy() { return this._isBusy; } get isBusy(): boolean { return this._isBusy; }
setBusy(status) { setBusy(status: boolean): void {
this._isBusy = status; this._isBusy = status;
this.emitChange("isBusy"); this.emitChange("isBusy");
} }
async startSSOLogin() { async startSSOLogin(): Promise<void> {
await this.platform.settingsStorage.setString("sso_ongoing_login_homeserver", this._sso.homeserver); await this.platform.settingsStorage.setString("sso_ongoing_login_homeserver", this._sso!.homeserver);
const link = this._sso.createSSORedirectURL(this.urlCreator.createSSOCallbackURL()); const link = this._sso!.createSSORedirectURL(this.urlCreator.createSSOCallbackURL());
this.platform.openUrl(link); this.platform.openUrl(link);
} }
} }