Skip to content

Commit

Permalink
wait for auth to come from server
Browse files Browse the repository at this point in the history
  • Loading branch information
pan261 committed Nov 8, 2021
1 parent f76f142 commit 88a589a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
1 change: 0 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ function createServer() {

const setCredentials = (cr) => {
credentials = cr
console.log(cr)
}

const getCredentials = () => {
Expand Down
54 changes: 43 additions & 11 deletions test.js
Original file line number Diff line number Diff line change
@@ -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);


// 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);
});
})();

0 comments on commit 88a589a

Please sign in to comment.