From 0ad0ecfcc23dd27527f7f6ca0fa7019f044e70da Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Thu, 3 Feb 2022 18:08:19 +0530 Subject: [PATCH] Check response code instead of existence of props --- src/matrix/net/HomeServerRequest.ts | 6 +++++ src/matrix/registration/Registration.ts | 33 ++++++++++++++++--------- src/matrix/registration/types.ts | 3 +++ 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/matrix/net/HomeServerRequest.ts b/src/matrix/net/HomeServerRequest.ts index c3f49834..066d5796 100644 --- a/src/matrix/net/HomeServerRequest.ts +++ b/src/matrix/net/HomeServerRequest.ts @@ -23,6 +23,7 @@ import type {IRequestOptions} from "../../platform/types/types.js"; export interface IHomeServerRequest { abort(): void; response(): Promise; + responseCode(): Promise; } export class HomeServerRequest implements IHomeServerRequest { @@ -110,6 +111,11 @@ export class HomeServerRequest implements IHomeServerRequest { response(): Promise { return this._promise; } + + async responseCode(): Promise { + const response = await this._sourceRequest.response(); + return response.status; + } } import {Request as MockRequest} from "../../mocks/Request.js"; diff --git a/src/matrix/registration/Registration.ts b/src/matrix/registration/Registration.ts index 0d408c2a..9b2c0684 100644 --- a/src/matrix/registration/Registration.ts +++ b/src/matrix/registration/Registration.ts @@ -57,8 +57,11 @@ export class Registration { async submitStage(stage: BaseRegistrationStage): Promise { const auth = stage.generateAuthenticationData(); const { username, password, initialDeviceDisplayName, inhibitLogin } = this._accountDetails; - const response = await this._hsApi.register(username, password, initialDeviceDisplayName, auth, inhibitLogin).response(); - return this.parseRegistrationResponse(response, stage); + const request = this._hsApi.register(username, password, initialDeviceDisplayName, auth, inhibitLogin); + const response = await request.response(); + const status = await request.responseCode(); + const registrationResponse: RegistrationResponse = { ...response, status }; + return this.parseRegistrationResponse(registrationResponse, stage); } parseStagesFromResponse(response: RegistrationResponseMoreDataNeeded): BaseRegistrationStage { @@ -86,17 +89,23 @@ export class Registration { return firstStage!; } - parseRegistrationResponse(response: RegistrationResponse, currentStage: BaseRegistrationStage) { - if ("user_id" in response) { - // registration completed successfully - this._sessionInfo = response; - return undefined; + async parseRegistrationResponse(response: RegistrationResponse, currentStage: BaseRegistrationStage) { + switch (response.status) { + case 200: + this._sessionInfo = response; + return undefined; + case 401: + if (response.completed?.includes(currentStage.type)) { + return currentStage.nextStage; + } + else { + throw new Error("This stage could not be completed!"); + } + case 400: + default: + const error = "error" in response? response.error: "Could not parse response"; + throw new Error(error); } - else if ("completed" in response && response.completed.find(c => c === currentStage.type)) { - return currentStage.nextStage; - } - const error = "error" in response? response.error: "Could not parse response"; - throw new Error(error); } get sessionInfo(): RegistrationResponseSuccess | undefined { diff --git a/src/matrix/registration/types.ts b/src/matrix/registration/types.ts index d79fd33a..77d051af 100644 --- a/src/matrix/registration/types.ts +++ b/src/matrix/registration/types.ts @@ -28,17 +28,20 @@ export type RegistrationResponseMoreDataNeeded = { flows: RegistrationFlow[]; params: Record; session: string; + status: 401; } type RegistrationResponseError = { errcode: string; error: string; + status: 400; } export type RegistrationResponseSuccess = { user_id: string; device_id: string; access_token?: string; + status: 200; } export type RegistrationFlow = {