forked from mystiq/hydrogen-web
Give proper names
This commit is contained in:
parent
fe0add01ee
commit
a351a185a0
6 changed files with 22 additions and 25 deletions
|
@ -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<BaseRegistrationStage> {
|
||||
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;
|
||||
|
|
|
@ -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<string, any>): 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) {
|
||||
|
|
|
@ -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<string, any>
|
||||
|
||||
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";
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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<string, any>;
|
||||
|
@ -46,7 +46,6 @@ export type RegistrationFlow = {
|
|||
}
|
||||
|
||||
/* Types for Registration Stage */
|
||||
|
||||
export type AuthenticationData = {
|
||||
type: string;
|
||||
session: string;
|
||||
|
|
Loading…
Reference in a new issue