- When checking authentication we should retry startup if the error we receive isn't related to invalid auth token (M_UNKNOWN_TOKEN)

This commit is contained in:
Skylar Sadlier 2022-02-03 10:15:52 -07:00
parent 9f0a962991
commit b44629c84e

View File

@ -38,6 +38,8 @@ module.exports = function(RED) {
localStorage = new LocalStorage(localStorageDir), localStorage = new LocalStorage(localStorageDir),
initialSetup = false; initialSetup = false;
let retryStartTimeout = null;
if(!this.credentials.accessToken) { if(!this.credentials.accessToken) {
node.log("Matrix connection failed: missing access token."); node.log("Matrix connection failed: missing access token.");
} else if(!this.url) { } else if(!this.url) {
@ -121,6 +123,10 @@ module.exports = function(RED) {
node.matrixClient.stopClient(); node.matrixClient.stopClient();
node.setConnected(false); node.setConnected(false);
} }
if(retryStartTimeout) {
clearTimeout(retryStartTimeout);
}
} }
node.on('close', function(done) { node.on('close', function(done) {
@ -290,6 +296,7 @@ module.exports = function(RED) {
// httpStatus: 401 // httpStatus: 401
// } // }
console.log("Authentication failure: ", errorObj);
node.error("Authentication failure: " + errorObj); node.error("Authentication failure: " + errorObj);
stopClient(); stopClient();
}); });
@ -312,13 +319,25 @@ module.exports = function(RED) {
// do an authed request and only continue if we don't get an error // do an authed request and only continue if we don't get an error
// this prevent the matrix client from crashing Node-RED on invalid auth token // this prevent the matrix client from crashing Node-RED on invalid auth token
node.matrixClient.getAccountDataFromServer() (function checkAuthTokenThenStart() {
.then( if(node.matrixClient.clientRunning) {
function() { return;
run().catch((error) => node.error(error)); }
},
function(err) {} node.matrixClient.getAccountDataFromServer()
); .then(
function() {
run().catch((error) => node.error(error));
},
function(err) {
// if the error isn't authentication related retry in a little bit
if(err.code !== "M_UNKNOWN_TOKEN") {
retryStartTimeout = setTimeout(checkAuthTokenThenStart, 15000);
node.error("Auth check failed: " + err);
}
}
)
})();
} }
} }
@ -344,7 +363,8 @@ module.exports = function(RED) {
const matrixClient = sdk.createClient({ const matrixClient = sdk.createClient({
baseUrl: baseUrl, baseUrl: baseUrl,
deviceId: deviceId deviceId: deviceId,
localTimeoutMs: '30000'
}); });
matrixClient.login( matrixClient.login(