From bc7500ce3e91564ffd046d1b2d0c3eb8b49c3640 Mon Sep 17 00:00:00 2001 From: Matt Low Date: Sat, 13 Mar 2021 14:51:32 -0700 Subject: [PATCH] Attempt to re-authenticate on auth refresh error --- src/index.ts | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/index.ts b/src/index.ts index a2c3c90..b1a6f59 100644 --- a/src/index.ts +++ b/src/index.ts @@ -68,16 +68,24 @@ async function initRingCentralSDK() { clientId: process.env.RC_APP_KEY, clientSecret: process.env.RC_APP_SECRET, }); + + const login = () => + sdk.login({ + username: process.env.RC_LOGIN_USERNAME, + extension: process.env.RC_LOGIN_EXT, + password: process.env.RC_LOGIN_PASSWORD, + }); + const platform = sdk.platform(); - platform.on(platform.events.refreshError, (err) => { - console.error(err); - }); - await sdk.login({ - username: process.env.RC_LOGIN_USERNAME, - extension: process.env.RC_LOGIN_EXT, - password: process.env.RC_LOGIN_PASSWORD, + platform.on(platform.events.refreshError, async (err) => { + console.error("Refresh token error:", err); + await login(); + console.log("RingCentral re-authentication successful."); }); + + await login(); console.log("Authenticated to RingCentral."); + return sdk; }