Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Move doLogin into LoginHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
pinusc committed Jan 20, 2024
1 parent c0ea6d2 commit e92ce41
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 48 deletions.
55 changes: 8 additions & 47 deletions src/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,52 +57,6 @@ class Login extends Component<{}, LoginState> {
}
};

doLogin = () => {
if (!this.selectedLoginFlow) {
throw new Error("selectedLoginFlow is not defined");
}
switch (this.selectedLoginFlow.type) {
case "m.login.password":
shared.mClient
.loginWithPassword(
`@${this.username}:${this.homeserverName}`,
this.password
)
.then((result: any) => {
if (result.well_known) {
localforage.setItem("well_known", result.well_known).then(() => {
console.log("Received a well_known from client login property. Updating previous settings.")
});
}
localforage.setItem("login", result).then(() => {
alert("Logged in as " + this.username);
window.location = window.location;
});
})
.catch((err: any) => {
switch (err.errcode) {
case "M_FORBIDDEN":
alert("Incorrect login credentials");
break;
case "M_USER_DEACTIVATED":
alert("This account has been deactivated");
break;
case "M_LIMIT_EXCEEDED":
const retry = Math.ceil(err.retry_after_ms / 1000);
alert("Too many requests! Retry after" + retry.toString());
break;
default:
alert("Login failed for some unknown reason");
break;
}
});
break;
default:
alert("Invalid/unsupported login method. This is likely a bug");
break;
}
};

rightCb = () => {
switch (this.state.stage) {
case 0:
Expand All @@ -122,7 +76,14 @@ class Login extends Component<{}, LoginState> {
}
break;
case 2:
this.doLogin();
let loginData = {'username': this.username, 'password': this.password};
if (this.selectedLoginFlow !== undefined) {
this.loginHandler.doLogin(this.selectedLoginFlow, loginData).then(() => {
window.location = window.location;
}).catch((e) => alert(e.message));
} else {
throw new Error("Undefined selectedLoginFlow")
}
break;
default:
alert("Invalid stage!");
Expand Down
59 changes: 58 additions & 1 deletion src/LoginHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,66 @@ export default class LoginHandler {
this.base_url = "";
}

private setWellKnown(well_known: WellKnown) {
return localforage.setItem("well_known", well_known);
}

public async doLogin(loginFlow: LoginFlow, loginData: any) {
// TODO implement more login flows
// Instead of implementing them one by one, consider using mClient.login
// and passing loginData (which needs to be properly formed according to their spec)
// (may or may not be a bad idea)
try {
let loginResult: any;
let username: string = `@${loginData.username}:${this.homeserverName}`;
switch (loginFlow.type) {
case "m.login.password":
let password: string = loginData.password;
loginResult = await shared.mClient
.loginWithPassword(username, password);
break;
default:
throw new Error("Unsupported");
break;
}
if (loginResult.well_known) {
this.setWellKnown(loginResult.well_known)
console.log("Received a well_known from client login property. Updating previous settings.")
console.log(loginResult.well_known)
}
await localforage.setItem("login", loginResult);
alert("Logged in as " + username);
} catch (e: any) {
let message: string;
switch (e.errcode) {
case "M_FORBIDDEN":
message = "Incorrect login credentials";
break;
case "M_USER_DEACTIVATED":
message = "This account has been deactivated";
break;
case "M_LIMIT_EXCEEDED":
const retry = Math.ceil(e.retry_after_ms / 1000);
message = `Too many requests! Retry after ${retry.toString()}`;
break;
default:
if (e.message === "Unsupported") {
message = "Login flow selected is unsupported"
} else if (e.errcode) {
message = e.errcode;
} else {
message = `Login failed for some unknown reason: ${e.message}`;
}
break;
}
throw new Error(message)
}
}

public async findHomeserver(name: string) {
name = name.replace("https://", "");
name = name.replace("http://", "");
this.homeserverName = name;
let base_url: string = "";
let well_known_url = `https://${name}/.well-known/matrix/client`;
try {
Expand All @@ -38,7 +95,7 @@ export default class LoginHandler {
base_url = "https://" + name;
} finally {
this.base_url = base_url;
localforage.setItem("well_known", {
this.setWellKnown({
"m.homeserver": {"base_url": base_url},
"m.identity_server": {"base_url": "https://vector.im"}, // TODO Where to infer this outside of actual .well-known?
})
Expand Down

0 comments on commit e92ce41

Please sign in to comment.