Convert src/Login.js to TypeScript
This commit is contained in:
parent
48da4c2abd
commit
11eb9b59e6
2 changed files with 90 additions and 48 deletions
|
@ -19,34 +19,70 @@ limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import Matrix from "matrix-js-sdk";
|
import Matrix from "matrix-js-sdk";
|
||||||
|
import { MatrixClient } from "matrix-js-sdk/src/client";
|
||||||
|
import { IMatrixClientCreds } from "./MatrixClientPeg";
|
||||||
|
|
||||||
|
interface ILoginOptions {
|
||||||
|
defaultDeviceDisplayName?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Move this to JS SDK
|
||||||
|
interface ILoginFlow {
|
||||||
|
type: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Move this to JS SDK
|
||||||
|
/* eslint-disable camelcase */
|
||||||
|
interface ILoginParams {
|
||||||
|
identifier?: string;
|
||||||
|
password?: string;
|
||||||
|
token?: string;
|
||||||
|
device_id?: string;
|
||||||
|
initial_device_display_name?: string;
|
||||||
|
}
|
||||||
|
/* eslint-enable camelcase */
|
||||||
|
|
||||||
export default class Login {
|
export default class Login {
|
||||||
constructor(hsUrl, isUrl, fallbackHsUrl, opts) {
|
private hsUrl: string;
|
||||||
this._hsUrl = hsUrl;
|
private isUrl: string;
|
||||||
this._isUrl = isUrl;
|
private fallbackHsUrl: string;
|
||||||
this._fallbackHsUrl = fallbackHsUrl;
|
private currentFlowIndex: number;
|
||||||
this._currentFlowIndex = 0;
|
// TODO: Flows need a type in JS SDK
|
||||||
this._flows = [];
|
private flows: Array<ILoginFlow>;
|
||||||
this._defaultDeviceDisplayName = opts.defaultDeviceDisplayName;
|
private defaultDeviceDisplayName: string;
|
||||||
this._tempClient = null; // memoize
|
private tempClient: MatrixClient;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
hsUrl: string,
|
||||||
|
isUrl: string,
|
||||||
|
fallbackHsUrl?: string,
|
||||||
|
opts?: ILoginOptions,
|
||||||
|
) {
|
||||||
|
this.hsUrl = hsUrl;
|
||||||
|
this.isUrl = isUrl;
|
||||||
|
this.fallbackHsUrl = fallbackHsUrl;
|
||||||
|
this.currentFlowIndex = 0;
|
||||||
|
this.flows = [];
|
||||||
|
this.defaultDeviceDisplayName = opts.defaultDeviceDisplayName;
|
||||||
|
this.tempClient = null; // memoize
|
||||||
}
|
}
|
||||||
|
|
||||||
getHomeserverUrl() {
|
public getHomeserverUrl(): string {
|
||||||
return this._hsUrl;
|
return this.hsUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
getIdentityServerUrl() {
|
public getIdentityServerUrl(): string {
|
||||||
return this._isUrl;
|
return this.isUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
setHomeserverUrl(hsUrl) {
|
public setHomeserverUrl(hsUrl: string): void {
|
||||||
this._tempClient = null; // clear memoization
|
this.tempClient = null; // clear memoization
|
||||||
this._hsUrl = hsUrl;
|
this.hsUrl = hsUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
setIdentityServerUrl(isUrl) {
|
public setIdentityServerUrl(isUrl: string): void {
|
||||||
this._tempClient = null; // clear memoization
|
this.tempClient = null; // clear memoization
|
||||||
this._isUrl = isUrl;
|
this.isUrl = isUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -54,40 +90,41 @@ export default class Login {
|
||||||
* requests.
|
* requests.
|
||||||
* @returns {MatrixClient}
|
* @returns {MatrixClient}
|
||||||
*/
|
*/
|
||||||
createTemporaryClient() {
|
public createTemporaryClient(): MatrixClient {
|
||||||
if (this._tempClient) return this._tempClient; // use memoization
|
if (this.tempClient) return this.tempClient; // use memoization
|
||||||
return this._tempClient = Matrix.createClient({
|
return this.tempClient = Matrix.createClient({
|
||||||
baseUrl: this._hsUrl,
|
baseUrl: this.hsUrl,
|
||||||
idBaseUrl: this._isUrl,
|
idBaseUrl: this.isUrl,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
getFlows() {
|
public async getFlows(): Promise<Array<ILoginFlow>> {
|
||||||
const self = this;
|
|
||||||
const client = this.createTemporaryClient();
|
const client = this.createTemporaryClient();
|
||||||
return client.loginFlows().then(function(result) {
|
const { flows } = await client.loginFlows();
|
||||||
self._flows = result.flows;
|
this.flows = flows;
|
||||||
self._currentFlowIndex = 0;
|
this.currentFlowIndex = 0;
|
||||||
// technically the UI should display options for all flows for the
|
// technically the UI should display options for all flows for the
|
||||||
// user to then choose one, so return all the flows here.
|
// user to then choose one, so return all the flows here.
|
||||||
return self._flows;
|
return this.flows;
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
chooseFlow(flowIndex) {
|
public chooseFlow(flowIndex): void {
|
||||||
this._currentFlowIndex = flowIndex;
|
this.currentFlowIndex = flowIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
getCurrentFlowStep() {
|
public getCurrentFlowStep(): string {
|
||||||
// technically the flow can have multiple steps, but no one does this
|
// technically the flow can have multiple steps, but no one does this
|
||||||
// for login so we can ignore it.
|
// for login so we can ignore it.
|
||||||
const flowStep = this._flows[this._currentFlowIndex];
|
const flowStep = this.flows[this.currentFlowIndex];
|
||||||
return flowStep ? flowStep.type : null;
|
return flowStep ? flowStep.type : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
loginViaPassword(username, phoneCountry, phoneNumber, pass) {
|
public loginViaPassword(
|
||||||
const self = this;
|
username: string,
|
||||||
|
phoneCountry: string,
|
||||||
|
phoneNumber: string,
|
||||||
|
password: string,
|
||||||
|
): Promise<IMatrixClientCreds> {
|
||||||
const isEmail = username.indexOf("@") > 0;
|
const isEmail = username.indexOf("@") > 0;
|
||||||
|
|
||||||
let identifier;
|
let identifier;
|
||||||
|
@ -113,14 +150,14 @@ export default class Login {
|
||||||
}
|
}
|
||||||
|
|
||||||
const loginParams = {
|
const loginParams = {
|
||||||
password: pass,
|
password,
|
||||||
identifier: identifier,
|
identifier,
|
||||||
initial_device_display_name: this._defaultDeviceDisplayName,
|
initial_device_display_name: this.defaultDeviceDisplayName,
|
||||||
};
|
};
|
||||||
|
|
||||||
const tryFallbackHs = (originalError) => {
|
const tryFallbackHs = (originalError) => {
|
||||||
return sendLoginRequest(
|
return sendLoginRequest(
|
||||||
self._fallbackHsUrl, this._isUrl, 'm.login.password', loginParams,
|
this.fallbackHsUrl, this.isUrl, 'm.login.password', loginParams,
|
||||||
).catch((fallbackError) => {
|
).catch((fallbackError) => {
|
||||||
console.log("fallback HS login failed", fallbackError);
|
console.log("fallback HS login failed", fallbackError);
|
||||||
// throw the original error
|
// throw the original error
|
||||||
|
@ -130,11 +167,11 @@ export default class Login {
|
||||||
|
|
||||||
let originalLoginError = null;
|
let originalLoginError = null;
|
||||||
return sendLoginRequest(
|
return sendLoginRequest(
|
||||||
self._hsUrl, self._isUrl, 'm.login.password', loginParams,
|
this.hsUrl, this.isUrl, 'm.login.password', loginParams,
|
||||||
).catch((error) => {
|
).catch((error) => {
|
||||||
originalLoginError = error;
|
originalLoginError = error;
|
||||||
if (error.httpStatus === 403) {
|
if (error.httpStatus === 403) {
|
||||||
if (self._fallbackHsUrl) {
|
if (this.fallbackHsUrl) {
|
||||||
return tryFallbackHs(originalLoginError);
|
return tryFallbackHs(originalLoginError);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -154,11 +191,16 @@ export default class Login {
|
||||||
* @param {string} hsUrl the base url of the Homeserver used to log in.
|
* @param {string} hsUrl the base url of the Homeserver used to log in.
|
||||||
* @param {string} isUrl the base url of the default identity server
|
* @param {string} isUrl the base url of the default identity server
|
||||||
* @param {string} loginType the type of login to do
|
* @param {string} loginType the type of login to do
|
||||||
* @param {object} loginParams the parameters for the login
|
* @param {ILoginParams} loginParams the parameters for the login
|
||||||
*
|
*
|
||||||
* @returns {MatrixClientCreds}
|
* @returns {MatrixClientCreds}
|
||||||
*/
|
*/
|
||||||
export async function sendLoginRequest(hsUrl, isUrl, loginType, loginParams) {
|
export async function sendLoginRequest(
|
||||||
|
hsUrl: string,
|
||||||
|
isUrl: string,
|
||||||
|
loginType: string,
|
||||||
|
loginParams: ILoginParams,
|
||||||
|
): Promise<IMatrixClientCreds> {
|
||||||
const client = Matrix.createClient({
|
const client = Matrix.createClient({
|
||||||
baseUrl: hsUrl,
|
baseUrl: hsUrl,
|
||||||
idBaseUrl: isUrl,
|
idBaseUrl: isUrl,
|
|
@ -40,7 +40,7 @@ export interface IMatrixClientCreds {
|
||||||
userId: string;
|
userId: string;
|
||||||
deviceId: string;
|
deviceId: string;
|
||||||
accessToken: string;
|
accessToken: string;
|
||||||
guest: boolean;
|
guest?: boolean;
|
||||||
pickleKey?: string;
|
pickleKey?: string;
|
||||||
freshLogin?: boolean;
|
freshLogin?: boolean;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue