Rewrite password login to use PasswordLoginMethod

Signed-off-by: RMidhunSuresh <rmidhunsuresh@gmail.com>
This commit is contained in:
RMidhunSuresh 2021-07-30 13:45:35 +05:30
parent 20765d9688
commit a53e29767f
3 changed files with 26 additions and 8 deletions

View File

@ -47,10 +47,12 @@ export class LoginViewModel extends ViewModel {
this._loadViewModel = this.disposeTracked(this._loadViewModel);
}
this._loadViewModel = this.track(new SessionLoadViewModel(this.childOptions({
createAndStartSessionContainer: () => {
createAndStartSessionContainer: async () => {
this._sessionContainer = this._createSessionContainer();
this._sessionContainer.queryLogin(homeserver);
this._sessionContainer.startWithLogin(homeserver, username, password);
const loginOptions = await this._sessionContainer.queryLogin(homeserver);
if (loginOptions.password) {
this._sessionContainer.startWithLogin(loginOptions.password(username, password));
}
return this._sessionContainer;
},
ready: sessionContainer => {

View File

@ -38,7 +38,7 @@ export class SessionLoadViewModel extends ViewModel {
try {
this._loading = true;
this.emitChange("loading");
this._sessionContainer = this._createAndStartSessionContainer();
this._sessionContainer = await this._createAndStartSessionContainer();
this._waitHandle = this._sessionContainer.loadStatus.waitFor(s => {
this.emitChange("loadLabel");
// wait for initial sync, but not catchup sync

View File

@ -23,6 +23,7 @@ import {MediaRepository} from "./net/MediaRepository.js";
import {RequestScheduler} from "./net/RequestScheduler.js";
import {Sync, SyncStatus} from "./Sync.js";
import {Session} from "./Session.js";
import {PasswordLoginMethod} from "./PasswordLoginMethod.js";
export const LoadStatus = createEnum(
"NotLoading",
@ -97,25 +98,40 @@ export class SessionContainer {
});
}
parseLoginOptions(options, homeServer) {
/* Take server response and return new object which has two props password and sso which
implements LoginMethod
*/
const flows = options.flows;
const result = {};
for (const flow of flows) {
if (flow.type === "m.login.password") {
result.password = (username, password) => new PasswordLoginMethod({homeServer, username, password, platform: this._platform});
}
}
return result;
}
async queryLogin(homeServer) {
homeServer = normalizeHomeserver(homeServer);
const hsApi = new HomeServerApi({homeServer, request: this._platform.request});
const response = hsApi.queryLogin();
const response = await hsApi.queryLogin().response();
return this.parseLoginOptions(response, homeServer);
}
async startWithLogin(homeServer, username, password) {
async startWithLogin(loginMethod) {
if (this._status.get() !== LoadStatus.NotLoading) {
return;
}
await this._platform.logger.run("login", async log => {
this._status.set(LoadStatus.Login);
homeServer = normalizeHomeserver(homeServer);
const clock = this._platform.clock;
let sessionInfo;
try {
const request = this._platform.request;
const homeServer = normalizeHomeserver(loginMethod.homeServer);
const hsApi = new HomeServerApi({homeServer, request});
const loginData = await hsApi.passwordLogin(username, password, "Hydrogen", {log}).response();
const loginData = await loginMethod.login(hsApi, "Hydrogen", log);
const sessionId = this.createNewSessionId();
sessionInfo = {
id: sessionId,