diff --git a/src/matrix/registration/Registration.ts b/src/matrix/registration/Registration.ts index 8b4d54f9..1d44c50d 100644 --- a/src/matrix/registration/Registration.ts +++ b/src/matrix/registration/Registration.ts @@ -17,32 +17,32 @@ limitations under the License. import type {HomeServerApi} from "../net/HomeServerApi"; import {registrationStageFromType} from "./registrationStageFromType"; import type {BaseRegistrationStage} from "./stages/BaseRegistrationStage"; -import type {RegistrationDetails, RegistrationFlow, RegistrationResponse401} from "./types/types"; +import type {AccountDetails, RegistrationFlow, RegistrationResponseMoreDataNeeded} from "./types/types"; type FlowSelector = (flows: RegistrationFlow[]) => RegistrationFlow | void; export class Registration { private _hsApi: HomeServerApi; - private _data: RegistrationDetails; + private _accountDetails: AccountDetails; private _flowSelector: FlowSelector; - constructor(hsApi: HomeServerApi, data: RegistrationDetails, flowSelector?: FlowSelector) { + constructor(hsApi: HomeServerApi, accountDetails: AccountDetails, flowSelector?: FlowSelector) { this._hsApi = hsApi; - this._data = data; + this._accountDetails = accountDetails; this._flowSelector = flowSelector ?? (flows => flows.pop()); } async start(): Promise { const response = await this._hsApi.register( - this._data.username, - this._data.password, - this._data.initialDeviceDisplayName, + this._accountDetails.username, + this._accountDetails.password, + this._accountDetails.initialDeviceDisplayName, undefined, - this._data.inhibitLogin).response(); + this._accountDetails.inhibitLogin).response(); return this.parseStagesFromResponse(response); } - parseStagesFromResponse(response: RegistrationResponse401): BaseRegistrationStage { + parseStagesFromResponse(response: RegistrationResponseMoreDataNeeded): BaseRegistrationStage { const { session, params } = response; const flow = this._flowSelector(response.flows); if (!flow) { @@ -55,7 +55,7 @@ export class Registration { if (!stageClass) { throw new Error(`Unknown stage: ${stage}`); } - const registrationStage = new stageClass(this._hsApi, this._data, session, params?.[stage]); + const registrationStage = new stageClass(this._hsApi, this._accountDetails, session, params?.[stage]); if (!firstStage) { firstStage = registrationStage; lastStage = registrationStage; diff --git a/src/matrix/registration/registrationStageFromType.ts b/src/matrix/registration/registrationStageFromType.ts index 9552d049..799451a7 100644 --- a/src/matrix/registration/registrationStageFromType.ts +++ b/src/matrix/registration/registrationStageFromType.ts @@ -16,11 +16,11 @@ limitations under the License. import type {BaseRegistrationStage} from "./stages/BaseRegistrationStage"; import type {HomeServerApi} from "../net/HomeServerApi"; -import type {RegistrationDetails} from "./types/types"; +import type {AccountDetails, RegistrationParams} from "./types/types"; import {DummyAuth} from "./stages/DummyAuth"; import {TermsAuth} from "./stages/TermsAuth"; -type ClassDerivedFromBaseRegistration = { new(hsApi: HomeServerApi, registrationData: RegistrationDetails, session: string, params?: Record): BaseRegistrationStage } & typeof BaseRegistrationStage; +type ClassDerivedFromBaseRegistration = { new(hsApi: HomeServerApi, registrationData: AccountDetails, session: string, params?: RegistrationParams): BaseRegistrationStage } & typeof BaseRegistrationStage; export function registrationStageFromType(type: string): ClassDerivedFromBaseRegistration | undefined{ switch (type) { diff --git a/src/matrix/registration/stages/BaseRegistrationStage.ts b/src/matrix/registration/stages/BaseRegistrationStage.ts index 7b41379c..88f0bad6 100644 --- a/src/matrix/registration/stages/BaseRegistrationStage.ts +++ b/src/matrix/registration/stages/BaseRegistrationStage.ts @@ -15,18 +15,18 @@ limitations under the License. */ import type {HomeServerApi} from "../../net/HomeServerApi"; -import type {RegistrationDetails, RegistrationResponse, AuthenticationData, RegistrationParams} from "../types/types"; +import type {AccountDetails, RegistrationResponse, AuthenticationData, RegistrationParams} from "../types/types"; export abstract class BaseRegistrationStage { protected _hsApi: HomeServerApi; - protected _registrationData: RegistrationDetails; + protected _accountDetails: AccountDetails; protected _session: string; protected _nextStage: BaseRegistrationStage; protected _params?: Record - constructor(hsApi: HomeServerApi, registrationData: RegistrationDetails, session: string, params?: RegistrationParams) { + constructor(hsApi: HomeServerApi, accountDetails: AccountDetails, session: string, params?: RegistrationParams) { this._hsApi = hsApi; - this._registrationData = registrationData; + this._accountDetails = accountDetails; this._session = session; this._params = params; } @@ -52,7 +52,7 @@ export abstract class BaseRegistrationStage { // registration completed successfully return response.user_id; } - else if ("completed" in response && response.completed?.find(c => c === this.type)) { + else if ("completed" in response && response.completed.find(c => c === this.type)) { return this._nextStage; } const error = "error" in response? response.error: "Could not parse response"; diff --git a/src/matrix/registration/stages/DummyAuth.ts b/src/matrix/registration/stages/DummyAuth.ts index 4a5d9bd8..e22409db 100644 --- a/src/matrix/registration/stages/DummyAuth.ts +++ b/src/matrix/registration/stages/DummyAuth.ts @@ -17,9 +17,8 @@ limitations under the License. import {BaseRegistrationStage} from "./BaseRegistrationStage"; export class DummyAuth extends BaseRegistrationStage { - async complete() { - const { username, password, initialDeviceDisplayName, inhibitLogin } = this._registrationData; + const { username, password, initialDeviceDisplayName, inhibitLogin } = this._accountDetails; const response = await this._hsApi.register(username, password, initialDeviceDisplayName, { session: this._session, type: this.type diff --git a/src/matrix/registration/stages/TermsAuth.ts b/src/matrix/registration/stages/TermsAuth.ts index 7d1647b9..e770b13b 100644 --- a/src/matrix/registration/stages/TermsAuth.ts +++ b/src/matrix/registration/stages/TermsAuth.ts @@ -17,9 +17,8 @@ limitations under the License. import {BaseRegistrationStage} from "./BaseRegistrationStage"; export class TermsAuth extends BaseRegistrationStage { - async complete() { - const { username, password, initialDeviceDisplayName, inhibitLogin } = this._registrationData; + const { username, password, initialDeviceDisplayName, inhibitLogin } = this._accountDetails; const response = await this._hsApi.register(username, password, initialDeviceDisplayName, { session: this._session, type: this.type diff --git a/src/matrix/registration/types/types.ts b/src/matrix/registration/types/types.ts index 5661e268..08fc0fc4 100644 --- a/src/matrix/registration/types/types.ts +++ b/src/matrix/registration/types/types.ts @@ -14,16 +14,16 @@ See the License for the specific language governing permissions and limitations under the License. */ -export type RegistrationDetails = { +export type AccountDetails = { username: string | null; password: string; initialDeviceDisplayName: string; inhibitLogin: boolean; } -export type RegistrationResponse = RegistrationResponse401 | RegistrationResponseError | RegistrationResponseSuccess; +export type RegistrationResponse = RegistrationResponseMoreDataNeeded | RegistrationResponseError | RegistrationResponseSuccess; -export type RegistrationResponse401 = { +export type RegistrationResponseMoreDataNeeded = { completed: string[]; flows: RegistrationFlow[]; params: Record; @@ -46,7 +46,6 @@ export type RegistrationFlow = { } /* Types for Registration Stage */ - export type AuthenticationData = { type: string; session: string;