forked from mystiq/hydrogen-web
Check response code instead of existence of props
This commit is contained in:
parent
c4894f2c24
commit
0ad0ecfcc2
3 changed files with 30 additions and 12 deletions
|
@ -23,6 +23,7 @@ import type {IRequestOptions} from "../../platform/types/types.js";
|
|||
export interface IHomeServerRequest {
|
||||
abort(): void;
|
||||
response(): Promise<any>;
|
||||
responseCode(): Promise<number>;
|
||||
}
|
||||
|
||||
export class HomeServerRequest implements IHomeServerRequest {
|
||||
|
@ -110,6 +111,11 @@ export class HomeServerRequest implements IHomeServerRequest {
|
|||
response(): Promise<any> {
|
||||
return this._promise;
|
||||
}
|
||||
|
||||
async responseCode(): Promise<number> {
|
||||
const response = await this._sourceRequest.response();
|
||||
return response.status;
|
||||
}
|
||||
}
|
||||
|
||||
import {Request as MockRequest} from "../../mocks/Request.js";
|
||||
|
|
|
@ -57,8 +57,11 @@ export class Registration {
|
|||
async submitStage(stage: BaseRegistrationStage): Promise<BaseRegistrationStage | undefined> {
|
||||
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,18 +89,24 @@ export class Registration {
|
|||
return firstStage!;
|
||||
}
|
||||
|
||||
parseRegistrationResponse(response: RegistrationResponse, currentStage: BaseRegistrationStage) {
|
||||
if ("user_id" in response) {
|
||||
// registration completed successfully
|
||||
async parseRegistrationResponse(response: RegistrationResponse, currentStage: BaseRegistrationStage) {
|
||||
switch (response.status) {
|
||||
case 200:
|
||||
this._sessionInfo = response;
|
||||
return undefined;
|
||||
}
|
||||
else if ("completed" in response && response.completed.find(c => c === currentStage.type)) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
get sessionInfo(): RegistrationResponseSuccess | undefined {
|
||||
return this._sessionInfo;
|
||||
|
|
|
@ -28,17 +28,20 @@ export type RegistrationResponseMoreDataNeeded = {
|
|||
flows: RegistrationFlow[];
|
||||
params: Record<string, any>;
|
||||
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 = {
|
||||
|
|
Loading…
Reference in a new issue