forked from mystiq/hydrogen-web
Refactor code
- Move all code that does /register to Registration.ts - RegistrationStage only deals with the generation of auth data - Change API so that undefined is returned instead of string when registration is over
This commit is contained in:
parent
2aad5546bf
commit
e64f4ad7b2
6 changed files with 56 additions and 31 deletions
|
@ -140,8 +140,7 @@ export class Client {
|
||||||
password,
|
password,
|
||||||
initialDeviceDisplayName,
|
initialDeviceDisplayName,
|
||||||
});
|
});
|
||||||
let stage = await registration.start();
|
return registration;
|
||||||
return stage;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async startWithLogin(loginMethod, {inspectAccountSetup} = {}) {
|
async startWithLogin(loginMethod, {inspectAccountSetup} = {}) {
|
||||||
|
|
|
@ -17,7 +17,13 @@ limitations under the License.
|
||||||
import type {HomeServerApi} from "../net/HomeServerApi";
|
import type {HomeServerApi} from "../net/HomeServerApi";
|
||||||
import {registrationStageFromType} from "./registrationStageFromType";
|
import {registrationStageFromType} from "./registrationStageFromType";
|
||||||
import type {BaseRegistrationStage} from "./stages/BaseRegistrationStage";
|
import type {BaseRegistrationStage} from "./stages/BaseRegistrationStage";
|
||||||
import type {AccountDetails, RegistrationFlow, RegistrationResponseMoreDataNeeded} from "./types";
|
import type {
|
||||||
|
AccountDetails,
|
||||||
|
RegistrationFlow,
|
||||||
|
RegistrationResponseMoreDataNeeded,
|
||||||
|
RegistrationResponse,
|
||||||
|
RegistrationResponseSuccess,
|
||||||
|
} from "./types";
|
||||||
|
|
||||||
type FlowSelector = (flows: RegistrationFlow[]) => RegistrationFlow | void;
|
type FlowSelector = (flows: RegistrationFlow[]) => RegistrationFlow | void;
|
||||||
|
|
||||||
|
@ -25,6 +31,7 @@ export class Registration {
|
||||||
private _hsApi: HomeServerApi;
|
private _hsApi: HomeServerApi;
|
||||||
private _accountDetails: AccountDetails;
|
private _accountDetails: AccountDetails;
|
||||||
private _flowSelector: FlowSelector;
|
private _flowSelector: FlowSelector;
|
||||||
|
private _sessionInfo?: RegistrationResponseSuccess
|
||||||
|
|
||||||
constructor(hsApi: HomeServerApi, accountDetails: AccountDetails, flowSelector?: FlowSelector) {
|
constructor(hsApi: HomeServerApi, accountDetails: AccountDetails, flowSelector?: FlowSelector) {
|
||||||
this._hsApi = hsApi;
|
this._hsApi = hsApi;
|
||||||
|
@ -42,6 +49,18 @@ export class Registration {
|
||||||
return this.parseStagesFromResponse(response);
|
return this.parseStagesFromResponse(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finish a registration stage, return value is:
|
||||||
|
* - the next stage if this stage was completed successfully
|
||||||
|
* - undefined if registration is completed
|
||||||
|
*/
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
parseStagesFromResponse(response: RegistrationResponseMoreDataNeeded): BaseRegistrationStage {
|
parseStagesFromResponse(response: RegistrationResponseMoreDataNeeded): BaseRegistrationStage {
|
||||||
const { session, params } = response;
|
const { session, params } = response;
|
||||||
const flow = this._flowSelector(response.flows);
|
const flow = this._flowSelector(response.flows);
|
||||||
|
@ -66,4 +85,21 @@ export class Registration {
|
||||||
}
|
}
|
||||||
return firstStage!;
|
return firstStage!;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
parseRegistrationResponse(response: RegistrationResponse, currentStage: BaseRegistrationStage) {
|
||||||
|
if ("user_id" in response) {
|
||||||
|
// registration completed successfully
|
||||||
|
this._sessionInfo = response;
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
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 {
|
||||||
|
return this._sessionInfo;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,7 @@ limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import type {HomeServerApi} from "../../net/HomeServerApi";
|
import type {HomeServerApi} from "../../net/HomeServerApi";
|
||||||
import type {AccountDetails, RegistrationResponse, AuthenticationData, RegistrationParams} from "../types";
|
import type {AccountDetails, AuthenticationData, RegistrationParams} from "../types";
|
||||||
|
|
||||||
export abstract class BaseRegistrationStage {
|
export abstract class BaseRegistrationStage {
|
||||||
protected _hsApi: HomeServerApi;
|
protected _hsApi: HomeServerApi;
|
||||||
|
@ -37,25 +37,16 @@ export abstract class BaseRegistrationStage {
|
||||||
abstract get type(): string;
|
abstract get type(): string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Finish a registration stage, return value is:
|
* This method should return auth part that must be provided to
|
||||||
* - the next stage if this stage was completed successfully
|
* /register endpoint to successfully complete this stage
|
||||||
* - user-id (string) if registration is completed
|
|
||||||
*/
|
*/
|
||||||
abstract complete(auth?: AuthenticationData): Promise<BaseRegistrationStage | string>;
|
abstract generateAuthenticationData(): AuthenticationData;
|
||||||
|
|
||||||
setNextStage(stage: BaseRegistrationStage) {
|
setNextStage(stage: BaseRegistrationStage) {
|
||||||
this._nextStage = stage;
|
this._nextStage = stage;
|
||||||
}
|
}
|
||||||
|
|
||||||
parseResponse(response: RegistrationResponse) {
|
get nextStage(): BaseRegistrationStage {
|
||||||
if ("user_id" in response) {
|
return this._nextStage;
|
||||||
// registration completed successfully
|
|
||||||
return response.user_id;
|
|
||||||
}
|
|
||||||
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";
|
|
||||||
throw new Error(error);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,16 +14,15 @@ See the License for the specific language governing permissions and
|
||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import {AuthenticationData} from "../types";
|
||||||
import {BaseRegistrationStage} from "./BaseRegistrationStage";
|
import {BaseRegistrationStage} from "./BaseRegistrationStage";
|
||||||
|
|
||||||
export class DummyAuth extends BaseRegistrationStage {
|
export class DummyAuth extends BaseRegistrationStage {
|
||||||
async complete() {
|
generateAuthenticationData(): AuthenticationData {
|
||||||
const { username, password, initialDeviceDisplayName, inhibitLogin } = this._accountDetails;
|
return {
|
||||||
const response = await this._hsApi.register(username, password, initialDeviceDisplayName, {
|
|
||||||
session: this._session,
|
session: this._session,
|
||||||
type: this.type
|
type: this.type,
|
||||||
}, inhibitLogin).response();
|
};
|
||||||
return this.parseResponse(response);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get type(): string {
|
get type(): string {
|
||||||
|
|
|
@ -14,16 +14,16 @@ See the License for the specific language governing permissions and
|
||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import {AuthenticationData} from "../types";
|
||||||
import {BaseRegistrationStage} from "./BaseRegistrationStage";
|
import {BaseRegistrationStage} from "./BaseRegistrationStage";
|
||||||
|
|
||||||
export class TermsAuth extends BaseRegistrationStage {
|
export class TermsAuth extends BaseRegistrationStage {
|
||||||
async complete() {
|
generateAuthenticationData(): AuthenticationData {
|
||||||
const { username, password, initialDeviceDisplayName, inhibitLogin } = this._accountDetails;
|
return {
|
||||||
const response = await this._hsApi.register(username, password, initialDeviceDisplayName, {
|
|
||||||
session: this._session,
|
session: this._session,
|
||||||
type: this.type
|
type: this.type,
|
||||||
}, inhibitLogin).response();
|
// No other auth data needed for m.login.terms
|
||||||
return this.parseResponse(response);
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
get type(): string {
|
get type(): string {
|
||||||
|
|
|
@ -35,7 +35,7 @@ type RegistrationResponseError = {
|
||||||
error: string;
|
error: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
type RegistrationResponseSuccess = {
|
export type RegistrationResponseSuccess = {
|
||||||
user_id: string;
|
user_id: string;
|
||||||
device_id: string;
|
device_id: string;
|
||||||
access_token?: string;
|
access_token?: string;
|
||||||
|
|
Loading…
Reference in a new issue