From 23cfdf6696c87ea0db9ff7cf3d77912f7b6e05d7 Mon Sep 17 00:00:00 2001 From: imnotbob Date: Sun, 27 Dec 2020 21:17:22 -0500 Subject: [PATCH] updates for failed cookie auth, and to restart so node library works correctly next time (#36) --- index.js | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 724d60bf..a729a26e 100644 --- a/index.js +++ b/index.js @@ -210,14 +210,33 @@ function startWebConfig() { formerRegistrationData: runTimeData.savedConfig.cookieData }, (err, result) => { if (result && Object.keys(result).length >= 2) { - sendCookiesToEndpoint((configData.settings.appCallbackUrl ? String(configData.settings.appCallbackUrl).replace("/receiveData?", "/cookie?") : null), result); - runTimeData.savedConfig.cookieData = result; - // console.log('RESULT: ' + err + ' / ' + JSON.stringify(result)); - logger.info('Successfully Refreshed Alexa Cookie...'); - res.send({ - result: JSON.stringify(result) + isCookieValid(result) + .then((valid) => { + if (valid) { + sendCookiesToEndpoint((configData.settings.appCallbackUrl ? String(configData.settings.appCallbackUrl).replace("/receiveData?", "/cookie?") : null), result); + runTimeData.savedConfig.cookieData = result; + // console.log('RESULT: ' + err + ' / ' + JSON.stringify(result)); + logger.info('Successfully Refreshed Alexa Cookie...'); + res.send({ + result: JSON.stringify(result) + }); + } else { + logger.error(`** ERROR: Unsuccessfully refreshed Alexa Cookie it was found to be invalid/expired... **`); + logger.error('RESULT: ' + err + ' / ' + JSON.stringify(result)); + logger.warn(`** WARNING: We are clearing the Cookie from ${configData.settings.hubPlatform} to prevent further requests and server load... **`); + sendClearAuthToST() + } }); + } else { + logger.error(`** ERROR: Unsuccessfully refreshed Alexa Cookie it was found to be invalid/expired... **`); + logger.error('RESULT: ' + err + ' / ' + JSON.stringify(result)); + logger.warn(`** WARNING: We are clearing the Cookie from ${configData.settings.hubPlatform} to prevent further requests and server load... **`); + sendClearAuthToST() } + setTimeout(() => { + logger.warn("Restarting after cookie refresh attempt"); + process.exit(1); + }, 25 * 1000); }); }); webApp.get('/configData', (req, res) => { @@ -854,4 +873,4 @@ function exitHandler(options, exitCode) { console.error("Could not close connections in time, forcefully shutting down"); process.exit(1); }, 2 * 1000); -} \ No newline at end of file +}