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.disposeTracked(this._loadViewModel);
} }
this._loadViewModel = this.track(new SessionLoadViewModel(this.childOptions({ this._loadViewModel = this.track(new SessionLoadViewModel(this.childOptions({
createAndStartSessionContainer: () => { createAndStartSessionContainer: async () => {
this._sessionContainer = this._createSessionContainer(); this._sessionContainer = this._createSessionContainer();
this._sessionContainer.queryLogin(homeserver); const loginOptions = await this._sessionContainer.queryLogin(homeserver);
this._sessionContainer.startWithLogin(homeserver, username, password); if (loginOptions.password) {
this._sessionContainer.startWithLogin(loginOptions.password(username, password));
}
return this._sessionContainer; return this._sessionContainer;
}, },
ready: sessionContainer => { ready: sessionContainer => {

View file

@ -38,7 +38,7 @@ export class SessionLoadViewModel extends ViewModel {
try { try {
this._loading = true; this._loading = true;
this.emitChange("loading"); this.emitChange("loading");
this._sessionContainer = this._createAndStartSessionContainer(); this._sessionContainer = await this._createAndStartSessionContainer();
this._waitHandle = this._sessionContainer.loadStatus.waitFor(s => { this._waitHandle = this._sessionContainer.loadStatus.waitFor(s => {
this.emitChange("loadLabel"); this.emitChange("loadLabel");
// wait for initial sync, but not catchup sync // 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 {RequestScheduler} from "./net/RequestScheduler.js";
import {Sync, SyncStatus} from "./Sync.js"; import {Sync, SyncStatus} from "./Sync.js";
import {Session} from "./Session.js"; import {Session} from "./Session.js";
import {PasswordLoginMethod} from "./PasswordLoginMethod.js";
export const LoadStatus = createEnum( export const LoadStatus = createEnum(
"NotLoading", "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) { async queryLogin(homeServer) {
homeServer = normalizeHomeserver(homeServer); homeServer = normalizeHomeserver(homeServer);
const hsApi = new HomeServerApi({homeServer, request: this._platform.request}); 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) { if (this._status.get() !== LoadStatus.NotLoading) {
return; return;
} }
await this._platform.logger.run("login", async log => { await this._platform.logger.run("login", async log => {
this._status.set(LoadStatus.Login); this._status.set(LoadStatus.Login);
homeServer = normalizeHomeserver(homeServer);
const clock = this._platform.clock; const clock = this._platform.clock;
let sessionInfo; let sessionInfo;
try { try {
const request = this._platform.request; const request = this._platform.request;
const homeServer = normalizeHomeserver(loginMethod.homeServer);
const hsApi = new HomeServerApi({homeServer, request}); 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(); const sessionId = this.createNewSessionId();
sessionInfo = { sessionInfo = {
id: sessionId, id: sessionId,