Skip to content

Commit

Permalink
clear credentials after 13 days (1 day buffer)
Browse files Browse the repository at this point in the history
  • Loading branch information
pan261 committed Nov 18, 2021
1 parent 933e7b6 commit 9c34966
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const oAuth2ThreeLegged = new ForgeSDK.AuthClientThreeLegged(

let credentials = null;
let refreshTime = null;
let intervalID = null;

/**
* Creates server with three endpoints:
Expand Down Expand Up @@ -66,20 +67,28 @@ function createServer () {
res.send('Generated token: ' + credentials.access_token);
})
.then(() => {
setInterval(() => refresh(), refreshTime * 1000); // seconds to ms
console.log(credentials);
// sets refresh() function to run on an interval every 55 minutes
intervalID = setInterval(() => refresh(), refreshTime * 1000); // 55 seconds to ms

// sets timeout for 13 days, before clearing refresh interval and clearing credentials
setTimeout(() => {
credentials = null;
clearInterval(intervalID);
console.log("Refresh token expiring, need to auth again");
}, 13 * 24 * 3600 * 1000); // 13 days to ms
})
.catch((err) => {
console.error(err);
res.send('Failed to authenticate');
res.send(err);
});
});

// Endpoint for internal use, to get credentials from our auth server
app.get('/credentials', function (req, res) {
res.send(credentials);
res.send(credentials ? credentials : "Need to authenticate at localhost:3000/auth");
});


// Default endpoint
app.use((err, req, res, next) => {
console.error(err);
res.status(err.statusCode).json(err);
Expand All @@ -97,7 +106,7 @@ const refresh = () => {
oAuth2ThreeLegged.refreshToken(credentials, scopes)
.then(creds => {
credentials = creds;
console.log('new token generated from refresh token');
console.log('new token generated from refresh token:');
console.log(credentials);
});
};
Expand Down

0 comments on commit 9c34966

Please sign in to comment.