Skip to content

Commit 34826de

Browse files
authored
Handle invalidated refresh tokens and prompt for new login if required (#65)
1 parent 3ef3608 commit 34826de

3 files changed

Lines changed: 58 additions & 15 deletions

File tree

js/steamAPI.js

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,28 @@ import SteamUser from 'steam-user';
22
import { CONFIG, addRefreshTokenToLocalDatabase, getRefreshTokenFromLocalDatabase, steamUserLoginRequired } from './utils.js';
33

44
// ---------- Steam API ----------
5+
function logOn(steamClient, config) {
6+
return new Promise((resolve, reject) => {
7+
const cleanup = () => {
8+
steamClient.removeListener('loggedOn', onLoggedOn);
9+
steamClient.removeListener('error', onError);
10+
};
11+
12+
const onLoggedOn = () => {
13+
cleanup();
14+
resolve();
15+
};
16+
17+
const onError = (err) => {
18+
cleanup();
19+
reject(err);
20+
};
21+
22+
steamClient.once('loggedOn', onLoggedOn);
23+
steamClient.once('error', onError);
24+
steamClient.logOn(config);
25+
});
26+
}
527

628
let steamUserConfig = {};
729

@@ -12,15 +34,15 @@ if (steamUserLoginRequired) {
1234
refreshToken: refreshToken,
1335
};
1436
} else
15-
if (CONFIG.steamUser.accountName && CONFIG.steamUser.password) {
16-
steamUserConfig = {
17-
accountName: CONFIG.steamUser.accountName,
18-
password: CONFIG.steamUser.password,
37+
if (CONFIG.steamUser.accountName && CONFIG.steamUser.password) {
38+
steamUserConfig = {
39+
accountName: CONFIG.steamUser.accountName,
40+
password: CONFIG.steamUser.password,
41+
}
42+
} else {
43+
console.error("\"steamUser.useRefreshToken\" was provided in the config, but no refresh token found in local database. \"steamUser.accountName\" and \"steamUser.password\" are required in the config, but they were not provided!");
44+
process.exit(1);
1945
}
20-
} else {
21-
console.error("\"steamUser.useRefreshToken\" was provided in the config, but no refresh token found in local database. \"steamUser.accountName\" and \"steamUser.password\" are required in the config, but they were not provided!");
22-
process.exit(1);
23-
}
2446
} else {
2547
steamUserConfig = {
2648
anonymous: true
@@ -34,8 +56,29 @@ steamClient.on('refreshToken', async function (refreshToken) {
3456
});
3557

3658
console.log("Logging in to Steam", steamUserConfig.anonymous ? "anonymously..." : (steamUserConfig.accountName ? `as ${steamUserConfig.accountName}...` : "using a refresh token..."));
37-
steamClient.logOn(steamUserConfig);
38-
await new Promise(resolve => steamClient.on('loggedOn', resolve));
59+
60+
try {
61+
await logOn(steamClient, steamUserConfig);
62+
} catch (error) {
63+
if (steamUserConfig.refreshToken) {
64+
console.log("Login with refresh token failed. Removing it and trying account/password...");
65+
await addRefreshTokenToLocalDatabase(null);
66+
67+
if (CONFIG.steamUser.accountName && CONFIG.steamUser.password) {
68+
steamUserConfig = {
69+
accountName: CONFIG.steamUser.accountName,
70+
password: CONFIG.steamUser.password,
71+
};
72+
await logOn(steamClient, steamUserConfig);
73+
} else {
74+
console.error("No account name/password available. Provide credentials or a valid refresh token.");
75+
process.exit(1);
76+
}
77+
} else {
78+
console.error("Login to Steam failed:", error?.message || error);
79+
process.exit(1);
80+
}
81+
}
3982

4083
// Gets app info directly from the Steam Store API
4184
// Does not offer all info that the SteamUser API does
@@ -53,8 +96,8 @@ export async function getSteamAppInfoDirect(appId, retryCount = 0) {
5396
// If the request failed, we try again
5497
if (!result && retryCount < 3) {
5598
retryCount++;
56-
console.log(`Failed to get app info for app ${appId} from the Steam Store API. Retrying in ${retryCount**2} second(s)...`);
57-
await new Promise(r => setTimeout(r, (retryCount**2) * 1000));
99+
console.log(`Failed to get app info for app ${appId} from the Steam Store API. Retrying in ${retryCount ** 2} second(s)...`);
100+
await new Promise(r => setTimeout(r, (retryCount ** 2) * 1000));
58101
return getSteamAppInfoDirect(appId, retryCount);
59102
} else if (retryCount >= 3) {
60103
console.log(`Failed to get app info for app ${appId} from the Steam Store API. Some info may still be available using the SteamUser API.`);

package-lock.json

Lines changed: 2 additions & 2 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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "notion-steam-api-integration",
33
"type": "module",
4-
"version": "1.9.1",
4+
"version": "1.9.2",
55
"description": "Notion integration to fetch data from the Steam API and populate a database given Steam App ID's.",
66
"main": "index.js",
77
"dependencies": {

0 commit comments

Comments
 (0)