Skip to content

Instantly share code, notes, and snippets.

@logicware
Created May 19, 2020 04:59
Show Gist options
  • Save logicware/ed037765dc36ea13f44eaa4aced47fa7 to your computer and use it in GitHub Desktop.
Save logicware/ed037765dc36ea13f44eaa4aced47fa7 to your computer and use it in GitHub Desktop.
import sdk from 'matrix-js-sdk';
const matrixClient = sdk.createClient({
baseUrl: process.env.MATRIX_BASE_URL, // https://programination.modular.im
});
async register(req, res) {
let sessionId = null;
const auth = { type: 'm.login.dummy' };
const name = 'matloob-new';
const password = 'testing';
let user = { message: 'Registration Failed!' };
try {
await matrixClient.register(name, password, sessionId, auth);
} catch (e) {
logger.debug(e, { label: '1. Error registering user on Matrix!' });
/*
{
"name":"Unknown error code",
"data":{
"session":"PahfbXbLHhTvcOBtALrdPRAa",
"flows":[
{
"stages":[
"m.login.recaptcha",
"m.login.terms",
"m.login.dummy"
]
},
{
"stages":[
"m.login.recaptcha",
"m.login.terms",
"m.login.email.identity"
]
}
],
"params":{
"m.login.recaptcha":{
"public_key":"6LcgI54UAAAAABGdGmruw6DdOocFpYVdjYBRe4zb"
},
"m.login.terms":{
"policies":{
"privacy_policy":{
"version":"1.0",
"en":{
"name":"Privacy Policy",
"url":"https://programination.modular.im/_matrix/consent?v=1.0"
}
}
}
}
}
},
"httpStatus":401
}
*/
if (e.httpStatus === 401) {
sessionId = e.data.session;
auth.response = e.data.params['m.login.recaptcha'].public_key;
auth.type = e.data.flows[0].stages[0];
auth.session = sessionId;
console.log(auth, sessionId);
try {
user = await matrixClient.register(name, password, sessionId, auth);
logger.debug(user, { label: '3. Success' });
} catch (err) {
logger.debug(err, { label: '2. Error registering user on Matrix!' });
user = err;
}
}
} finally {
return res.status(200)
.send(user);
}
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment