Login with custom homeserver

This commit is contained in:
Robert Long
2021-10-14 11:48:05 -07:00
parent 1b1a81441d
commit 85f42cc2d0
2 changed files with 43 additions and 8 deletions

View File

@@ -130,15 +130,34 @@ export function useClient(homeserverUrl) {
});
}, []);
const login = useCallback(async (username, password) => {
const login = useCallback(async (homeserver, username, password) => {
try {
const registrationClient = matrix.createClient(homeserverUrl);
let loginHomeserverUrl = homeserver.trim();
if (!loginHomeserverUrl.includes("://")) {
loginHomeserverUrl = "https://" + loginHomeserverUrl;
}
try {
const wellKnownUrl = new URL(
"/.well-known/matrix/client",
window.location
);
const response = await fetch(wellKnownUrl);
const config = await response.json();
if (config["m.homeserver"]) {
loginHomeserverUrl = config["m.homeserver"];
}
} catch (error) {}
const registrationClient = matrix.createClient(loginHomeserverUrl);
const { user_id, device_id, access_token } =
await registrationClient.loginWithPassword(username, password);
const client = await initClient({
baseUrl: homeserverUrl,
baseUrl: loginHomeserverUrl,
accessToken: access_token,
userId: user_id,
deviceId: device_id,