make startWithFinishedRegistration more broadly useful

This commit is contained in:
Bruno Windels 2022-10-14 10:17:32 +02:00
parent dcba6d1500
commit f46d2c1bf5
3 changed files with 29 additions and 19 deletions

View File

@ -146,18 +146,13 @@ export class Client {
return registration; return registration;
} }
async startWithFinishedRegistration(registration) { /** Method to start client after registration or with given access token.
this._platform.logger.run("startWithFinishedRegistration", async (log) => { * To start the client after registering, use `startWithAuthData(registration.authData)`.
const sessionInfo = registration.sessionInfo; * `homeserver` won't be resolved or normalized using this method,
if (!sessionInfo) { * use `lookupHomeserver` first if needed (not needed after registration) */
throw new Error("Registration.sessionInfo is not available; are you sure that registration is finished?"); async startWithAuthData({accessToken, deviceId, userId, homeserver}) {
} this._platform.logger.run("startWithAuthData", async (log) => {
await this.startWithSessionInfo({ await this._createSessionAfterAuth({accessToken, deviceId, userId, homeserver}, true, log);
accessToken: sessionInfo.access_token,
deviceId: sessionInfo.device_id,
userId: sessionInfo.user_id,
homeserver: registration.homeserver,
}, true, log);
}); });
} }
@ -200,12 +195,12 @@ export class Client {
} }
return; return;
} }
await this.startWithSessionInfo(sessionInfo, inspectAccountSetup, log); await this._createSessionAfterAuth(sessionInfo, inspectAccountSetup, log);
}); });
} }
async startWithSessionInfo({deviceId, userId, accessToken, homeserver}, inspectAccountSetup, log) { async _createSessionAfterAuth({deviceId, userId, accessToken, homeserver}, inspectAccountSetup, log) {
await log.wrap("startWithSessionInfo", async (l) => { await log.wrap("_createSessionAfterAuth", async (l) => {
const id = this.createNewSessionId(); const id = this.createNewSessionId();
const lastUsed = this._platform.clock.now(); const lastUsed = this._platform.clock.now();
const sessionInfo = { const sessionInfo = {

View File

@ -25,6 +25,7 @@ import type {
RegistrationResponseMoreDataNeeded, RegistrationResponseMoreDataNeeded,
RegistrationResponse, RegistrationResponse,
RegistrationResponseSuccess, RegistrationResponseSuccess,
AuthData,
RegistrationParams, RegistrationParams,
} from "./types"; } from "./types";
@ -34,7 +35,7 @@ export class Registration {
private readonly _hsApi: HomeServerApi; private readonly _hsApi: HomeServerApi;
private readonly _accountDetails: AccountDetails; private readonly _accountDetails: AccountDetails;
private readonly _flowSelector: FlowSelector; private readonly _flowSelector: FlowSelector;
private _sessionInfo?: RegistrationResponseSuccess; private _registerResponse?: RegistrationResponseSuccess;
public readonly homeserver: string; public readonly homeserver: string;
constructor(homeserver: string, hsApi: HomeServerApi, accountDetails: AccountDetails, flowSelector?: FlowSelector) { constructor(homeserver: string, hsApi: HomeServerApi, accountDetails: AccountDetails, flowSelector?: FlowSelector) {
@ -93,7 +94,7 @@ export class Registration {
private async parseRegistrationResponse(response: RegistrationResponse, currentStage: BaseRegistrationStage) { private async parseRegistrationResponse(response: RegistrationResponse, currentStage: BaseRegistrationStage) {
switch (response.status) { switch (response.status) {
case 200: case 200:
this._sessionInfo = response; this._registerResponse = response;
return undefined; return undefined;
case 401: case 401:
if (response.completed?.includes(currentStage.type)) { if (response.completed?.includes(currentStage.type)) {
@ -119,7 +120,14 @@ export class Registration {
} }
} }
get sessionInfo(): RegistrationResponseSuccess | undefined { get authData(): AuthData | undefined {
return this._sessionInfo; if (this._registerResponse) {
return {
accessToken: this._registerResponse.access_token,
homeserver: this.homeserver,
userId: this._registerResponse.user_id,
deviceId: this._registerResponse.device_id,
};
}
} }
} }

View File

@ -38,6 +38,13 @@ export type RegistrationResponseSuccess = {
status: 200; status: 200;
} }
export type AuthData = {
userId: string;
deviceId: string;
homeserver: string;
accessToken?: string;
}
export type RegistrationFlow = { export type RegistrationFlow = {
stages: string[]; stages: string[];
} }