Login if the user is already registered

This commit is contained in:
Half-Shot 2022-02-14 10:55:10 +00:00
parent e6083c310f
commit a4af3bc423
2 changed files with 23 additions and 15 deletions

View File

@ -53,6 +53,28 @@ export async function registerUser(username: string, displayname: string, passwo
await new Promise(resolve => setTimeout(resolve, REGISTRATION_RETRY_BASE_DELAY_MS * i * i));
continue;
}
// If already created, try logging in.
if (ex?.body?.errcode === 'M_USER_IN_USE') {
const loginUrl = `${config.homeserverUrl}/_matrix/client/r0/login`
const params = {
uri: loginUrl,
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify({
"type": "m.login.password",
"identifier": {
"type": "m.id.user",
"user": username
},
"password": password
}),
timeout: 60000
}
return await new Promise((resolve, reject) => {
getRequestFn()(params, (error, result) => error ? reject(error) : resolve(result));
});
}
throw ex;
}
}

View File

@ -60,21 +60,7 @@ async function configureMjolnir() {
);
return access_token;
} catch (e) {
if (e.isAxiosError) {
console.log('Received error while registering', e.response.data || e.response);
if (e.response.data && e.response.data.errcode === 'M_USER_IN_USE') {
console.log('mjolnir already registered, skipping');
// Needed for encryption tests
return (await new MatrixClient(config.homeserverUrl, "").doRequest('POST', '/_matrix/client/r0/login', undefined, {
"type": "m.login.password",
"identifier": {
"type": "m.id.user",
"user": "mjolnir"
},
"password": "mjolnir"
})).access_token;
}
}
console.log('Received error while registering', e.response.data || e.response);
throw e;
};
}