From 88a589af20f81c738082c8f256a9acb99c907a61 Mon Sep 17 00:00:00 2001 From: pan261 Date: Mon, 8 Nov 2021 17:03:24 -0500 Subject: [PATCH] wait for auth to come from server --- server.js | 1 - test.js | 54 +++++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/server.js b/server.js index b6d064c..d049dec 100644 --- a/server.js +++ b/server.js @@ -81,7 +81,6 @@ function createServer() { const setCredentials = (cr) => { credentials = cr - console.log(cr) } const getCredentials = () => { diff --git a/test.js b/test.js index fe07737..1c3acad 100644 --- a/test.js +++ b/test.js @@ -1,22 +1,54 @@ const authServer = require('./server'); - +const ForgeSDK = require('forge-apis'); /* Look at this for fetching new auth tokens in a loop: https://nodejs.org/en/docs/guides/timers-in-node/ */ -function intervalFunc() { - console.log('Running every 15 sec'); - cr = authServer.getCredentials(); - console.log(cr); - console.log(authServer.authClient); +var hasCredentials = false; +var credentials; +var authClient = authServer.authClient; + +var HubsApi = new ForgeSDK.HubsApi(); //Hubs Client +var BucketsApi = new ForgeSDK.BucketsApi(); //Buckets Client + +async function waitUntil(hasCredentials) { + return await new Promise(resolve => { + const interval = setInterval(() => { + console.log('Running every 5 sec, waiting for browser authorization at localhost:3000/auth'); + hasCredentials = authServer.getCredentials(); + if (hasCredentials != undefined) { + credentials = hasCredentials; + resolve('credentials recieved'); + clearInterval(interval); + }; + }, 5000); + }); } + (async () => { + // create the server let server = await authServer.create(); -})(); + + // wait until user authenticates through browser + await waitUntil(hasCredentials); + + // cannot shutdown the server becuase it is running on the same thread, doing this will just kill the node process (i.e. test.js) + // authServer.shutdown(server); + + console.log("Authenticated"); + // console.log(credentials); + // console.log(authClient); -/* - Testing setInterval function, will likely need to use this for refreshing auth tokens -*/ -setInterval(intervalFunc, 15000); \ No newline at end of file + + + // Can do use forge APIs now, using credentials and authClient + + // testing + HubsApi.getHubs({}, authClient, credentials).then(function(hubs) { + console.log(hubs.body.data); + }, function(err){ + console.error(err); + }); +})();