Skip to content

Commit 2cca250

Browse files
authored
Debug Access Token Refresh (#43)
* This does it * Test deploy * token is always expired, for testing purposes * token is expired once until refreshed, for testing purposes * token is expired once until refreshed, for testing purposes * successful auto refresh on dev * Added an expired token to mock the process one more time. * log wording * Ready for production * dumb extra space
1 parent 58d59ba commit 2cca250

3 files changed

Lines changed: 46 additions & 7 deletions

File tree

package-lock.json

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"debug": "~2.6.9",
1414
"dotenv": "^10.0.0",
1515
"dotenv-expand": "^8.0.1",
16+
"envfile": "^7.1.0",
1617
"express": "^4.18.2",
1718
"got": "^11.8.3",
1819
"http-errors": "~1.6.3",

tokens.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
require('dotenv').config()
22
const got = require('got')
3+
const fs = require('node:fs/promises')
4+
const { parse, stringify } = require('envfile')
5+
const sourcePath = '.env'
6+
let expired = true
37

48
// https://stackoverflow.com/a/69058154/1413302
59
const isTokenExpired = (token) => (Date.now() >= JSON.parse(Buffer.from(token.split('.')[1], 'base64').toString()).exp * 1000)
@@ -11,21 +15,35 @@ const isTokenExpired = (token) => (Date.now() >= JSON.parse(Buffer.from(token.sp
1115
*/
1216
async function generateNewAccessToken() {
1317
try {
14-
const { tokenObject } = await got.post(process.env.RERUM_ACCESS_TOKEN_URL, {
18+
const tokenObject = await got.post(process.env.RERUM_ACCESS_TOKEN_URL, {
1519
timeout: 10000,
16-
json: { REFRESH_TOKEN: process.env.REFRESH_TOKEN }
20+
json: { "refresh_token": process.env.REFRESH_TOKEN }
1721
}).json()
18-
1922
process.env.ACCESS_TOKEN = tokenObject.access_token
23+
try{
24+
const data = await fs.readFile('./.env', { encoding: 'utf8' })
25+
// Please note that this parse() will remove all #comments in the .env file.
26+
let env_file_obj = parse(data)
27+
env_file_obj.ACCESS_TOKEN = tokenObject.access_token
28+
await fs.writeFile('./.env', stringify(env_file_obj))
29+
console.log("TinyNode now has an updated access token.")
30+
}
31+
catch(env_error){
32+
console.error("Could not write new token property to the file. The access token has not been updated.")
33+
console.error(env_error)
34+
}
35+
}
36+
catch (err) {
37+
console.error("Access token not updated: ", err)
2038
}
21-
catch (err) { console.error("Token not updated: ", err) }
22-
23-
console.warn("Access Token expired. Consider updating your .env files")
2439
}
2540

2641
/**
2742
* This will conduct a simple check against the expiry date in your token.
2843
* This does not validate your access token, so you may still be rejected by
2944
* your RERUM instance as unauthorized.
3045
*/
31-
//if (isTokenExpired(process.env.ACCESS_TOKEN)) { generateNewAccessToken() }
46+
if (isTokenExpired(process.env.ACCESS_TOKEN)) {
47+
console.log("Tiny Node detected an expired access token. Updating the token now.")
48+
generateNewAccessToken()
49+
}

0 commit comments

Comments
 (0)